summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsyasonik <syasonik@gitlab.com>2019-04-19 18:12:54 +0800
committersyasonik <syasonik@gitlab.com>2019-04-24 18:23:04 +0800
commitc1c0fb66937dcea326cb70528373ce6ab822d25a (patch)
tree91551fbad54ead6acddf768d12c84cd4f75f3171
parent655fae4242e88d71756578970d19c3db42ed63e1 (diff)
downloadgitlab-ce-c1c0fb66937dcea326cb70528373ce6ab822d25a.tar.gz
Make EE interactions and transformations cleaner
-rw-r--r--app/controllers/projects/environments_controller.rb2
-rw-r--r--lib/gitlab/metrics_dashboard/processor.rb25
-rw-r--r--lib/gitlab/metrics_dashboard/service.rb2
-rw-r--r--lib/gitlab/metrics_dashboard/stages/base_stage.rb9
-rw-r--r--lib/gitlab/metrics_dashboard/stages/common_metrics_inserter.rb4
-rw-r--r--lib/gitlab/metrics_dashboard/stages/project_metrics_inserter.rb2
-rw-r--r--lib/gitlab/metrics_dashboard/stages/sorter.rb10
-rw-r--r--spec/lib/gitlab/metrics_dashboard/processor_spec.rb4
8 files changed, 32 insertions, 26 deletions
diff --git a/app/controllers/projects/environments_controller.rb b/app/controllers/projects/environments_controller.rb
index 3f345204241..e285011469c 100644
--- a/app/controllers/projects/environments_controller.rb
+++ b/app/controllers/projects/environments_controller.rb
@@ -10,7 +10,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
before_action :verify_api_request!, only: :terminal_websocket_authorize
before_action :expire_etag_cache, only: [:index]
- before_action only: [:metrics, :additional_metrics] do
+ before_action only: [:metrics, :additional_metrics, :metrics_dashboard] do
push_frontend_feature_flag(:metrics_time_window)
push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint)
end
diff --git a/lib/gitlab/metrics_dashboard/processor.rb b/lib/gitlab/metrics_dashboard/processor.rb
index 3b8ab27a07b..ef9d75947f0 100644
--- a/lib/gitlab/metrics_dashboard/processor.rb
+++ b/lib/gitlab/metrics_dashboard/processor.rb
@@ -2,26 +2,33 @@
module Gitlab
module MetricsDashboard
+ # Responsible for processesing a dashboard hash, inserting
+ # relevantDB records & sorting for proper rendering in
+ # the UI. These includes shared metric info, custom metrics
+ # info, and alerts (only in EE).
class Processor
- def initialize(dashboard, project, environment)
- @dashboard = dashboard.deep_transform_keys(&:to_sym)
+ def initialize(project, environment)
@project = project
@environment = environment
end
- def stages
- @stages ||= [
+ def sequence
+ [
Stages::CommonMetricsInserter,
Stages::ProjectMetricsInserter,
Stages::Sorter
- ].freeze
+ ]
end
- def process
- stage_params = [@dashboard, @project, @environment]
- stages.each { |stage| stage.new(*stage_params).transform! }
+ # Returns a new dashboard hash with the results of
+ # running transforms on the dashboard.
+ def process(dashboard)
+ dashboard = dashboard.deep_transform_keys(&:to_sym)
- @dashboard
+ stage_params = [@project, @environment]
+ sequence.each { |stage| stage.new(*stage_params).transform!(dashboard) }
+
+ dashboard
end
end
end
diff --git a/lib/gitlab/metrics_dashboard/service.rb b/lib/gitlab/metrics_dashboard/service.rb
index 01e61b257e2..a65f01ca54e 100644
--- a/lib/gitlab/metrics_dashboard/service.rb
+++ b/lib/gitlab/metrics_dashboard/service.rb
@@ -28,7 +28,7 @@ module Gitlab
end
def process_dashboard(dashboard)
- Processor.new(dashboard, project, params[:environment]).process
+ Processor.new(project, params[:environment]).process(dashboard)
end
end
end
diff --git a/lib/gitlab/metrics_dashboard/stages/base_stage.rb b/lib/gitlab/metrics_dashboard/stages/base_stage.rb
index 72085e0c09e..bdbf0c196cc 100644
--- a/lib/gitlab/metrics_dashboard/stages/base_stage.rb
+++ b/lib/gitlab/metrics_dashboard/stages/base_stage.rb
@@ -6,10 +6,9 @@ module Gitlab
class BaseStage
DEFAULT_PANEL_TYPE = 'area-chart'
- attr_reader :dashboard, :project, :environment
+ attr_reader :project, :environment
- def initialize(dashboard, project, environment)
- @dashboard = dashboard
+ def initialize(project, environment)
@project = project
@environment = environment
end
@@ -18,13 +17,13 @@ module Gitlab
# @param dashboard [Hash]
# @param project [Project]
# @param environment [Environment]
- def transform!
+ def transform!(_dashboard)
raise NotImplementedError
end
protected
- def for_metrics
+ def for_metrics(dashboard)
dashboard[:panel_groups].each do |panel_group|
panel_group[:panels].each do |panel|
panel[:metrics].each do |metric|
diff --git a/lib/gitlab/metrics_dashboard/stages/common_metrics_inserter.rb b/lib/gitlab/metrics_dashboard/stages/common_metrics_inserter.rb
index e85bdb2700b..ef70347c6b2 100644
--- a/lib/gitlab/metrics_dashboard/stages/common_metrics_inserter.rb
+++ b/lib/gitlab/metrics_dashboard/stages/common_metrics_inserter.rb
@@ -7,10 +7,10 @@ module Gitlab
# For each metric in the dashboard config, attempts to
# find a corresponding database record. If found,
# includes the record's id in the dashboard config.
- def transform!
+ def transform!(dashboard)
common_metrics = ::PrometheusMetric.common
- for_metrics do |metric|
+ for_metrics(dashboard) do |metric|
metric_record = common_metrics.find { |m| m.identifier == metric[:id] }
metric[:metric_id] = metric_record.id if metric_record
end
diff --git a/lib/gitlab/metrics_dashboard/stages/project_metrics_inserter.rb b/lib/gitlab/metrics_dashboard/stages/project_metrics_inserter.rb
index af59e6f5910..8edb21c89c1 100644
--- a/lib/gitlab/metrics_dashboard/stages/project_metrics_inserter.rb
+++ b/lib/gitlab/metrics_dashboard/stages/project_metrics_inserter.rb
@@ -7,7 +7,7 @@ module Gitlab
# Inserts project-specific metrics into the dashboard
# config. If there are no project-specific metrics,
# this will have no effect.
- def transform!
+ def transform!(dashboard)
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)
diff --git a/lib/gitlab/metrics_dashboard/stages/sorter.rb b/lib/gitlab/metrics_dashboard/stages/sorter.rb
index 74b596038fe..a2429e65efa 100644
--- a/lib/gitlab/metrics_dashboard/stages/sorter.rb
+++ b/lib/gitlab/metrics_dashboard/stages/sorter.rb
@@ -4,20 +4,20 @@ module Gitlab
module MetricsDashboard
module Stages
class Sorter < BaseStage
- def transform!
- sort_groups!
- sort_panels!
+ def transform!(dashboard)
+ sort_groups!(dashboard)
+ sort_panels!(dashboard)
end
private
# Sorts the groups in the dashboard by the :priority key
- def sort_groups!
+ def sort_groups!(dashboard)
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!
+ def sort_panels!(dashboard)
dashboard[:panel_groups].each do |group|
group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i }
end
diff --git a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb
index bc5f6527ad7..1bd905989fe 100644
--- a/spec/lib/gitlab/metrics_dashboard/processor_spec.rb
+++ b/spec/lib/gitlab/metrics_dashboard/processor_spec.rb
@@ -8,8 +8,8 @@ describe Gitlab::MetricsDashboard::Processor do
let(:dashboard_yml) { YAML.load_file('spec/fixtures/lib/gitlab/metrics_dashboard/sample_dashboard.yml') }
describe 'process' do
- let(:process_params) { [dashboard_yml, project, environment] }
- let(:dashboard) { described_class.new(*process_params).process }
+ let(:process_params) { [project, environment] }
+ let(:dashboard) { described_class.new(*process_params).process(dashboard_yml) }
context 'when dashboard config corresponds to common metrics' do
let!(:common_metric) { create(:prometheus_metric, :common, identifier: 'metric_a1') }