Commit 6e4d5334 authored by Pawel Chojnacki's avatar Pawel Chojnacki
Browse files

Move Prometheus service to project model

parent af24b108749e
......@@ -23,7 +23,7 @@ def metrics
end
def additional_metrics
return render_404 unless deployment.prometheus_service.present?
return render_404 unless deployment.has_additional_metrics?
metrics = deployment.additional_metrics
......
......@@ -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 @@ def active_metrics
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
......@@ -114,17 +114,17 @@ def metrics
project.monitoring_service.deployment_metrics(self)
end
def has_additional_metrics?
project.prometheus_service.present?
end
def additional_metrics
return {} unless prometheus_service.present?
return {} unless project.prometheus_service.present?
metrics = prometheus_service.additional_deployment_metrics(self)
metrics = project.prometheus_service.additional_deployment_metrics(self)
metrics&.merge(deployment_time: created_at.to_i) || {}
end
def prometheus_service
@prometheus_service ||= project.monitoring_services.reorder(nil).find_by(active: true, type: PrometheusService.name)
end
private
def ref_path
......
......@@ -158,19 +158,15 @@ def metrics
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:
......
......@@ -798,6 +798,10 @@ def monitoring_service
@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
......
......@@ -132,7 +132,7 @@
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
......
......@@ -8,7 +8,7 @@
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)
......
......@@ -30,7 +30,7 @@
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 @@
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 @@
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
......
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