Commit 8705340a authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'ab-cluster-banzai-filter-2' into 'master'

Add Banzai Filter for Cluster Metrics

See merge request gitlab-org/gitlab!25643
parents 45fdbab7 87233904
# frozen_string_literal: true
module Banzai
module Filter
class InlineClusterMetricsFilter < ::Banzai::Filter::InlineEmbedsFilter
def create_element(params)
doc.document.create_element(
'div',
class: 'js-render-metrics',
'data-dashboard-url': metrics_dashboard_url(params)
)
end
def embed_params(node)
url = node['href']
return unless [:group, :title, :y_label].all? do |param|
query_params(url).include?(param)
end
link_pattern.match(url) { |m| m.named_captures }.symbolize_keys
end
def xpath_search
"descendant-or-self::a[contains(@href,'clusters') and \
starts-with(@href, '#{::Gitlab.config.gitlab.url}')]"
end
def link_pattern
::Gitlab::Metrics::Dashboard::Url.clusters_regex
end
def metrics_dashboard_url(params)
::Gitlab::Routing.url_helpers.metrics_dashboard_namespace_project_cluster_url(
params[:namespace],
params[:project],
params[:cluster_id],
# Only Project clusters are supported for now
# admin and group cluster types may be supported in the future
cluster_type: :project,
embedded: true,
format: :json,
**query_params(params['url'])
)
end
end
end
end
......@@ -21,6 +21,13 @@ module EE
*super
]
end
def metrics_filters
[
::Banzai::Filter::InlineClusterMetricsFilter,
*super
]
end
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Banzai::Filter::InlineClusterMetricsFilter do
include FilterSpecHelper
let!(:cluster) { create(:cluster) }
let!(:project) { create(:project) }
let(:params) { [project.namespace.path, project.path, cluster.id] }
let(:query_params) { { group: 'Food metrics', title: 'Pizza Consumption', y_label: 'Slice Count' } }
let(:trigger_url) { urls.metrics_namespace_project_cluster_url(*params, **query_params) }
let(:dashboard_url) do
urls.metrics_dashboard_namespace_project_cluster_url(
*params,
**{
embedded: true,
cluster_type: 'project',
format: :json
}.merge(query_params)
)
end
it_behaves_like 'a metrics embed filter'
end
......@@ -6,6 +6,7 @@ module Banzai
# a given link format. To transform references to DB
# resources in place, prefer to inherit from AbstractReferenceFilter.
class InlineEmbedsFilter < HTML::Pipeline::Filter
include Gitlab::Utils::StrongMemoize
# Find every relevant link, create a new node based on
# the link, and insert this node after any html content
# surrounding the link.
......@@ -60,6 +61,16 @@ module Banzai
link_pattern.match(url) { |m| m.named_captures }
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 query_params(url)
strong_memoize(:query_params) do
Gitlab::Metrics::Dashboard::Url.parse_query(url)
end
end
end
end
end
......@@ -42,14 +42,6 @@ module Banzai
**query_params(params['url'])
)
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 query_params(url)
Gitlab::Metrics::Dashboard::Url.parse_query(url)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment