summaryrefslogtreecommitdiff
path: root/app/models/deployment_metrics.rb
blob: 2056c8bc59c9020d715950f107d2a98f1b49ae95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

class DeploymentMetrics
  include Gitlab::Utils::StrongMemoize

  attr_reader :project, :deployment

  delegate :cluster, to: :deployment

  def initialize(project, deployment)
    @project = project
    @deployment = deployment
  end

  def has_metrics?
    deployment.success? && prometheus_adapter&.can_query?
  end

  def metrics
    return {} unless has_metrics?

    metrics = prometheus_adapter.query(:deployment, deployment)
    metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
  end

  def additional_metrics
    return {} unless has_metrics?

    metrics = prometheus_adapter.query(:additional_metrics_deployment, deployment)
    metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
  end

  private

  def prometheus_adapter
    strong_memoize(:prometheus_adapter) do
      service = project.find_or_initialize_service('prometheus')

      if service.can_query?
        service
      else
        cluster_prometheus
      end
    end
  end

  def cluster_prometheus
    cluster.application_prometheus if cluster&.application_prometheus_available?
  end
end