From b209fb6fb21369b381e4a538f2e46ef1b6579aed Mon Sep 17 00:00:00 2001 From: syasonik Date: Wed, 17 Apr 2019 14:47:50 +0800 Subject: [PATCH] Minor optimization and code clarity --- .../project_metrics_inserter.rb | 28 +++++++++---------- lib/gitlab/metrics_dashboard/sorter.rb | 4 +-- .../metrics_dashboard/processor_spec.rb | 9 +++++- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb b/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb index 3903bd39912..f4463645d2c 100644 --- a/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb +++ b/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb @@ -18,20 +18,6 @@ module Gitlab private - # Looks for a panel corresponding to the provided metric object. - # If unavailable, inserts one. - # @param panels [Array] - # @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. # If unavailable, inserts one. # @param panel_groups [Array] @@ -46,6 +32,20 @@ module Gitlab panel_group end + # Looks for a panel corresponding to the provided metric object. + # If unavailable, inserts one. + # @param panels [Array] + # @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. # If unavailable, inserts one. # @param metrics [Array] diff --git a/lib/gitlab/metrics_dashboard/sorter.rb b/lib/gitlab/metrics_dashboard/sorter.rb index 9a8f87fcb6e..59b21d207e1 100644 --- a/lib/gitlab/metrics_dashboard/sorter.rb +++ b/lib/gitlab/metrics_dashboard/sorter.rb @@ -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] }.reverse + dashboard[:panel_groups] = dashboard[:panel_groups].sort_by { |group| -group[:priority].to_i } 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] }.reverse + group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i } end end end diff --git a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb index bd6af18cbc0..9cf5b00e915 100644 --- a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb +++ b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb @@ -31,7 +31,14 @@ describe Gitlab::MetricsDashboard::Processor do end 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] } expect(actual_metrics_order).to eq expected_metrics_order