Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab_alert_embed_service.rb « dashboard « metrics « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38e89d392adb0f3e8f52257ad6d959d333ced708 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

# Responsible for returning an embed containing the specified
# metrics chart for an alert. Creates panels based on the
# matching metric stored in the database.
#
# Use Gitlab::Metrics::Dashboard::Finder to retrieve dashboards.
module Metrics
  module Dashboard
    class GitlabAlertEmbedService < ::Metrics::Dashboard::BaseEmbedService
      include Gitlab::Utils::StrongMemoize

      SEQUENCE = [
        STAGES::EndpointInserter,
        STAGES::PanelIdsInserter
      ].freeze

      class << self
        # Determines whether the provided params are sufficient
        # to uniquely identify a panel composed of user-defined
        # custom metrics from the DB.
        def valid_params?(params)
          [
            embedded?(params[:embedded]),
            params[:prometheus_alert_id].is_a?(Integer)
          ].all?
        end
      end

      def raw_dashboard
        panels_not_found!(alert_id: alert_id) unless alert && prometheus_metric

        { 'panel_groups' => [{ 'panels' => [panel] }] }
      end

      private

      def allowed?
        Ability.allowed?(current_user, :read_prometheus_alerts, project)
      end

      def alert_id
        params[:prometheus_alert_id]
      end

      def alert
        strong_memoize(:alert) do
          Projects::Prometheus::AlertsFinder.new(id: alert_id).execute.first
        end
      end

      def process_params
        params.merge(environment: alert.environment)
      end

      def prometheus_metric
        strong_memoize(:prometheus_metric) do
          PrometheusMetricsFinder.new(id: alert.prometheus_metric_id).execute.first
        end
      end

      def panel
        {
          title: prometheus_metric.title,
          y_label: prometheus_metric.y_label,
          metrics: [prometheus_metric.to_metric_hash]
        }
      end

      def sequence
        SEQUENCE
      end
    end
  end
end