BigW Consortium Gitlab

pipeline_metrics_worker.rb 932 Bytes
Newer Older
1 2
class PipelineMetricsWorker
  include Sidekiq::Worker
3
  include PipelineQueue
4 5 6

  def perform(pipeline_id)
    Ci::Pipeline.find_by(id: pipeline_id).try do |pipeline|
7 8
      update_metrics_for_active_pipeline(pipeline) if pipeline.active?
      update_metrics_for_succeeded_pipeline(pipeline) if pipeline.success?
9 10
    end
  end
11 12 13 14

  private

  def update_metrics_for_active_pipeline(pipeline)
15
    metrics(pipeline).update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: nil, pipeline_id: pipeline.id)
16 17 18
  end

  def update_metrics_for_succeeded_pipeline(pipeline)
19
    metrics(pipeline).update_all(latest_build_started_at: pipeline.started_at, latest_build_finished_at: pipeline.finished_at, pipeline_id: pipeline.id)
20 21 22 23 24 25 26 27 28
  end

  def metrics(pipeline)
    MergeRequest::Metrics.where(merge_request_id: merge_requests(pipeline))
  end

  def merge_requests(pipeline)
    pipeline.merge_requests.map(&:id)
  end
29
end