summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/dashboard/importer.rb
blob: ca8356506489c64ac70de2e380c370169e259af4 (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
# frozen_string_literal: true

module Gitlab
  module Metrics
    module Dashboard
      class Importer
        def initialize(dashboard_path, project)
          @dashboard_path = dashboard_path.to_s
          @project = project
        end

        def execute
          return false unless Dashboard::Validator.validate(dashboard_hash, project: project, dashboard_path: dashboard_path)

          Dashboard::Importers::PrometheusMetrics.new(dashboard_hash, project: project, dashboard_path: dashboard_path).execute
        rescue Gitlab::Config::Loader::FormatError
          false
        end

        def execute!
          Dashboard::Validator.validate!(dashboard_hash, project: project, dashboard_path: dashboard_path)

          Dashboard::Importers::PrometheusMetrics.new(dashboard_hash, project: project, dashboard_path: dashboard_path).execute!
        end

        private

        attr_accessor :dashboard_path, :project

        def dashboard_hash
          @dashboard_hash ||= begin
            raw_dashboard = Dashboard::RepoDashboardFinder.read_dashboard(project, dashboard_path)
            return unless raw_dashboard.present?

            ::Gitlab::Config::Loader::Yaml.new(raw_dashboard).load_raw!
          end
        end
      end
    end
  end
end