2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
class PipelineMetricsWorker # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2021-04-30 14:10:09 -04:00
|
|
|
|
2021-07-21 08:09:35 -04:00
|
|
|
data_consistency :always
|
|
|
|
|
2021-04-30 14:10:09 -04:00
|
|
|
sidekiq_options retry: 3
|
2016-10-21 12:13:41 -04:00
|
|
|
include PipelineQueue
|
2016-10-14 13:08:48 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
urgency :high
|
2019-10-30 11:14:17 -04:00
|
|
|
|
2016-10-14 13:08:48 -04:00
|
|
|
def perform(pipeline_id)
|
2021-11-10 10:13:21 -05:00
|
|
|
Ci::Pipeline.find_by_id(pipeline_id).try do |pipeline|
|
2016-10-17 03:55:47 -04:00
|
|
|
update_metrics_for_active_pipeline(pipeline) if pipeline.active?
|
|
|
|
update_metrics_for_succeeded_pipeline(pipeline) if pipeline.success?
|
2016-10-14 13:08:48 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-17 03:55:47 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_metrics_for_active_pipeline(pipeline)
|
2016-11-15 09:22:35 -05:00
|
|
|
metrics(pipeline).update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: nil, pipeline_id: pipeline.id)
|
2016-10-17 03:55:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_metrics_for_succeeded_pipeline(pipeline)
|
2016-11-15 09:22:35 -05:00
|
|
|
metrics(pipeline).update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: pipeline.finished_at, pipeline_id: pipeline.id)
|
2016-10-17 03:55:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def metrics(pipeline)
|
2021-11-10 10:13:21 -05:00
|
|
|
MergeRequest::Metrics.where(merge_request_id: merge_requests(pipeline)) # rubocop: disable CodeReuse/ActiveRecord
|
2016-10-17 03:55:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_requests(pipeline)
|
2019-03-01 00:10:07 -05:00
|
|
|
pipeline.merge_requests_as_head_pipeline.map(&:id)
|
2016-10-17 03:55:47 -04:00
|
|
|
end
|
2016-10-14 13:08:48 -04:00
|
|
|
end
|