Move dashboard param to initialize method

This commit is contained in:
syasonik 2019-04-25 14:13:43 +08:00
parent 8926b37d5b
commit 0e093940e1
7 changed files with 19 additions and 20 deletions

View File

@ -14,17 +14,18 @@ module Gitlab
Stages::Sorter Stages::Sorter
].freeze ].freeze
def initialize(project, environment) def initialize(project, environment, dashboard)
@project = project @project = project
@environment = environment @environment = environment
@dashboard = dashboard
end end
# Returns a new dashboard hash with the results of # Returns a new dashboard hash with the results of
# running transforms on the dashboard. # running transforms on the dashboard.
def process(raw_dashboard) def process
raw_dashboard.deep_symbolize_keys.tap do |dashboard| @dashboard.deep_symbolize_keys.tap do |dashboard|
sequence.each do |stage| sequence.each do |stage|
stage.new(@project, @environment).transform!(dashboard) stage.new(@project, @environment, dashboard).transform!
end end
end end
end end

View File

@ -32,7 +32,7 @@ module Gitlab
# Returns a new dashboard Hash, supplemented with DB info # Returns a new dashboard Hash, supplemented with DB info
def process_dashboard(dashboard) def process_dashboard(dashboard)
Gitlab::Metrics::Dashboard::Processor.new(project, params[:environment]).process(dashboard) Gitlab::Metrics::Dashboard::Processor.new(project, params[:environment], dashboard).process
end end
end end
end end

View File

@ -9,18 +9,16 @@ module Gitlab
DEFAULT_PANEL_TYPE = 'area-chart' DEFAULT_PANEL_TYPE = 'area-chart'
attr_reader :project, :environment attr_reader :project, :environment, :dashboard
def initialize(project, environment) def initialize(project, environment, dashboard)
@project = project @project = project
@environment = environment @environment = environment
@dashboard = dashboard
end end
# Entry-point to the stage # Entry-point to the stage
# @param dashboard [Hash] def transform!
# @param project [Project]
# @param environment [Environment]
def transform!(_dashboard)
raise NotImplementedError raise NotImplementedError
end end

View File

@ -8,7 +8,7 @@ module Gitlab
# For each metric in the dashboard config, attempts to # For each metric in the dashboard config, attempts to
# find a corresponding database record. If found, # find a corresponding database record. If found,
# includes the record's id in the dashboard config. # includes the record's id in the dashboard config.
def transform!(dashboard) def transform!
common_metrics = ::PrometheusMetric.common common_metrics = ::PrometheusMetric.common
for_metrics(dashboard) do |metric| for_metrics(dashboard) do |metric|

View File

@ -8,7 +8,7 @@ module Gitlab
# Inserts project-specific metrics into the dashboard # Inserts project-specific metrics into the dashboard
# config. If there are no project-specific metrics, # config. If there are no project-specific metrics,
# this will have no effect. # this will have no effect.
def transform!(dashboard) def transform!
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)

View File

@ -5,22 +5,22 @@ module Gitlab
module Dashboard module Dashboard
module Stages module Stages
class Sorter < BaseStage class Sorter < BaseStage
def transform!(dashboard) def transform!
missing_panel_groups! unless dashboard[:panel_groups].is_a? Array missing_panel_groups! unless dashboard[:panel_groups].is_a? Array
sort_groups!(dashboard) sort_groups!
sort_panels!(dashboard) sort_panels!
end end
private 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[:panel_groups] = dashboard[:panel_groups].sort_by { |group| -group[:priority].to_i } 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[:panel_groups].each do |group| dashboard[:panel_groups].each do |group|
missing_panels! unless group[:panels].is_a? Array missing_panels! unless group[:panels].is_a? Array

View File

@ -8,8 +8,8 @@ describe Gitlab::Metrics::Dashboard::Processor do
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics/dashboard/sample_dashboard.yml') } let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
describe 'process' do describe 'process' do
let(:process_params) { [project, environment] } let(:process_params) { [project, environment, dashboard_yml] }
let(:dashboard) { described_class.new(*process_params).process(dashboard_yml) } let(:dashboard) { described_class.new(*process_params).process }
context 'when dashboard config corresponds to common metrics' do context 'when dashboard config corresponds to common metrics' do
let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') } let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }