2018-06-27 07:23:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-14 17:08:48 +00:00
|
|
|
class PipelineMetricsWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2016-10-21 16:13:41 +00:00
|
|
|
include PipelineQueue
|
2016-10-14 17:08:48 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-14 17:08:48 +00:00
|
|
|
def perform(pipeline_id)
|
|
|
|
Ci::Pipeline.find_by(id: pipeline_id).try do |pipeline|
|
2016-10-17 07:55:47 +00:00
|
|
|
update_metrics_for_active_pipeline(pipeline) if pipeline.active?
|
|
|
|
update_metrics_for_succeeded_pipeline(pipeline) if pipeline.success?
|
2016-10-14 17:08:48 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-17 07:55:47 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_metrics_for_active_pipeline(pipeline)
|
2016-11-15 14:22:35 +00:00
|
|
|
metrics(pipeline).update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: nil, pipeline_id: pipeline.id)
|
2016-10-17 07:55:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_metrics_for_succeeded_pipeline(pipeline)
|
2016-11-15 14:22:35 +00: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 07:55:47 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-10-17 07:55:47 +00:00
|
|
|
def metrics(pipeline)
|
|
|
|
MergeRequest::Metrics.where(merge_request_id: merge_requests(pipeline))
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-10-17 07:55:47 +00:00
|
|
|
|
|
|
|
def merge_requests(pipeline)
|
2019-03-01 05:10:07 +00:00
|
|
|
pipeline.merge_requests_as_head_pipeline.map(&:id)
|
2016-10-17 07:55:47 +00:00
|
|
|
end
|
2016-10-14 17:08:48 +00:00
|
|
|
end
|