From 6e4d5334211d73dd731cb8757b2ef10e8ea428b7 Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Fri, 16 Jun 2017 20:48:34 +0200 Subject: [PATCH] Move Prometheus service to project model --- app/controllers/projects/deployments_controller.rb | 2 +- app/controllers/projects/prometheus_controller.rb | 8 ++------ app/models/deployment.rb | 14 +++++++------- app/models/environment.rb | 8 ++------ app/models/project.rb | 4 ++++ .../projects/deployments_controller_spec.rb | 2 +- .../projects/prometheus_controller_spec.rb | 2 +- spec/models/deployment_spec.rb | 7 ++++--- 8 files changed, 22 insertions(+), 25 deletions(-) diff --git a/app/controllers/projects/deployments_controller.rb b/app/controllers/projects/deployments_controller.rb index e7d95e46b06..acf5573935a 100644 --- a/app/controllers/projects/deployments_controller.rb +++ b/app/controllers/projects/deployments_controller.rb @@ -23,7 +23,7 @@ class Projects::DeploymentsController < Projects::ApplicationController end def additional_metrics - return render_404 unless deployment.prometheus_service.present? + return render_404 unless deployment.has_additional_metrics? metrics = deployment.additional_metrics diff --git a/app/controllers/projects/prometheus_controller.rb b/app/controllers/projects/prometheus_controller.rb index 9609fa44945..7b828981dd8 100644 --- a/app/controllers/projects/prometheus_controller.rb +++ b/app/controllers/projects/prometheus_controller.rb @@ -5,7 +5,7 @@ class Projects::PrometheusController < Projects::ApplicationController def active_metrics respond_to do |format| format.json do - matched_metrics = prometheus_service.matched_metrics || {} + matched_metrics = project.prometheus_service.matched_metrics || {} if matched_metrics.any? render json: matched_metrics @@ -22,11 +22,7 @@ class Projects::PrometheusController < Projects::ApplicationController render_404 end - def prometheus_service - @prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name) - end - def require_prometheus_metrics! - render_404 unless prometheus_service.present? + render_404 unless project.prometheus_service.present? end end diff --git a/app/models/deployment.rb b/app/models/deployment.rb index 04d08f2cfd5..00bf0c118ae 100644 --- a/app/models/deployment.rb +++ b/app/models/deployment.rb @@ -114,15 +114,15 @@ class Deployment < ActiveRecord::Base project.monitoring_service.deployment_metrics(self) end - def additional_metrics - return {} unless prometheus_service.present? - - metrics = prometheus_service.additional_deployment_metrics(self) - metrics&.merge(deployment_time: created_at.to_i) || {} + def has_additional_metrics? + project.prometheus_service.present? end - def prometheus_service - @prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name) + def additional_metrics + return {} unless project.prometheus_service.present? + + metrics = project.prometheus_service.additional_deployment_metrics(self) + metrics&.merge(deployment_time: created_at.to_i) || {} end private diff --git a/app/models/environment.rb b/app/models/environment.rb index 62db7e958ab..b391a487b30 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -158,19 +158,15 @@ class Environment < ActiveRecord::Base end def has_additional_metrics? - prometheus_service.present? && available? && last_deployment.present? + project.prometheus_service.present? && available? && last_deployment.present? end def additional_metrics if has_additional_metrics? - prometheus_service.additional_environment_metrics(self) + project.prometheus_service.additional_environment_metrics(self) end end - def prometheus_service - @prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name) - end - # An environment name is not necessarily suitable for use in URLs, DNS # or other third-party contexts, so provide a slugified version. A slug has # the following properties: diff --git a/app/models/project.rb b/app/models/project.rb index 4c394646787..f6338b92b9f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -798,6 +798,10 @@ class Project < ActiveRecord::Base @monitoring_service ||= monitoring_services.reorder(nil).find_by(active: true) end + def prometheus_service + @prometheus_service ||= monitoring_services.find_by(active: true, type: PrometheusService.name) + end + def jira_tracker? issues_tracker.to_param == 'jira' end diff --git a/spec/controllers/projects/deployments_controller_spec.rb b/spec/controllers/projects/deployments_controller_spec.rb index c3b4f812db9..4a77d4d8289 100644 --- a/spec/controllers/projects/deployments_controller_spec.rb +++ b/spec/controllers/projects/deployments_controller_spec.rb @@ -132,7 +132,7 @@ describe Projects::DeploymentsController do let(:prometheus_service) { double('prometheus_service') } before do - allow(deployment).to receive(:prometheus_service).and_return(prometheus_service) + allow(deployment.project).to receive(:prometheus_service).and_return(prometheus_service) end context 'when environment has no metrics' do diff --git a/spec/controllers/projects/prometheus_controller_spec.rb b/spec/controllers/projects/prometheus_controller_spec.rb index a994ac6409f..eddf7275975 100644 --- a/spec/controllers/projects/prometheus_controller_spec.rb +++ b/spec/controllers/projects/prometheus_controller_spec.rb @@ -8,7 +8,7 @@ describe Projects::PrometheusController do before do allow(controller).to receive(:project).and_return(project) - allow(controller).to receive(:prometheus_service).and_return(prometheus_service) + allow(project).to receive(:prometheus_service).and_return(prometheus_service) project.add_master(user) sign_in(user) diff --git a/spec/models/deployment_spec.rb b/spec/models/deployment_spec.rb index eba0175c54c..bb84d3fc13d 100644 --- a/spec/models/deployment_spec.rb +++ b/spec/models/deployment_spec.rb @@ -30,7 +30,7 @@ describe Deployment, models: true do end describe '#includes_commit?' do - let(:project) { create(:project, :repository) } + let(:project) { create(:project, :repository) } let(:environment) { create(:environment, project: project) } let(:deployment) do create(:deployment, environment: environment, sha: project.commit.id) @@ -91,7 +91,8 @@ describe Deployment, models: true do end describe '#additional_metrics' do - let(:deployment) { create(:deployment) } + let(:project) { create(:project) } + let(:deployment) { create(:deployment, project: project) } subject { deployment.additional_metrics } @@ -111,7 +112,7 @@ describe Deployment, models: true do let(:prometheus_service) { double('prometheus_service') } before do - allow(deployment).to receive(:prometheus_service).and_return(prometheus_service) + allow(project).to receive(:prometheus_service).and_return(prometheus_service) allow(prometheus_service).to receive(:additional_deployment_metrics).and_return(simple_metrics) end