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 end
def additional_metrics def additional_metrics
return render_404 unless deployment.prometheus_service.present? return render_404 unless deployment.has_additional_metrics?
metrics = deployment.additional_metrics metrics = deployment.additional_metrics

View file

@ -5,7 +5,7 @@ class Projects::PrometheusController < Projects::ApplicationController
def active_metrics def active_metrics
respond_to do |format| respond_to do |format|
format.json do format.json do
matched_metrics = prometheus_service.matched_metrics || {} matched_metrics = project.prometheus_service.matched_metrics || {}
if matched_metrics.any? if matched_metrics.any?
render json: matched_metrics render json: matched_metrics
@ -22,11 +22,7 @@ class Projects::PrometheusController < Projects::ApplicationController
render_404 render_404
end end
def prometheus_service
@prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name)
end
def require_prometheus_metrics! def require_prometheus_metrics!
render_404 unless prometheus_service.present? render_404 unless project.prometheus_service.present?
end end
end end

View file

@ -114,15 +114,15 @@ class Deployment < ActiveRecord::Base
project.monitoring_service.deployment_metrics(self) project.monitoring_service.deployment_metrics(self)
end end
def additional_metrics def has_additional_metrics?
return {} unless prometheus_service.present? project.prometheus_service.present?
metrics = prometheus_service.additional_deployment_metrics(self)
metrics&.merge(deployment_time: created_at.to_i) || {}
end end
def prometheus_service def additional_metrics
@prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name) return {} unless project.prometheus_service.present?
metrics = project.prometheus_service.additional_deployment_metrics(self)
metrics&.merge(deployment_time: created_at.to_i) || {}
end end
private private

View file

@ -158,19 +158,15 @@ class Environment < ActiveRecord::Base
end end
def has_additional_metrics? def has_additional_metrics?
prometheus_service.present? && available? && last_deployment.present? project.prometheus_service.present? && available? && last_deployment.present?
end end
def additional_metrics def additional_metrics
if has_additional_metrics? if has_additional_metrics?
prometheus_service.additional_environment_metrics(self) project.prometheus_service.additional_environment_metrics(self)
end end
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 # 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 # or other third-party contexts, so provide a slugified version. A slug has
# the following properties: # the following properties:

View file

@ -798,6 +798,10 @@ class Project < ActiveRecord::Base
@monitoring_service ||= monitoring_services.reorder(nil).find_by(active: true) @monitoring_service ||= monitoring_services.reorder(nil).find_by(active: true)
end end
def prometheus_service
@prometheus_service ||= monitoring_services.find_by(active: true, type: PrometheusService.name)
end
def jira_tracker? def jira_tracker?
issues_tracker.to_param == 'jira' issues_tracker.to_param == 'jira'
end end

View file

@ -132,7 +132,7 @@ describe Projects::DeploymentsController do
let(:prometheus_service) { double('prometheus_service') } let(:prometheus_service) { double('prometheus_service') }
before do before do
allow(deployment).to receive(:prometheus_service).and_return(prometheus_service) allow(deployment.project).to receive(:prometheus_service).and_return(prometheus_service)
end end
context 'when environment has no metrics' do context 'when environment has no metrics' do

View file

@ -8,7 +8,7 @@ describe Projects::PrometheusController do
before do before do
allow(controller).to receive(:project).and_return(project) 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) project.add_master(user)
sign_in(user) sign_in(user)

View file

@ -91,7 +91,8 @@ describe Deployment, models: true do
end end
describe '#additional_metrics' do describe '#additional_metrics' do
let(:deployment) { create(:deployment) } let(:project) { create(:project) }
let(:deployment) { create(:deployment, project: project) }
subject { deployment.additional_metrics } subject { deployment.additional_metrics }
@ -111,7 +112,7 @@ describe Deployment, models: true do
let(:prometheus_service) { double('prometheus_service') } let(:prometheus_service) { double('prometheus_service') }
before do 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) allow(prometheus_service).to receive(:additional_deployment_metrics).and_return(simple_metrics)
end end