2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class MergeRequest::Metrics < ApplicationRecord
|
2020-08-05 08:09:45 -04:00
|
|
|
belongs_to :merge_request, inverse_of: :metrics
|
2016-11-15 09:22:35 -05:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :pipeline_id
|
2017-12-07 12:41:30 -05:00
|
|
|
belongs_to :latest_closed_by, class_name: 'User'
|
|
|
|
belongs_to :merged_by, class_name: 'User'
|
2021-01-27 04:09:01 -05:00
|
|
|
belongs_to :target_project, class_name: 'Project', inverse_of: :merge_requests
|
2020-08-05 08:09:45 -04:00
|
|
|
|
|
|
|
before_save :ensure_target_project_id
|
|
|
|
|
2020-08-06 14:09:41 -04:00
|
|
|
scope :merged_after, ->(date) { where(arel_table[:merged_at].gteq(date)) }
|
|
|
|
scope :merged_before, ->(date) { where(arel_table[:merged_at].lteq(date)) }
|
2020-12-02 19:09:53 -05:00
|
|
|
scope :with_valid_time_to_merge, -> { where(arel_table[:merged_at].gt(arel_table[:created_at])) }
|
2021-01-27 04:09:01 -05:00
|
|
|
scope :by_target_project, ->(project) { where(target_project_id: project) }
|
2020-12-02 19:09:53 -05:00
|
|
|
|
|
|
|
def self.time_to_merge_expression
|
|
|
|
Arel.sql('EXTRACT(epoch FROM SUM(AGE(merge_request_metrics.merged_at, merge_request_metrics.created_at)))')
|
|
|
|
end
|
2020-08-06 14:09:41 -04:00
|
|
|
|
2020-08-05 08:09:45 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_target_project_id
|
|
|
|
self.target_project_id ||= merge_request.target_project_id
|
|
|
|
end
|
2021-01-27 04:09:01 -05:00
|
|
|
|
|
|
|
def self.total_time_to_merge
|
|
|
|
with_valid_time_to_merge
|
|
|
|
.pluck(time_to_merge_expression)
|
|
|
|
.first
|
|
|
|
end
|
2016-08-25 01:54:41 -04:00
|
|
|
end
|
2020-03-03 07:08:08 -05:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MergeRequest::Metrics.prepend_mod_with('MergeRequest::Metrics')
|