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 ...@@ -13,7 +13,7 @@ module Metrics
def dashboard_path def dashboard_path
params[:dashboard_path].presence || params[:dashboard_path].presence ||
::Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH ::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH
end end
def group def group
......
...@@ -40,7 +40,7 @@ module Metrics ...@@ -40,7 +40,7 @@ module Metrics
# All custom metrics are displayed on the system dashboard. # All custom metrics are displayed on the system dashboard.
# Nil is acceptable as we'll default to the system dashboard. # Nil is acceptable as we'll default to the system dashboard.
def valid_dashboard?(dashboard) def valid_dashboard?(dashboard)
dashboard.nil? || ::Metrics::Dashboard::SystemDashboardService.system_dashboard?(dashboard) dashboard.nil? || ::Metrics::Dashboard::SystemDashboardService.matching_dashboard?(dashboard)
end end
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 # frozen_string_literal: true
# Fetches the system metrics dashboard and formats the output. # 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 Metrics
module Dashboard module Dashboard
class SystemDashboardService < ::Metrics::Dashboard::BaseService class SystemDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
SYSTEM_DASHBOARD_PATH = 'config/prometheus/common_metrics.yml' DASHBOARD_PATH = 'config/prometheus/common_metrics.yml'
SYSTEM_DASHBOARD_NAME = 'Default' DASHBOARD_NAME = 'Default'
SEQUENCE = [ SEQUENCE = [
STAGES::CommonMetricsInserter, STAGES::CommonMetricsInserter,
...@@ -18,37 +18,12 @@ module Metrics ...@@ -18,37 +18,12 @@ module Metrics
class << self class << self
def all_dashboard_paths(_project) def all_dashboard_paths(_project)
[{ [{
path: SYSTEM_DASHBOARD_PATH, path: DASHBOARD_PATH,
display_name: SYSTEM_DASHBOARD_NAME, display_name: DASHBOARD_NAME,
default: true, default: true,
system_dashboard: true system_dashboard: true
}] }]
end 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 end
end end
......
...@@ -4,36 +4,15 @@ ...@@ -4,36 +4,15 @@
# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards. # Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
module Metrics module Metrics
module Dashboard module Dashboard
class ClusterDashboardService < ::Metrics::Dashboard::BaseService class ClusterDashboardService < ::Metrics::Dashboard::PredefinedDashboardService
CLUSTER_DASHBOARD_PATH = 'ee/config/prometheus/cluster_metrics.yml' DASHBOARD_PATH = 'ee/config/prometheus/cluster_metrics.yml'
CLUSTER_DASHBOARD_NAME = 'Cluster' DASHBOARD_NAME = 'Cluster'
SEQUENCE = [ SEQUENCE = [
STAGES::CommonMetricsInserter, STAGES::CommonMetricsInserter,
STAGES::ClusterEndpointInserter, STAGES::ClusterEndpointInserter,
STAGES::Sorter STAGES::Sorter
].freeze ].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 end
end end
...@@ -19,13 +19,7 @@ describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_memory_st ...@@ -19,13 +19,7 @@ describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_memory_st
let(:service_call) { described_class.new(*service_params).get_dashboard } let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
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
context 'when called with a non-system dashboard' do context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' } let(:dashboard_path) { 'garbage/dashboard/path' }
......
...@@ -34,7 +34,7 @@ module Gitlab ...@@ -34,7 +34,7 @@ module Gitlab
end end
def system_dashboard?(filepath) def system_dashboard?(filepath)
SERVICES::SystemDashboardService.system_dashboard?(filepath) SERVICES::SystemDashboardService.matching_dashboard?(filepath)
end end
def custom_metric_embed?(params) def custom_metric_embed?(params)
......
...@@ -14,24 +14,18 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -14,24 +14,18 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
end end
describe 'get_dashboard' do 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_params) { [project, user, { environment: environment, dashboard_path: dashboard_path }] }
let(:service_call) { described_class.new(*service_params).get_dashboard } let(:service_call) { described_class.new(*service_params).get_dashboard }
it_behaves_like 'valid dashboard service response' it_behaves_like 'valid dashboard service response'
it_behaves_like 'raises error for users with insufficient permissions' it_behaves_like 'raises error for users with insufficient permissions'
it_behaves_like 'caches the unprocessed dashboard for subsequent calls'
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
context 'when called with a non-system dashboard' do context 'when called with a non-system dashboard' do
let(:dashboard_path) { 'garbage/dashboard/path' } 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' it_behaves_like 'valid dashboard service response'
end end
end end
...@@ -42,8 +36,8 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -42,8 +36,8 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
expect(all_dashboards).to eq( expect(all_dashboards).to eq(
[{ [{
path: described_class::SYSTEM_DASHBOARD_PATH, path: described_class::DASHBOARD_PATH,
display_name: described_class::SYSTEM_DASHBOARD_NAME, display_name: described_class::DASHBOARD_NAME,
default: true, default: true,
system_dashboard: true system_dashboard: true
}] }]
......
...@@ -19,7 +19,7 @@ module MetricsDashboardHelpers ...@@ -19,7 +19,7 @@ module MetricsDashboardHelpers
end end
def system_dashboard_path def system_dashboard_path
Metrics::Dashboard::SystemDashboardService::SYSTEM_DASHBOARD_PATH Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH
end end
def business_metric_title def business_metric_title
...@@ -53,6 +53,15 @@ module MetricsDashboardHelpers ...@@ -53,6 +53,15 @@ module MetricsDashboardHelpers
it_behaves_like 'valid dashboard service response for schema' it_behaves_like 'valid dashboard service response for schema'
end 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 shared_examples_for 'valid embedded dashboard service response' do
let(:dashboard_schema) { JSON.parse(fixture_file('lib/gitlab/metrics/dashboard/schemas/embedded_dashboard.json')) } 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