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

# Fetches the metrics dashboard layout and supplemented the output with DB info.
module Gitlab
  module Metrics
    module Dashboard
      class Service < ::BaseService
        SYSTEM_DASHBOARD_NAME = 'common_metrics'
        SYSTEM_DASHBOARD_PATH = Rails.root.join('config', 'prometheus', "#{SYSTEM_DASHBOARD_NAME}.yml")

        # Returns a DB-supplemented json representation of a dashboard config file.
        def get_dashboard
          dashboard_string = Rails.cache.fetch(cache_key) { system_dashboard }

          dashboard = process_dashboard(dashboard_string)

          success(dashboard: dashboard)
        rescue Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardLayoutError => e
          error(e.message, :unprocessable_entity)
        end

        private

        # Returns the base metrics shipped with every GitLab service.
        def system_dashboard
          YAML.safe_load(File.read(SYSTEM_DASHBOARD_PATH))
        end

        def cache_key
          "metrics_dashboard_#{SYSTEM_DASHBOARD_NAME}"
        end

        # Returns a new dashboard Hash, supplemented with DB info
        def process_dashboard(dashboard)
          Gitlab::Metrics::Dashboard::Processor.new(project, params[:environment]).process(dashboard)
        end
      end
    end
  end
end