debian-mirror-gitlab/app/models/merge_request/metrics.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.6 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class MergeRequest::Metrics < ApplicationRecord
2020-10-24 23:57:45 +05:30
belongs_to :merge_request, inverse_of: :metrics
2017-08-17 22:00:37 +05:30
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :pipeline_id
2018-03-17 18:26:18 +05:30
belongs_to :latest_closed_by, class_name: 'User'
belongs_to :merged_by, class_name: 'User'
2021-03-11 19:13:27 +05:30
belongs_to :target_project, class_name: 'Project', inverse_of: :merge_requests
2020-10-24 23:57:45 +05:30
before_save :ensure_target_project_id
scope :merged_after, ->(date) { where(arel_table[:merged_at].gteq(date)) }
scope :merged_before, ->(date) { where(arel_table[:merged_at].lteq(date)) }
2021-02-22 17:27:13 +05:30
scope :with_valid_time_to_merge, -> { where(arel_table[:merged_at].gt(arel_table[:created_at])) }
2021-03-11 19:13:27 +05:30
scope :by_target_project, ->(project) { where(target_project_id: project) }
2021-02-22 17:27:13 +05:30
2021-11-11 11:23:49 +05:30
class << self
def time_to_merge_expression
Arel.sql('EXTRACT(epoch FROM SUM(AGE(merge_request_metrics.merged_at, merge_request_metrics.created_at)))')
end
def record!(mr)
sql = <<~SQL
INSERT INTO #{self.table_name} (merge_request_id, target_project_id, updated_at, created_at)
VALUES (#{mr.id}, #{mr.target_project_id}, NOW(), NOW())
ON CONFLICT (merge_request_id)
DO UPDATE SET
target_project_id = EXCLUDED.target_project_id,
updated_at = NOW()
SQL
connection.execute(sql)
end
2021-02-22 17:27:13 +05:30
end
2020-10-24 23:57:45 +05:30
private
def ensure_target_project_id
self.target_project_id ||= merge_request.target_project_id
end
2021-03-11 19:13:27 +05:30
def self.total_time_to_merge
with_valid_time_to_merge
.pluck(time_to_merge_expression)
.first
end
2016-09-29 09:46:39 +05:30
end
2020-04-08 14:13:33 +05:30
2021-06-08 01:23:25 +05:30
MergeRequest::Metrics.prepend_mod_with('MergeRequest::Metrics')