diff options
author | syasonik <syasonik@gitlab.com> | 2019-07-30 20:04:23 +0300 |
---|---|---|
committer | syasonik <syasonik@gitlab.com> | 2019-07-31 13:07:14 +0300 |
commit | 982b8a7041be5efd2905c81e9c9a07cfe26bfc8b (patch) | |
tree | 2c5c8f03e42c41731d7893adbc0ab2f95258dccd | |
parent | d60cdcf3f523aa787c9886af5f57499654c8a3fc (diff) | |
download | gitlab-ce-982b8a7041be5efd2905c81e9c9a07cfe26bfc8b.tar.gz |
Support query parameters in metrics embeds
https://gitlab.com/gitlab-org/gitlab-ce/issues/62971
Adds support for embedding specific charts from the
metrics dashboard. Expected parameters are dashboard,
title, group, and y_label.
-rw-r--r-- | changelogs/unreleased/enable-specific-embeds.yml | 5 | ||||
-rw-r--r-- | lib/banzai/filter/inline_metrics_filter.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/metrics/dashboard/url.rb | 4 | ||||
-rw-r--r-- | spec/lib/banzai/filter/inline_metrics_filter_spec.rb | 25 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/dashboard/url_spec.rb | 11 |
5 files changed, 57 insertions, 5 deletions
diff --git a/changelogs/unreleased/enable-specific-embeds.yml b/changelogs/unreleased/enable-specific-embeds.yml new file mode 100644 index 00000000000..f2e591621a8 --- /dev/null +++ b/changelogs/unreleased/enable-specific-embeds.yml @@ -0,0 +1,5 @@ +--- +title: Enable embedding of specific metrics charts in GFM +merge_request: 31304 +author: +type: added diff --git a/lib/banzai/filter/inline_metrics_filter.rb b/lib/banzai/filter/inline_metrics_filter.rb index 0120cc37d6f..29f526d3421 100644 --- a/lib/banzai/filter/inline_metrics_filter.rb +++ b/lib/banzai/filter/inline_metrics_filter.rb @@ -22,10 +22,25 @@ module Banzai params['namespace'], params['project'], params['environment'], - embedded: true + embedded: true, + **query_params(params) ) end + # Parses query params out from full string into hash. + # If multiple values are given for a parameter, they + # will be captured in an array. + # Ex) '?title=Title&group=Group' --> { title: 'Title', group: Group } + def query_params(params) + return {} unless params['query'] + + CGI.parse(params['query'][1..-1]).map do |key, value| + target = value.length == 1 ? value.first : value + + [key.to_sym, target] + end.to_h + end + # Search params for selecting metrics links. A few # simple checks is enough to boost performance without # the cost of doing a full regex match. diff --git a/lib/gitlab/metrics/dashboard/url.rb b/lib/gitlab/metrics/dashboard/url.rb index b197e7ca86b..83d0a1cdf49 100644 --- a/lib/gitlab/metrics/dashboard/url.rb +++ b/lib/gitlab/metrics/dashboard/url.rb @@ -21,8 +21,8 @@ module Gitlab \/(?<environment>\d+) \/metrics (?<query> - \?[a-z0-9_=-]+ - (&[a-z0-9_=-]+)* + \?[%.+a-zA-Z0-9_=-]+ + (&[%.+a-zA-Z0-9_=-]+)* )? (?<anchor>\#[a-z0-9_-]+)? ) diff --git a/spec/lib/banzai/filter/inline_metrics_filter_spec.rb b/spec/lib/banzai/filter/inline_metrics_filter_spec.rb index 772c94e3180..1dfec8118ff 100644 --- a/spec/lib/banzai/filter/inline_metrics_filter_spec.rb +++ b/spec/lib/banzai/filter/inline_metrics_filter_spec.rb @@ -51,5 +51,30 @@ describe Banzai::Filter::InlineMetricsFilter do end end end + + context 'with dashboard params specified' do + let(:params) do + [ + 'foo', + 'bar', + 12, + { + embedded: true, + dashboard: 'config%2Fprometheus%2Fcommon_metrics.yml', + group: 'System+metrics+%28Kubernetes%29', + title: 'Core+Usage+%28Pod+Average%29', + y_label: 'Cores+per+Pod' + } + ] + end + + it 'appends a metrics charts placeholder with dashboard url after metrics links' do + node = doc.at_css('.js-render-metrics') + expect(node).to be_present + + dashboard_url = urls.metrics_dashboard_namespace_project_environment_url(*params) + expect(node.attribute('data-dashboard-url').to_s).to eq dashboard_url + end + end end end diff --git a/spec/lib/gitlab/metrics/dashboard/url_spec.rb b/spec/lib/gitlab/metrics/dashboard/url_spec.rb index 34bc6359414..edd11bf8023 100644 --- a/spec/lib/gitlab/metrics/dashboard/url_spec.rb +++ b/spec/lib/gitlab/metrics/dashboard/url_spec.rb @@ -9,14 +9,21 @@ describe Gitlab::Metrics::Dashboard::Url do end it 'matches a metrics dashboard link with named params' do - url = Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url('foo', 'bar', 1, start: 123345456, anchor: 'title') + url = Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url( + 'foo', + 'bar', + 1, + start: 123345456, + dashboard: 'config%2Fprometheus%2Fcommon_metrics.yml', + anchor: 'title' + ) expected_params = { 'url' => url, 'namespace' => 'foo', 'project' => 'bar', 'environment' => '1', - 'query' => '?start=123345456', + 'query' => '?dashboard=config%252Fprometheus%252Fcommon_metrics.yml&start=123345456', 'anchor' => '#title' } |