Commit 67e2ce26 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'sy-fix-multi-metric-embed' into 'master'

Show multimetric embeds on a single chart

See merge request gitlab-org/gitlab!28841
parents a40aac30 e17529b4
......@@ -57,7 +57,7 @@ module Metrics
# @return [Hash]
override :raw_dashboard
def raw_dashboard
panels_not_found!(identifiers) if panels.empty?
panels_not_found!(identifiers) if metrics.empty?
{ 'panel_groups' => [{ 'panels' => panels }] }
end
......@@ -66,11 +66,20 @@ module Metrics
# Generated dashboard panels for each metric which
# matches the provided input.
#
# As the panel is generated
# on the fly, we're using default values for info
# not represented in the DB.
#
# @return [Array<Hash>]
def panels
strong_memoize(:panels) do
metrics.map { |metric| panel_for_metric(metric) }
end
[{
type: DEFAULT_PANEL_TYPE,
weight: DEFAULT_PANEL_WEIGHT,
title: title,
y_label: y_label,
metrics: metrics.map(&:to_metric_hash)
}]
end
# Metrics which match the provided inputs.
......@@ -78,6 +87,7 @@ module Metrics
# displayed in a single panel/chart.
# @return [ActiveRecord::AssociationRelation<PromtheusMetric>]
def metrics
strong_memoize(:metrics) do
PrometheusMetricsFinder.new(
project: project,
group: group_key,
......@@ -85,6 +95,7 @@ module Metrics
y_label: y_label
).execute
end
end
# Returns a symbol representing the group that
# the dashboard's group title belongs to.
......@@ -101,22 +112,6 @@ module Metrics
.to_s
end
end
# Returns a representation of a PromtheusMetric
# as a dashboard panel. As the panel is generated
# on the fly, we're using default values for info
# not represented in the DB.
#
# @return [Hash]
def panel_for_metric(metric)
{
type: DEFAULT_PANEL_TYPE,
weight: DEFAULT_PANEL_WEIGHT,
title: metric.title,
y_label: metric.y_label,
metrics: [metric.to_metric_hash]
}
end
end
end
end
---
title: Show multimetric embeds on a single chart
merge_request: 28841
author:
type: fixed
......@@ -121,11 +121,18 @@ describe Metrics::Dashboard::CustomMetricEmbedService do
it_behaves_like 'valid embedded dashboard service response'
it 'includes both metrics' do
it 'includes both metrics in a single panel' do
result = service_call
included_queries = all_queries(result[:dashboard])
expect(included_queries).to include('avg(metric_2)', 'avg(metric)')
panel_groups = result[:dashboard][:panel_groups]
panels = panel_groups[0][:panels]
metrics = panels[0][:metrics]
queries = metrics.map { |metric| metric[:query_range] }
expect(panel_groups.length).to eq(1)
expect(panels.length).to eq(1)
expect(metrics.length).to eq(2)
expect(queries).to include('avg(metric_2)', 'avg(metric)')
end
end
end
......@@ -136,16 +143,4 @@ describe Metrics::Dashboard::CustomMetricEmbedService do
it_behaves_like 'misconfigured dashboard service response', :not_found
end
end
private
def all_queries(dashboard)
dashboard[:panel_groups].flat_map do |group|
group[:panels].flat_map do |panel|
panel[:metrics].map do |metric|
metric[:query_range]
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