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.
This commit is contained in:
parent
1596e55560
commit
b1773bf8b7
3 changed files with 46 additions and 15 deletions
|
@ -3,7 +3,7 @@
|
||||||
module Gitlab
|
module Gitlab
|
||||||
module MetricsDashboard
|
module MetricsDashboard
|
||||||
class Processor
|
class Processor
|
||||||
STAGES = [CommonMetricsInserter, Sorter, ProjectMetricsInserter]
|
STAGES = [CommonMetricsInserter, Sorter, ProjectMetricsInserter].freeze
|
||||||
|
|
||||||
def initialize(dashboard, project)
|
def initialize(dashboard, project)
|
||||||
@dashboard = dashboard.deep_transform_keys(&:to_sym)
|
@dashboard = dashboard.deep_transform_keys(&:to_sym)
|
||||||
|
|
|
@ -10,25 +10,54 @@ module Gitlab
|
||||||
# If there are no project-specific metrics, this will have no effect.
|
# If there are no project-specific metrics, this will have no effect.
|
||||||
def transform!(dashboard, project)
|
def transform!(dashboard, project)
|
||||||
project.prometheus_metrics.each do |project_metric|
|
project.prometheus_metrics.each do |project_metric|
|
||||||
group = find_or_create(:panel_group, dashboard[:panel_groups], project_metric)
|
group = find_or_create_panel_group(dashboard[:panel_groups], project_metric)
|
||||||
panel = find_or_create(:panel, group[:panels], project_metric)
|
panel = find_or_create_panel(group[:panels], project_metric)
|
||||||
find_or_create(:metric, panel[:metrics], project_metric)
|
find_or_create_metric(panel[:metrics], project_metric)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Looks for an instance of the named resource corresponding to the provided
|
private
|
||||||
# metric object. If unavailable, inserts one.
|
|
||||||
# @param name [Symbol, String] One of :panel_group, :panel, or :metric
|
# Looks for a panel corresponding to the provided metric object.
|
||||||
# @param existing_resources [Array<Hash>]
|
# If unavailable, inserts one.
|
||||||
|
# @param panels [Array<Hash>]
|
||||||
# @param metric [PrometheusMetric]
|
# @param metric [PrometheusMetric]
|
||||||
def find_or_create(name, existing_resources, metric)
|
def find_or_create_panel(panels, metric)
|
||||||
target = self.send("find_#{name}", existing_resources, metric)
|
panel = find_panel(panels, metric)
|
||||||
return target if target
|
return panel if panel
|
||||||
|
|
||||||
target = self.send("new_#{name}", metric)
|
panel = new_panel(metric)
|
||||||
existing_resources << target
|
panels << panel
|
||||||
|
|
||||||
target
|
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_metric(metrics, metric)
|
||||||
|
target_metric = find_metric(metrics, metric)
|
||||||
|
return target_metric if target_metric
|
||||||
|
|
||||||
|
target_metric = new_metric(metric)
|
||||||
|
metrics << target_metric
|
||||||
|
|
||||||
|
target_metric
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_panel_group(panel_groups, metric)
|
def find_panel_group(panel_groups, metric)
|
||||||
|
@ -37,7 +66,7 @@ module Gitlab
|
||||||
|
|
||||||
def find_panel(panels, metric)
|
def find_panel(panels, metric)
|
||||||
panel_identifiers = [DEFAULT_PANEL_TYPE, metric.title, metric.y_label]
|
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
|
end
|
||||||
|
|
||||||
def find_metric(metrics, metric)
|
def find_metric(metrics, metric)
|
||||||
|
|
|
@ -9,6 +9,8 @@ module Gitlab
|
||||||
sort_panels!(dashboard)
|
sort_panels!(dashboard)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
# 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] }
|
||||||
|
|
Loading…
Reference in a new issue