Move Prometheus service to project model

This commit is contained in:
Pawel Chojnacki 2017-06-16 20:48:34 +02:00
parent be5f665557
commit 6e4d533421
8 changed files with 22 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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