summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/dashboard/url.rb
blob: 10a2f3c2397dc3e0bfda8e75f6f39c26516632a2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true

# Manages url matching for metrics dashboards.
module Gitlab
  module Metrics
    module Dashboard
      class Url
        class << self
          include Gitlab::Utils::StrongMemoize

          QUERY_PATTERN = '(?<query>\?[a-zA-Z0-9%.()+_=-]+(&[a-zA-Z0-9%.()+_=-]+)*)?'
          ANCHOR_PATTERN = '(?<anchor>\#[a-z0-9_-]+)?'
          OPTIONAL_DASH_PATTERN = '(?:/-)?'

          # Matches urls for a metrics dashboard. This could be
          # either the /metrics endpoint or the /metrics_dashboard
          # endpoint.
          #
          # EX - https://<host>/<namespace>/<project>/environments/<env_id>/metrics
          def metrics_regex
            strong_memoize(:metrics_regex) do
              regex_for_project_metrics(
                %r{
                    /environments
                    /(?<environment>\d+)
                    /(metrics_dashboard|metrics)
                }x
              )
            end
          end

          # Matches dashboard urls for a Grafana embed.
          #
          # EX - https://<host>/<namespace>/<project>/grafana/metrics_dashboard
          def grafana_regex
            strong_memoize(:grafana_regex) do
              regex_for_project_metrics(
                %r{
                  /grafana
                  /metrics_dashboard
                }x
              )
            end
          end

          # Parses query params out from full url string into hash.
          #
          # Ex) 'https://<root>/<project>/<environment>/metrics?title=Title&group=Group'
          #       --> { title: 'Title', group: 'Group' }
          def parse_query(url)
            query_string = URI.parse(url).query.to_s

            CGI.parse(query_string)
              .transform_values { |value| value.first }
              .symbolize_keys
          end

          # Builds a metrics dashboard url based on the passed in arguments
          def build_dashboard_url(*args)
            Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_environment_url(*args)
          end

          # Matches dashboard urls for a metric chart embed
          # for cluster metrics
          #
          # EX - https://<host>/<namespace>/<project>/-/clusters/<cluster_id>/?group=Cluster%20Health&title=Memory%20Usage&y_label=Memory%20(GiB)
          def clusters_regex
            strong_memoize(:clusters_regex) do
              regex_for_project_metrics(
                %r{
                  /clusters
                  /(?<cluster_id>\d+)
                  /?
                }x
              )
            end
          end

          private

          def regex_for_project_metrics(path_suffix_pattern)
            %r{
              (?<url>
                #{gitlab_host_pattern}
                #{project_path_pattern}
                #{OPTIONAL_DASH_PATTERN}
                #{path_suffix_pattern}
                #{QUERY_PATTERN}
                #{ANCHOR_PATTERN}
              )
            }x
          end

          def gitlab_host_pattern
            Regexp.escape(Gitlab.config.gitlab.url)
          end

          def project_path_pattern
            "\/#{Project.reference_pattern}"
          end
        end
      end
    end
  end
end

Gitlab::Metrics::Dashboard::Url.extend_if_ee('::EE::Gitlab::Metrics::Dashboard::Url')