1# frozen_string_literal: true
2
3module Banzai
4  module Filter
5    class InlineClusterMetricsFilter < ::Banzai::Filter::InlineEmbedsFilter
6      def embed_params(node)
7        url = node['href']
8        @query_params = query_params(url)
9        return unless [:group, :title, :y_label].all? do |param|
10          @query_params.include?(param)
11        end
12
13        link_pattern.match(url) { |m| m.named_captures }.symbolize_keys
14      end
15
16      def xpath_search
17        "descendant-or-self::a[contains(@href,'clusters') and \
18          starts-with(@href, '#{gitlab_domain}')]"
19      end
20
21      def link_pattern
22        ::Gitlab::Metrics::Dashboard::Url.clusters_regex
23      end
24
25      def metrics_dashboard_url(params)
26        ::Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_cluster_url(
27          params[:namespace],
28          params[:project],
29          params[:cluster_id],
30          # Only Project clusters are supported for now
31          # admin and group cluster types may be supported in the future
32          cluster_type: :project,
33          embedded: true,
34          format: :json,
35          **@query_params
36        )
37      end
38    end
39  end
40end
41