Commit 88d9713d authored by Michael Kozono's avatar Michael Kozono

Merge branch '210008-the-same-chart-appears-twice-for-different-embeds' into 'master'

Resolve "The same chart appears twice for different embeds"

See merge request gitlab-org/gitlab!26997
parents 9a83054d 56e1d45c
---
title: Fix embeds so that a chart appears only once
merge_request: 26997
author:
type: fixed
...@@ -5,9 +5,10 @@ module Banzai ...@@ -5,9 +5,10 @@ module Banzai
class InlineClusterMetricsFilter < ::Banzai::Filter::InlineEmbedsFilter class InlineClusterMetricsFilter < ::Banzai::Filter::InlineEmbedsFilter
def embed_params(node) def embed_params(node)
url = node['href'] url = node['href']
@query_params = query_params(url)
return unless [:group, :title, :y_label].all? do |param| return unless [:group, :title, :y_label].all? do |param|
query_params(url).include?(param) @query_params.include?(param)
end end
link_pattern.match(url) { |m| m.named_captures }.symbolize_keys link_pattern.match(url) { |m| m.named_captures }.symbolize_keys
...@@ -32,7 +33,7 @@ module Banzai ...@@ -32,7 +33,7 @@ module Banzai
cluster_type: :project, cluster_type: :project,
embedded: true, embedded: true,
format: :json, format: :json,
**query_params(params['url']) **@query_params
) )
end end
end end
......
...@@ -6,7 +6,6 @@ module Banzai ...@@ -6,7 +6,6 @@ module Banzai
# a given link format. To transform references to DB # a given link format. To transform references to DB
# resources in place, prefer to inherit from AbstractReferenceFilter. # resources in place, prefer to inherit from AbstractReferenceFilter.
class InlineEmbedsFilter < HTML::Pipeline::Filter class InlineEmbedsFilter < HTML::Pipeline::Filter
include Gitlab::Utils::StrongMemoize
# Find every relevant link, create a new node based on # Find every relevant link, create a new node based on
# the link, and insert this node after any html content # the link, and insert this node after any html content
# surrounding the link. # surrounding the link.
...@@ -74,9 +73,7 @@ module Banzai ...@@ -74,9 +73,7 @@ module Banzai
# Ex) 'https://<root>/<project>/<environment>/metrics?title=Title&group=Group' # Ex) 'https://<root>/<project>/<environment>/metrics?title=Title&group=Group'
# --> { title: 'Title', group: 'Group' } # --> { title: 'Title', group: 'Group' }
def query_params(url) def query_params(url)
strong_memoize(:query_params) do Gitlab::Metrics::Dashboard::Url.parse_query(url)
Gitlab::Metrics::Dashboard::Url.parse_query(url)
end
end end
# Implement in child class. # Implement in child class.
......
...@@ -62,6 +62,29 @@ describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidek ...@@ -62,6 +62,29 @@ describe 'Metrics rendering', :js, :use_clean_rails_memory_store_caching, :sidek
expect(page).to have_text(chart_params[:title]) expect(page).to have_text(chart_params[:title])
expect(page).to have_text(chart_params[:y_label]) expect(page).to have_text(chart_params[:y_label])
end end
context 'when two dashboard urls are included' do
let(:chart_params_2) do
{
group: 'System metrics (Kubernetes)',
title: 'Core Usage (Total)',
y_label: 'Total Cores'
}
end
let(:metrics_url_2) { urls.metrics_project_environment_url(project, environment, **chart_params_2) }
let(:description) { "See [metrics dashboard](#{metrics_url}) for info. \n See [metrics dashboard](#{metrics_url_2}) for info." }
let(:issue) { create(:issue, project: project, description: description) }
it 'shows embedded metrics for both urls' do
visit project_issue_path(project, issue)
expect(page).to have_css('div.prometheus-graph')
expect(page).to have_text(chart_params[:title])
expect(page).to have_text(chart_params[:y_label])
expect(page).to have_text(chart_params_2[:title])
expect(page).to have_text(chart_params_2[:y_label])
end
end
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