summaryrefslogtreecommitdiff
path: root/app/services/metrics/dashboard/panel_preview_service.rb
blob: 02dd908e229320712d2a9572d401a6f278396107 (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
# frozen_string_literal: true

# Ingest YAML fragment with metrics dashboard panel definition
# https://docs.gitlab.com/ee/operations/metrics/dashboards/yaml.html#panel-panels-properties
# process it and returns renderable json version
module Metrics
  module Dashboard
    class PanelPreviewService
      SEQUENCE = [
        ::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
        ::Gitlab::Metrics::Dashboard::Stages::MetricEndpointInserter,
        ::Gitlab::Metrics::Dashboard::Stages::PanelIdsInserter,
        ::Gitlab::Metrics::Dashboard::Stages::AlertsInserter,
        ::Gitlab::Metrics::Dashboard::Stages::UrlValidator
      ].freeze

      HANDLED_PROCESSING_ERRORS = [
        Gitlab::Metrics::Dashboard::Errors::DashboardProcessingError,
        Gitlab::Config::Loader::Yaml::NotHashError,
        Gitlab::Config::Loader::Yaml::DataTooLargeError,
        Gitlab::Config::Loader::FormatError
      ].freeze

      def initialize(project, panel_yaml, environment)
        @project = project
        @panel_yaml = panel_yaml
        @environment = environment
      end

      def execute
        dashboard = ::Gitlab::Metrics::Dashboard::Processor.new(project, dashboard_structure, SEQUENCE, environment: environment).process
        ServiceResponse.success(payload: dashboard[:panel_groups][0][:panels][0])
      rescue *HANDLED_PROCESSING_ERRORS => error
        ServiceResponse.error(message: error.message)
      end

      private

      attr_accessor :project, :panel_yaml, :environment

      def dashboard_structure
        {
          panel_groups: [
            {
              panels: [panel_hash]
            }
          ]
        }
      end

      def panel_hash
        ::Gitlab::Config::Loader::Yaml.new(panel_yaml).load_raw!
      end
    end
  end
end