summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/dashboard/finder.rb
blob: 1373830844bd4f310c3f78f760f6a2bb82fb41ba (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

# Returns DB-supplmented dashboard info for determining
# the layout of UI. Intended entry-point for the Metrics::Dashboard
# module.
module Gitlab
  module Metrics
    module Dashboard
      class Finder
        class << self
          # Returns a formatted dashboard packed with DB info.
          # @param project [Project]
          # @param user [User]
          # @param environment [Environment]
          # @param opts - dashboard_path [String] Path at which the
          #         dashboard can be found. Nil values will
          #         default to the system dashboard.
          # @param opts - embedded [Boolean] Determines whether the
          #         dashboard is to be rendered as part of an
          #         issue or location other than the primary
          #         metrics dashboard UI. Returns only the
          #         Memory/CPU charts of the system dash.
          # @return [Hash]
          def find(project, user, environment, dashboard_path: nil, embedded: false)
            service_for_path(dashboard_path, embedded: embedded)
              .new(project, user, environment: environment, dashboard_path: dashboard_path)
              .get_dashboard
          end

          # Summary of all known dashboards.
          # @return [Array<Hash>] ex) [{ path: String,
          #                              display_name: String,
          #                              default: Boolean }]
          def find_all_paths(project)
            project.repository.metrics_dashboard_paths
          end

          # Summary of all known dashboards. Used to populate repo cache.
          # Prefer #find_all_paths.
          def find_all_paths_from_source(project)
            Gitlab::Metrics::Dashboard::Cache.delete_all!

            system_service.all_dashboard_paths(project)
            .+ project_service.all_dashboard_paths(project)
          end

          private

          def service_for_path(dashboard_path, embedded:)
            return embed_service if embedded
            return system_service if system_dashboard?(dashboard_path)

            project_service
          end

          def system_service
            ::Metrics::Dashboard::SystemDashboardService
          end

          def project_service
            ::Metrics::Dashboard::ProjectDashboardService
          end

          def embed_service
            ::Metrics::Dashboard::DefaultEmbedService
          end

          def system_dashboard?(filepath)
            !filepath || system_service.system_dashboard?(filepath)
          end
        end
      end
    end
  end
end