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