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

# Searches a projects repository for a metrics dashboard and formats the output.
# Expects any custom dashboards will be located in `.gitlab/dashboards`
module Gitlab
  module Metrics
    module Dashboard
      class BaseService < ::BaseService
        DASHBOARD_LAYOUT_ERROR = Gitlab::Metrics::Dashboard::Stages::BaseStage::DashboardLayoutError

        def get_dashboard
          return error("#{dashboard_path} could not be found.", :not_found) unless path_available?

          success(dashboard: process_dashboard)
        rescue DASHBOARD_LAYOUT_ERROR => e
          error(e.message, :unprocessable_entity)
        end

        # Summary of all known dashboards for the service.
        # @return [Array<Hash>] ex) [{ path: String, default: Boolean }]
        def all_dashboard_paths(_project)
          raise NotImplementedError
        end

        private

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

        # @return [String] Relative filepath of the dashboard yml
        def dashboard_path
          params[:dashboard_path]
        end

        # Returns an un-processed dashboard from the cache.
        def raw_dashboard
          Rails.cache.fetch(cache_key) { get_raw_dashboard }
        end

        # @return [Hash] an unmodified dashboard
        def get_raw_dashboard
          raise NotImplementedError
        end

        # @return [String]
        def cache_key
          raise NotImplementedError
        end

        # Determines whether custom metrics should be included
        # in the processed output.
        def insert_project_metrics?
          false
        end

        # Checks if dashboard path exists or should be rejected
        # as a result of file-changes to the project repository.
        # @return [Boolean]
        def path_available?
          available_paths = Gitlab::Metrics::Dashboard::Finder.find_all_paths(project)

          available_paths.any? do |path_params|
            path_params[:path] == dashboard_path
          end
        end
      end
    end
  end
end