Commit fe5227b3 authored by Stan Hu's avatar Stan Hu

Merge branch 'sy-service-selector-refactor' into 'master'

Refactor dashboard ServiceSelector to use consistent selection API

See merge request gitlab-org/gitlab!23488
parents 7f2ea03a 14bf5670
...@@ -20,6 +20,12 @@ module Metrics ...@@ -20,6 +20,12 @@ module Metrics
system_metrics_kubernetes_container_cores_total system_metrics_kubernetes_container_cores_total
).freeze ).freeze
class << self
def valid_params?(params)
params[:embedded].present?
end
end
# Returns a new dashboard with only the matching # Returns a new dashboard with only the matching
# metrics from the system dashboard, stripped of groups. # metrics from the system dashboard, stripped of groups.
# @return [Hash] # @return [Hash]
......
...@@ -15,6 +15,10 @@ module Metrics ...@@ -15,6 +15,10 @@ module Metrics
].freeze ].freeze
class << self class << self
def valid_params?(params)
matching_dashboard?(params[:dashboard_path])
end
def matching_dashboard?(filepath) def matching_dashboard?(filepath)
filepath == self::DASHBOARD_PATH filepath == self::DASHBOARD_PATH
end end
......
...@@ -9,6 +9,10 @@ module Metrics ...@@ -9,6 +9,10 @@ module Metrics
DASHBOARD_ROOT = ".gitlab/dashboards" DASHBOARD_ROOT = ".gitlab/dashboards"
class << self class << self
def valid_params?(params)
params[:dashboard_path].present?
end
def all_dashboard_paths(project) def all_dashboard_paths(project)
file_finder(project) file_finder(project)
.list_files_for(DASHBOARD_ROOT) .list_files_for(DASHBOARD_ROOT)
......
...@@ -14,6 +14,12 @@ module Metrics ...@@ -14,6 +14,12 @@ module Metrics
STAGES::Sorter STAGES::Sorter
].freeze ].freeze
class << self
def valid_params?(params)
params[:cluster].present?
end
end
# Permissions are handled at the controller level # Permissions are handled at the controller level
def allowed? def allowed?
true true
......
...@@ -7,11 +7,18 @@ module EE ...@@ -7,11 +7,18 @@ module EE
module ServiceSelector module ServiceSelector
extend ActiveSupport::Concern extend ActiveSupport::Concern
EE_SERVICES = [
::Metrics::Dashboard::ClusterDashboardService
].freeze
class_methods do class_methods do
def call(params) extend ::Gitlab::Utils::Override
return ::Metrics::Dashboard::ClusterDashboardService if params[:cluster]
private
super override :services
def services
EE_SERVICES + super
end end
end end
end end
......
...@@ -14,7 +14,21 @@ describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_memory_st ...@@ -14,7 +14,21 @@ describe Metrics::Dashboard::ClusterDashboardService, :use_clean_rails_memory_st
project.add_maintainer(user) project.add_maintainer(user)
end end
describe 'get_dashboard' do describe '.valid_params?' do
let(:params) { { cluster: cluster } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing cluster' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
end
describe '#get_dashboard' do
let(:service_params) { [project, user, { cluster: cluster, cluster_type: :project }] } let(:service_params) { [project, user, { cluster: cluster, cluster_type: :project }] }
let(:service_call) { described_class.new(*service_params).get_dashboard } let(:service_call) { described_class.new(*service_params).get_dashboard }
......
...@@ -8,50 +8,39 @@ module Gitlab ...@@ -8,50 +8,39 @@ module Gitlab
module Metrics module Metrics
module Dashboard module Dashboard
class ServiceSelector class ServiceSelector
SERVICES = ::Metrics::Dashboard
class << self class << self
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
SERVICES = [
::Metrics::Dashboard::CustomMetricEmbedService,
::Metrics::Dashboard::GrafanaMetricEmbedService,
::Metrics::Dashboard::DynamicEmbedService,
::Metrics::Dashboard::DefaultEmbedService,
::Metrics::Dashboard::SystemDashboardService,
::Metrics::Dashboard::PodDashboardService,
::Metrics::Dashboard::ProjectDashboardService
].freeze
# Returns a class which inherits from the BaseService # Returns a class which inherits from the BaseService
# class that can be used to obtain a dashboard. # class that can be used to obtain a dashboard for
# the provided params.
# @return [Gitlab::Metrics::Dashboard::Services::BaseService] # @return [Gitlab::Metrics::Dashboard::Services::BaseService]
def call(params) def call(params)
return SERVICES::CustomMetricEmbedService if custom_metric_embed?(params) service = services.find do |service_class|
return SERVICES::GrafanaMetricEmbedService if grafana_metric_embed?(params) service_class.valid_params?(params)
return SERVICES::DynamicEmbedService if dynamic_embed?(params) end
return SERVICES::DefaultEmbedService if params[:embedded]
return SERVICES::SystemDashboardService if system_dashboard?(params[:dashboard_path])
return SERVICES::PodDashboardService if pod_dashboard?(params[:dashboard_path])
return SERVICES::ProjectDashboardService if params[:dashboard_path]
default_service
end
private service || default_service
def default_service
SERVICES::SystemDashboardService
end end
def system_dashboard?(filepath) private
SERVICES::SystemDashboardService.matching_dashboard?(filepath)
end
def pod_dashboard?(filepath)
SERVICES::PodDashboardService.matching_dashboard?(filepath)
end
def custom_metric_embed?(params)
SERVICES::CustomMetricEmbedService.valid_params?(params)
end
def grafana_metric_embed?(params) def services
SERVICES::GrafanaMetricEmbedService.valid_params?(params) SERVICES
end end
def dynamic_embed?(params) def default_service
SERVICES::DynamicEmbedService.valid_params?(params) ::Metrics::Dashboard::SystemDashboardService
end end
end end
end end
......
...@@ -13,6 +13,26 @@ describe Metrics::Dashboard::DefaultEmbedService, :use_clean_rails_memory_store_ ...@@ -13,6 +13,26 @@ describe Metrics::Dashboard::DefaultEmbedService, :use_clean_rails_memory_store_
project.add_maintainer(user) project.add_maintainer(user)
end end
describe '.valid_params?' do
let(:params) { { embedded: true } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing embedded' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
context 'not embedded' do
let(:params) { { embedded: false } }
it { is_expected.to be_falsey }
end
end
describe '#get_dashboard' do describe '#get_dashboard' do
let(:service_params) { [project, user, { environment: environment }] } let(:service_params) { [project, user, { environment: environment }] }
let(:service_call) { described_class.new(*service_params).get_dashboard } let(:service_call) { described_class.new(*service_params).get_dashboard }
......
...@@ -13,7 +13,27 @@ describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_store_ ...@@ -13,7 +13,27 @@ describe Metrics::Dashboard::PodDashboardService, :use_clean_rails_memory_store_
project.add_maintainer(user) project.add_maintainer(user)
end end
describe 'get_dashboard' do describe '.valid_params?' do
let(:params) { { dashboard_path: described_class::DASHBOARD_PATH } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing dashboard_path' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
context 'non-matching dashboard_path' do
let(:params) { { dashboard_path: 'path/to/bunk.yml' } }
it { is_expected.to be_falsey }
end
end
describe '#get_dashboard' do
let(:dashboard_path) { described_class::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 }
......
...@@ -13,7 +13,7 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st ...@@ -13,7 +13,7 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st
project.add_maintainer(user) project.add_maintainer(user)
end end
describe 'get_dashboard' do describe '#get_dashboard' do
let(:dashboard_path) { '.gitlab/dashboards/test.yml' } let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
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 }
...@@ -62,7 +62,7 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st ...@@ -62,7 +62,7 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st
end end
end end
describe '::all_dashboard_paths' do describe '.all_dashboard_paths' do
let(:all_dashboards) { described_class.all_dashboard_paths(project) } let(:all_dashboards) { described_class.all_dashboard_paths(project) }
context 'when there are no project dashboards' do context 'when there are no project dashboards' do
...@@ -87,4 +87,24 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st ...@@ -87,4 +87,24 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st
end end
end end
end end
describe '.valid_params?' do
let(:params) { { dashboard_path: '.gitlab/dashboard/test.yml' } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing dashboard_path' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
context 'empty dashboard_path' do
let(:params) { { dashboard_path: '' } }
it { is_expected.to be_falsey }
end
end
end end
...@@ -13,7 +13,7 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -13,7 +13,7 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
project.add_maintainer(user) project.add_maintainer(user)
end end
describe 'get_dashboard' do describe '#get_dashboard' do
let(:dashboard_path) { described_class::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 }
...@@ -30,7 +30,7 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -30,7 +30,7 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
end end
end end
describe '::all_dashboard_paths' do describe '.all_dashboard_paths' do
it 'returns the dashboard attributes' do it 'returns the dashboard attributes' do
all_dashboards = described_class.all_dashboard_paths(project) all_dashboards = described_class.all_dashboard_paths(project)
...@@ -44,4 +44,24 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -44,4 +44,24 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
) )
end end
end end
describe '.valid_params?' do
let(:params) { { dashboard_path: described_class::DASHBOARD_PATH } }
subject { described_class.valid_params?(params) }
it { is_expected.to be_truthy }
context 'missing dashboard_path' do
let(:params) { {} }
it { is_expected.to be_falsey }
end
context 'non-matching dashboard_path' do
let(:params) { { dashboard_path: 'path/to/bunk.yml' } }
it { is_expected.to be_falsey }
end
end
end end
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