Commit be5416e0 authored by Reuben Pereira's avatar Reuben Pereira Committed by Rémy Coutable

Refactor predefined dashboards

Move code for predefined dashboards into a common class that can be
inherited from.
parent 447c2db5
......@@ -13,7 +13,7 @@ module Metrics
def dashboard_path
params[:dashboard_path].presence ||
::Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH
::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH
end
def group
......
......@@ -40,7 +40,7 @@ module Metrics
# All custom metrics are displayed on the system dashboard.
# Nil is acceptable as we'll default to the system dashboard.
def valid_dashboard?(dashboard)
dashboard.nil? || ::Metrics::Dashboard::SystemDashboardService.system_dashboard?(dashboard)
dashboard.nil? || ::Metrics::Dashboard::SystemDashboardService.matching_dashboard?(dashboard)
end
end
......
# frozen_string_literal: true
module Metrics
module Dashboard
class PredefinedDashboardService < ::Metrics::Dashboard::BaseService
# These constants should be overridden in the inheriting class. For Ex:
# DASHBOARD_PATH = 'config/prometheus/common_metrics.yml'
# DASHBOARD_NAME = 'Default'
DASHBOARD_PATH = nil
DASHBOARD_NAME = nil
SEQUENCE = [
STAGES::EndpointInserter,
STAGES::Sorter
].freeze
class << self
def matching_dashboard?(filepath)
filepath == self::DASHBOARD_PATH
end
end
private
def cache_key
"metrics_dashboard_#{dashboard_path}"
end
def dashboard_path
self.class::DASHBOARD_PATH
end
# Returns the base metrics shipped with every GitLab service.
def get_raw_dashboard
yml = File.read(Rails.root.join(dashboard_path))
YAML.safe_load(yml)
end
def sequence
self.class::SEQUENCE
end
end
end
end
# frozen_string_literal: true
# Fetches the system metrics dashboard and formats the output.
# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
# Use Gitlab::Metrics::Dashboard::Finder to retrieve dashboards.
module Metrics
module Dashboard
class SystemDashboardService < ::Metrics::Dashboard::BaseService
SYSTEM_DASHBOARD_PATH = 'config/prometheus/common_metrics.yml'
SYSTEM_DASHBOARD_NAME = 'Default'
class SystemDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
DASHBOARD_PATH = 'config/prometheus/common_metrics.yml'
DASHBOARD_NAME = 'Default'
SEQUENCE = [
STAGES::CommonMetricsInserter,
......@@ -18,37 +18,12 @@ module Metrics
class << self
def all_dashboard_paths(_project)
[{
path: SYSTEM_DASHBOARD_PATH,
display_name: SYSTEM_DASHBOARD_NAME,
path: DASHBOARD_PATH,
display_name: DASHBOARD_NAME,
default: true,
system_dashboard: true
}]
end
def system_dashboard?(filepath)
filepath == SYSTEM_DASHBOARD_PATH
end
end
private
def cache_key
"metrics_dashboard_#{dashboard_path}"
end
def dashboard_path
SYSTEM_DASHBOARD_PATH
end
# Returns the base metrics shipped with every GitLab service.
def get_raw_dashboard
yml = File.read(Rails.root.join(dashboard_path))
YAML.safe_load(yml)
end
def sequence
SEQUENCE
end
end
end
......
......@@ -4,36 +4,15 @@
# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
module Metrics
module Dashboard
class ClusterDashboardService < ::Metrics::Dashboard::BaseService
CLUSTER_DASHBOARD_PATH = 'ee/config/prometheus/cluster_metrics.yml'
CLUSTER_DASHBOARD_NAME = 'Cluster'
class ClusterDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
DASHBOARD_PATH = 'ee/config/prometheus/cluster_metrics.yml'
DASHBOARD_NAME = 'Cluster'
SEQUENCE = [
STAGES::CommonMetricsInserter,
STAGES::ClusterEndpointInserter,
STAGES::Sorter
].freeze
private
def cache_key
"metrics_dashboard_#{dashboard_path}"
end
def dashboard_path
CLUSTER_DASHBOARD_PATH
end
# Returns the base metrics shipped with every GitLab service.
def get_raw_dashboard
yml = File.read(Rails.root.join(dashboard_path))
YAML.safe_load(yml)
end
def sequence
SEQUENCE
end
end
end
end
......@@ -19,13 +19,7 @@ describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_memory_st
let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid dashboard service response'
it 'caches the unprocessed dashboard for subsequent calls' do
expect(YAML).to receive(:safe_load).once.and_call_original
described_class.new(*service_params).get_dashboard
described_class.new(*service_params).get_dashboard
end
it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' }
......
......@@ -34,7 +34,7 @@ module Gitlab
end
def system_dashboard?(filepath)
SERVICES::SystemDashboardService.system_dashboard?(filepath)
SERVICES::SystemDashboardService.matching_dashboard?(filepath)
end
def custom_metric_embed?(params)
......
......@@ -14,24 +14,18 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
end
describe 'get_dashboard' do
let(:dashboard_path) { described_class::SYSTEM_DASHBOARD_PATH }
let(:dashboard_path) { described_class::DASHBOARD_PATH }
let(:service_params) { [project, user, { environment: environment, dashboard_path: dashboard_path }] }
let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions'
it 'caches the unprocessed dashboard for subsequent calls' do
expect(YAML).to receive(:safe_load).once.and_call_original
described_class.new(*service_params).get_dashboard
described_class.new(*service_params).get_dashboard
end
it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' }
# We want to alwaus return the system dashboard.
# We want to always return the system dashboard.
it_behaves_like 'valid dashboard service response'
end
end
......@@ -42,8 +36,8 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
expect(all_dashboards).to eq(
[{
path: described_class::SYSTEM_DASHBOARD_PATH,
display_name: described_class::SYSTEM_DASHBOARD_NAME,
path: described_class::DASHBOARD_PATH,
display_name: described_class::DASHBOARD_NAME,
default: true,
system_dashboard: true
}]
......
......@@ -19,7 +19,7 @@ module MetricsDashboardHelpers
end
def system_dashboard_path
Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH
Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH
end
def business_metric_title
......@@ -53,6 +53,15 @@ module MetricsDashboardHelpers
it_behaves_like 'valid dashboard service response for schema'
end
shared_examples_for 'caches the unprocessed dashboard for subsequent calls' do
it do
expect(YAML).to receive(:safe_load).once.and_call_original
described_class.new(*service_params).get_dashboard
described_class.new(*service_params).get_dashboard
end
end
shared_examples_for 'valid embedded dashboard service response' do
let(:dashboard_schema) { JSON.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) }
......
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