summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrpereira2 <rpereira@gitlab.com>2019-04-16 16:13:08 +0530
committersyasonik <syasonik@gitlab.com>2019-04-24 18:23:03 +0800
commitb1773bf8b741ffc52e2699848e42aa0a054c9e6e (patch)
treea9307380d8a494ec9982688d287d665a2d334225
parent1596e5556030ab3990a7d0117bf7d4306417c052 (diff)
downloadgitlab-ce-b1773bf8b741ffc52e2699848e42aa0a054c9e6e.tar.gz
Fix rubocop failures
- Add 3 functions called find_or_create_panel, find_or_create_panel_group, and find_or_create_metric to avoid having to use 'send'. - Remove an unused variable. - Freeze a constant array.
-rw-r--r--lib/gitlab/metrics_dashboard/processor.rb2
-rw-r--r--lib/gitlab/metrics_dashboard/project_metrics_inserter.rb57
-rw-r--r--lib/gitlab/metrics_dashboard/sorter.rb2
3 files changed, 46 insertions, 15 deletions
diff --git a/lib/gitlab/metrics_dashboard/processor.rb b/lib/gitlab/metrics_dashboard/processor.rb
index 9e254aa2b37..cfeb0ddb468 100644
--- a/lib/gitlab/metrics_dashboard/processor.rb
+++ b/lib/gitlab/metrics_dashboard/processor.rb
@@ -3,7 +3,7 @@
module Gitlab
module MetricsDashboard
class Processor
- STAGES = [CommonMetricsInserter, Sorter, ProjectMetricsInserter]
+ STAGES = [CommonMetricsInserter, Sorter, ProjectMetricsInserter].freeze
def initialize(dashboard, project)
@dashboard = dashboard.deep_transform_keys(&:to_sym)
diff --git a/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb b/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb
index 67300c79e57..3903bd39912 100644
--- a/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb
+++ b/lib/gitlab/metrics_dashboard/project_metrics_inserter.rb
@@ -10,25 +10,54 @@ module Gitlab
# If there are no project-specific metrics, this will have no effect.
def transform!(dashboard, project)
project.prometheus_metrics.each do |project_metric|
- group = find_or_create(:panel_group, dashboard[:panel_groups], project_metric)
- panel = find_or_create(:panel, group[:panels], project_metric)
- find_or_create(:metric, panel[:metrics], project_metric)
+ group = find_or_create_panel_group(dashboard[:panel_groups], project_metric)
+ panel = find_or_create_panel(group[:panels], project_metric)
+ find_or_create_metric(panel[:metrics], project_metric)
end
end
- # Looks for an instance of the named resource corresponding to the provided
- # metric object. If unavailable, inserts one.
- # @param name [Symbol, String] One of :panel_group, :panel, or :metric
- # @param existing_resources [Array<Hash>]
+ 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.
+ # If unavailable, inserts one.
+ # @param panel_groups [Array<Hash>]
+ # @param metric [PrometheusMetric]
+ def find_or_create_panel_group(panel_groups, metric)
+ panel_group = find_panel_group(panel_groups, metric)
+ return panel_group if panel_group
+
+ panel_group = new_panel_group(metric)
+ panel_groups << panel_group
+
+ panel_group
+ end
+
+ # Looks for a metric corresponding to the provided metric object.
+ # If unavailable, inserts one.
+ # @param metrics [Array<Hash>]
# @param metric [PrometheusMetric]
- def find_or_create(name, existing_resources, metric)
- target = self.send("find_#{name}", existing_resources, metric)
- return target if target
+ def find_or_create_metric(metrics, metric)
+ target_metric = find_metric(metrics, metric)
+ return target_metric if target_metric
- target = self.send("new_#{name}", metric)
- existing_resources << target
+ target_metric = new_metric(metric)
+ metrics << target_metric
- target
+ target_metric
end
def find_panel_group(panel_groups, metric)
@@ -37,7 +66,7 @@ module Gitlab
def find_panel(panels, metric)
panel_identifiers = [DEFAULT_PANEL_TYPE, metric.title, metric.y_label]
- target_panel = panels.find { |panel| panel.values_at(:type, :title, :y_label) == panel_identifiers }
+ panels.find { |panel| panel.values_at(:type, :title, :y_label) == panel_identifiers }
end
def find_metric(metrics, metric)
diff --git a/lib/gitlab/metrics_dashboard/sorter.rb b/lib/gitlab/metrics_dashboard/sorter.rb
index ba30c1e4656..1d28fc8bd3a 100644
--- a/lib/gitlab/metrics_dashboard/sorter.rb
+++ b/lib/gitlab/metrics_dashboard/sorter.rb
@@ -9,6 +9,8 @@ module Gitlab
sort_panels!(dashboard)
end
+ private
+
# 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] }