Commit 0007a42a authored by rpereira2's avatar rpereira2 Committed by syasonik

Correct the order of groups and panels

- Order groups by descending order of priority.
- Order panels by descending order of weight.
- Perform sorting after adding project/custom metrics.
parent b1773bf8
......@@ -3,7 +3,7 @@
module Gitlab
module MetricsDashboard
class Processor
STAGES = [CommonMetricsInserter, Sorter, ProjectMetricsInserter].freeze
STAGES = [CommonMetricsInserter, ProjectMetricsInserter, Sorter].freeze
def initialize(dashboard, project)
@dashboard = dashboard.deep_transform_keys(&:to_sym)
......
......@@ -13,13 +13,13 @@ module Gitlab
# Sorts the groups in the dashboard by the :priority key
def sort_groups!(dashboard)
dashboard[:panel_groups] = dashboard[:panel_groups].sort_by { |group| group[:priority] }
dashboard[:panel_groups] = dashboard[:panel_groups].sort_by { |group| group[:priority] }.reverse
end
# Sorts the panels in the dashboard by the :weight key
def sort_panels!(dashboard)
dashboard[:panel_groups].each do |group|
group[:panels] = group[:panels].sort_by { |panel| panel[:weight] }
group[:panels] = group[:panels].sort_by { |panel| panel[:weight] }.reverse
end
end
end
......
......@@ -7,7 +7,7 @@ panel_groups:
- title: "Super Chart A1"
type: "area-chart"
y_label: "y_label"
weight: 2
weight: 1
metrics:
- id: metric_a1
query_range: 'query'
......@@ -16,7 +16,7 @@ panel_groups:
- title: "Super Chart A2"
type: "area-chart"
y_label: "y_label"
weight: 1
weight: 2
metrics:
- id: metric_a2
query_range: 'query'
......
......@@ -18,38 +18,39 @@ describe Gitlab::MetricsDashboard::Processor do
end
context 'when the project has associated metrics' do
let!(:project_metric) { create(:prometheus_metric, project: project) }
let!(:project_response_metric) { create(:prometheus_metric, project: project, group: :response) }
let!(:project_system_metric) { create(:prometheus_metric, project: project, group: :system) }
let!(:project_business_metric) { create(:prometheus_metric, project: project, group: :business) }
it 'includes project-specific metrics' do
project_metric_details = {
query_range: project_metric.query,
unit: project_metric.unit,
label: project_metric.legend,
metric_id: project_metric.id
}
expect(all_metrics).to include project_metric_details
end
it 'includes project metrics at the end of the config' do
expected_metrics_order = ['metric_b', 'metric_a2', 'metric_a1', nil]
actual_metrics_order = all_metrics.map { |m| m[:id] }
expect(actual_metrics_order).to eq expected_metrics_order
end
expect(all_metrics).to include get_metric_details(project_system_metric)
expect(all_metrics).to include get_metric_details(project_response_metric)
expect(all_metrics).to include get_metric_details(project_business_metric)
end
it 'orders groups by priority and panels by weight' do
expected_metrics_order = %w(metric_b metric_a2 metric_a1)
actual_metrics_order = all_metrics.map { |m| m[:id] }
expected_metrics_order = ['metric_a2', 'metric_a1', 'metric_b', project_business_metric.id, project_response_metric.id, project_system_metric.id]
actual_metrics_order = all_metrics.map { |m| m[:id] || m[:metric_id] }
expect(actual_metrics_order).to eq expected_metrics_order
end
end
end
private
def all_metrics
dashboard[:panel_groups].map do |group|
group[:panels].map { |panel| panel[:metrics] }
end.flatten
end
def get_metric_details(metric)
{
query_range: metric.query,
unit: metric.unit,
label: metric.legend,
metric_id: metric.id
}
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