2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class Issue::Metrics < ApplicationRecord
|
2016-09-29 09:46:39 +05:30
|
|
|
belongs_to :issue
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
scope :for_issues, ->(issues) { where(issue: issues) }
|
|
|
|
scope :with_first_mention_not_earlier_than, -> (timestamp) {
|
|
|
|
where(first_mentioned_in_commit_at: nil)
|
|
|
|
.or(where(arel_table['first_mentioned_in_commit_at'].gteq(timestamp)))
|
|
|
|
}
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
class << self
|
|
|
|
def record!(issue)
|
|
|
|
now = connection.quote(Time.current)
|
|
|
|
first_associated_with_milestone_at = issue.milestone_id.present? ? now : 'NULL'
|
|
|
|
first_added_to_board_at = issue_assigned_to_list_label?(issue) ? now : 'NULL'
|
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
INSERT INTO #{self.table_name} (issue_id, first_associated_with_milestone_at, first_added_to_board_at, created_at, updated_at)
|
|
|
|
VALUES (#{issue.id}, #{first_associated_with_milestone_at}, #{first_added_to_board_at}, NOW(), NOW())
|
|
|
|
ON CONFLICT (issue_id)
|
|
|
|
DO UPDATE SET
|
|
|
|
first_associated_with_milestone_at = LEAST(#{self.table_name}.first_associated_with_milestone_at, EXCLUDED.first_associated_with_milestone_at),
|
|
|
|
first_added_to_board_at = LEAST(#{self.table_name}.first_added_to_board_at, EXCLUDED.first_added_to_board_at),
|
|
|
|
updated_at = NOW()
|
|
|
|
RETURNING id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
connection.execute(sql)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
private
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def issue_assigned_to_list_label?(issue)
|
|
|
|
issue.labels.joins(:lists).exists?
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|