Commit 6ca07bf8 authored by syasonik's avatar syasonik

Make EE interactions and transformations cleaner

parent 9b746b4a
...@@ -10,7 +10,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -10,7 +10,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics] before_action :environment, only: [:show, :edit, :update, :stop, :terminal, :terminal_websocket_authorize, :metrics]
before_action :verify_api_request!, only: :terminal_websocket_authorize before_action :verify_api_request!, only: :terminal_websocket_authorize
before_action :expire_etag_cache, only: [:index] 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(:metrics_time_window)
push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint) push_frontend_feature_flag(:environment_metrics_use_prometheus_endpoint)
end end
......
...@@ -2,26 +2,33 @@ ...@@ -2,26 +2,33 @@
module Gitlab module Gitlab
module MetricsDashboard 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 class Processor
def initialize(dashboard, project, environment) def initialize(project, environment)
@dashboard = dashboard.deep_transform_keys(&:to_sym)
@project = project @project = project
@environment = environment @environment = environment
end end
def stages def sequence
@stages ||= [ [
Stages::CommonMetricsInserter, Stages::CommonMetricsInserter,
Stages::ProjectMetricsInserter, Stages::ProjectMetricsInserter,
Stages::Sorter Stages::Sorter
].freeze ]
end end
def process # Returns a new dashboard hash with the results of
stage_params = [@dashboard, @project, @environment] # running transforms on the dashboard.
stages.each { |stage| stage.new(*stage_params).transform! } 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 end
end end
......
...@@ -28,7 +28,7 @@ module Gitlab ...@@ -28,7 +28,7 @@ module Gitlab
end end
def process_dashboard(dashboard) def process_dashboard(dashboard)
Processor.new(dashboard, project, params[:environment]).process Processor.new(project, params[:environment]).process(dashboard)
end end
end end
end end
......
...@@ -6,10 +6,9 @@ module Gitlab ...@@ -6,10 +6,9 @@ module Gitlab
class BaseStage class BaseStage
DEFAULT_PANEL_TYPE = 'area-chart' DEFAULT_PANEL_TYPE = 'area-chart'
attr_reader :dashboard, :project, :environment attr_reader :project, :environment
def initialize(dashboard, project, environment) def initialize(project, environment)
@dashboard = dashboard
@project = project @project = project
@environment = environment @environment = environment
end end
...@@ -18,13 +17,13 @@ module Gitlab ...@@ -18,13 +17,13 @@ module Gitlab
# @param dashboard [Hash] # @param dashboard [Hash]
# @param project [Project] # @param project [Project]
# @param environment [Environment] # @param environment [Environment]
def transform! def transform!(_dashboard)
raise NotImplementedError raise NotImplementedError
end end
protected protected
def for_metrics def for_metrics(dashboard)
dashboard[:panel_groups].each do |panel_group| dashboard[:panel_groups].each do |panel_group|
panel_group[:panels].each do |panel| panel_group[:panels].each do |panel|
panel[:metrics].each do |metric| panel[:metrics].each do |metric|
......
...@@ -7,10 +7,10 @@ module Gitlab ...@@ -7,10 +7,10 @@ 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! def transform!(dashboard)
common_metrics = ::PrometheusMetric.common common_metrics = ::PrometheusMetric.common
for_metrics do |metric| for_metrics(dashboard) do |metric|
metric_record = common_metrics.find { |m| m.identifier == metric[:id] } metric_record = common_metrics.find { |m| m.identifier == metric[:id] }
metric[:metric_id] = metric_record.id if metric_record metric[:metric_id] = metric_record.id if metric_record
end end
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,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! def transform!(dashboard)
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)
......
...@@ -4,20 +4,20 @@ module Gitlab ...@@ -4,20 +4,20 @@ module Gitlab
module MetricsDashboard module MetricsDashboard
module Stages module Stages
class Sorter < BaseStage class Sorter < BaseStage
def transform! def transform!(dashboard)
sort_groups! sort_groups!(dashboard)
sort_panels! sort_panels!(dashboard)
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! def sort_groups!(dashboard)
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! def sort_panels!(dashboard)
dashboard[:panel_groups].each do |group| dashboard[:panel_groups].each do |group|
group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i } group[:panels] = group[:panels].sort_by { |panel| -panel[:weight].to_i }
end end
......
...@@ -8,8 +8,8 @@ describe Gitlab::MetricsDashboard::Processor do ...@@ -8,8 +8,8 @@ describe Gitlab::MetricsDashboard::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) { [dashboard_yml, project, environment] } let(:process_params) { [project, environment] }
let(:dashboard) { described_class.new(*process_params).process } let(:dashboard) { described_class.new(*process_params).process(dashboard_yml) }
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') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment