Minor optimization and code clarity
This commit is contained in:
parent
64a1fab788
commit
b209fb6fb2
3 changed files with 24 additions and 17 deletions
|
@ -18,20 +18,6 @@ module Gitlab
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Looks for a panel corresponding to the provided metric object.
|
|
||||||
# If unavailable, inserts one.
|
|
||||||
# @param panels [Array<Hash>]
|
|
||||||
# @param metric [PrometheusMetric]
|
|
||||||
def find_or_create_panel(panels, metric)
|
|
||||||
panel = find_panel(panels, metric)
|
|
||||||
return panel if panel
|
|
||||||
|
|
||||||
panel = new_panel(metric)
|
|
||||||
panels << panel
|
|
||||||
|
|
||||||
panel
|
|
||||||
end
|
|
||||||
|
|
||||||
# Looks for a panel_group corresponding to the provided metric object.
|
# Looks for a panel_group corresponding to the provided metric object.
|
||||||
# If unavailable, inserts one.
|
# If unavailable, inserts one.
|
||||||
# @param panel_groups [Array<Hash>]
|
# @param panel_groups [Array<Hash>]
|
||||||
|
@ -46,6 +32,20 @@ module Gitlab
|
||||||
panel_group
|
panel_group
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Looks for a panel corresponding to the provided metric object.
|
||||||
|
# If unavailable, inserts one.
|
||||||
|
# @param panels [Array<Hash>]
|
||||||
|
# @param metric [PrometheusMetric]
|
||||||
|
def find_or_create_panel(panels, metric)
|
||||||
|
panel = find_panel(panels, metric)
|
||||||
|
return panel if panel
|
||||||
|
|
||||||
|
panel = new_panel(metric)
|
||||||
|
panels << panel
|
||||||
|
|
||||||
|
panel
|
||||||
|
end
|
||||||
|
|
||||||
# Looks for a metric corresponding to the provided metric object.
|
# Looks for a metric corresponding to the provided metric object.
|
||||||
# If unavailable, inserts one.
|
# If unavailable, inserts one.
|
||||||
# @param metrics [Array<Hash>]
|
# @param metrics [Array<Hash>]
|
||||||
|
|
|
@ -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] }.reverse
|
dashboard[:panel_groups] = dashboard[:panel_groups].sort_by { |group| -group[:priority].to_i }
|
||||||
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] }.reverse
|
group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,7 +31,14 @@ describe Gitlab::MetricsDashboard::Processor do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'orders groups by priority and panels by weight' do
|
it 'orders groups by priority and panels by weight' do
|
||||||
expected_metrics_order = ['metric_a2', 'metric_a1', 'metric_b', project_business_metric.id, project_response_metric.id, project_system_metric.id]
|
expected_metrics_order = [
|
||||||
|
'metric_a2', # group priority 10, panel weight 2
|
||||||
|
'metric_a1', # group priority 10, panel weight 1
|
||||||
|
'metric_b', # group priority 1, panel weight 1
|
||||||
|
project_business_metric.id, # group priority 0, panel weight nil (0)
|
||||||
|
project_response_metric.id, # group priority -5, panel weight nil (0)
|
||||||
|
project_system_metric.id, # group priority -10, panel weight nil (0)
|
||||||
|
]
|
||||||
actual_metrics_order = all_metrics.map { |m| m[:id] || m[:metric_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
|
||||||
|
|
Loading…
Reference in a new issue