Inherit from BaseService

Change MetricsDashboard::Service to inherit from BaseService so that
it can reuse methods like initialize, success, error.
This commit is contained in:
rpereira2 2019-04-17 15:16:11 +05:30 committed by syasonik
parent b209fb6fb2
commit 4a9002cee3
4 changed files with 25 additions and 15 deletions

View File

@ -162,9 +162,19 @@ class Projects::EnvironmentsController < Projects::ApplicationController
respond_to do |format|
format.json do
dashboard = Gitlab::MetricsDashboard::Service.new(@project).get_dashboard
result = Gitlab::MetricsDashboard::Service.new(@project).get_dashboard
render json: dashboard, status: :ok
if result[:status] == :success
render status: :ok, json: {
status: :success,
dashboard: result[:dashboard]
}
else
render status: result[:http_status] || :bad_request, json: {
message: result[:message],
status: result[:status]
}
end
end
end
end

View File

@ -3,19 +3,17 @@
# Fetches the metrics dashboard layout and supplemented the output with DB info.
module Gitlab
module MetricsDashboard
class Service
class Service < ::BaseService
SYSTEM_DASHBOARD_NAME = 'common_metrics'
SYSTEM_DASHBOARD_PATH = Rails.root.join('config', 'prometheus', "#{SYSTEM_DASHBOARD_NAME}.yml")
def initialize(project)
@project = project
end
# Returns a DB-supplemented json representation of a dashboard config file.
def get_dashboard
dashboard = Rails.cache.fetch(cache_key) { system_dashboard }
dashboard_string = Rails.cache.fetch(cache_key) { system_dashboard }
process_dashboard(dashboard)
dashboard = JSON.parse(process_dashboard(dashboard_string))
success(dashboard: dashboard)
end
private

View File

@ -479,8 +479,8 @@ describe Projects::EnvironmentsController do
get :metrics_dashboard, params: environment_params(format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include('dashboard', 'order', 'panel_groups')
expect(json_response['panel_groups']).to all( include('group', 'priority', 'panels') )
expect(json_response.keys).to contain_exactly('dashboard', 'status')
expect(json_response['dashboard']).to be_an_instance_of(Hash)
end
end
end

View File

@ -7,11 +7,13 @@ describe Gitlab::MetricsDashboard::Service, :use_clean_rails_memory_store_cachin
describe 'get_dashboard' do
it 'returns a json representation of the environment dashboard' do
dashboard = described_class.new(project).get_dashboard
json = JSON.parse(dashboard, symbolize_names: true)
result = described_class.new(project).get_dashboard
expect(json).to include(:dashboard, :order, :panel_groups)
expect(json[:panel_groups]).to all( include(:group, :priority, :panels) )
expect(result.keys).to contain_exactly(:dashboard, :status)
expect(result[:status]).to eq(:success)
expect(result[:dashboard]).to include('dashboard', 'order', 'panel_groups')
expect(result[:dashboard]['panel_groups']).to all( include('group', 'priority', 'panels') )
end
it 'caches the dashboard for subsequent calls' do