Commit 4a9002ce authored by rpereira2's avatar rpereira2 Committed by syasonik

Inherit from BaseService

Change MetricsDashboard::Service to inherit from BaseService so that
it can reuse methods like initialize, success, error.
parent b209fb6f
...@@ -162,9 +162,19 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -162,9 +162,19 @@ class Projects::EnvironmentsController < Projects::ApplicationController
respond_to do |format| respond_to do |format|
format.json do 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 end
end end
......
...@@ -3,19 +3,17 @@ ...@@ -3,19 +3,17 @@
# Fetches the metrics dashboard layout and supplemented the output with DB info. # Fetches the metrics dashboard layout and supplemented the output with DB info.
module Gitlab module Gitlab
module MetricsDashboard module MetricsDashboard
class Service class Service < ::BaseService
SYSTEM_DASHBOARD_NAME = 'common_metrics' SYSTEM_DASHBOARD_NAME = 'common_metrics'
SYSTEM_DASHBOARD_PATH = Rails.root.join('config', 'prometheus', "#{SYSTEM_DASHBOARD_NAME}.yml") 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. # Returns a DB-supplemented json representation of a dashboard config file.
def get_dashboard def get_dashboard
dashboard = Rails.cache.fetch(cache_key) { system_dashboard } dashboard_string = Rails.cache.fetch(cache_key) { system_dashboard }
dashboard = JSON.parse(process_dashboard(dashboard_string))
process_dashboard(dashboard) success(dashboard: dashboard)
end end
private private
......
...@@ -479,8 +479,8 @@ describe Projects::EnvironmentsController do ...@@ -479,8 +479,8 @@ describe Projects::EnvironmentsController do
get :metrics_dashboard, params: environment_params(format: :json) get :metrics_dashboard, params: environment_params(format: :json)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to include('dashboard', 'order', 'panel_groups') expect(json_response.keys).to contain_exactly('dashboard', 'status')
expect(json_response['panel_groups']).to all( include('group', 'priority', 'panels') ) expect(json_response['dashboard']).to be_an_instance_of(Hash)
end end
end end
end end
......
...@@ -7,11 +7,13 @@ describe Gitlab::MetricsDashboard::Service, :use_clean_rails_memory_store_cachin ...@@ -7,11 +7,13 @@ describe Gitlab::MetricsDashboard::Service, :use_clean_rails_memory_store_cachin
describe 'get_dashboard' do describe 'get_dashboard' do
it 'returns a json representation of the environment dashboard' do it 'returns a json representation of the environment dashboard' do
dashboard = described_class.new(project).get_dashboard result = described_class.new(project).get_dashboard
json = JSON.parse(dashboard, symbolize_names: true)
expect(json).to include(:dashboard, :order, :panel_groups) expect(result.keys).to contain_exactly(:dashboard, :status)
expect(json[:panel_groups]).to all( include(:group, :priority, :panels) ) 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 end
it 'caches the dashboard for subsequent calls' do it 'caches the dashboard for subsequent calls' do
......
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