Commit c1c0fb66 authored by syasonik's avatar syasonik

Make EE interactions and transformations cleaner

parent 655fae42
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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|
......
......@@ -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
......
......@@ -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)
......
......@@ -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
......
......@@ -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') }
......
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