summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorThong Kuah <tkuah@gitlab.com>2019-07-01 21:40:59 +1200
committerThong Kuah <tkuah@gitlab.com>2019-07-08 09:13:21 +1200
commitd2ba2951f737082edd568505f985ebf9a0808be7 (patch)
tree7da25a607c16810546f6ea57e5354ba145029918 /app/models
parent1b5b0dea5228ae7fd520c8bca3f03c4799a4d31d (diff)
downloadgitlab-ce-d2ba2951f737082edd568505f985ebf9a0808be7.tar.gz
Extract deployment_metrics into own object
We can now share project so that we don't have to load project twice. Also, this extracts non-relevant logic out of Deployment. Update DeploymentsController accordingly
Diffstat (limited to 'app/models')
-rw-r--r--app/models/deployment.rb32
-rw-r--r--app/models/deployment_metrics.rb50
-rw-r--r--app/models/environment_status.rb4
3 files changed, 54 insertions, 32 deletions
diff --git a/app/models/deployment.rb b/app/models/deployment.rb
index 3b31e7b1333..ad32ec3f1d5 100644
--- a/app/models/deployment.rb
+++ b/app/models/deployment.rb
@@ -176,40 +176,8 @@ class Deployment < ApplicationRecord
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
end
- def has_metrics?
- success? && prometheus_adapter&.can_query?
- end
-
- def metrics
- return {} unless has_metrics?
-
- metrics = prometheus_adapter.query(:deployment, self)
- metrics&.merge(deployment_time: finished_at.to_i) || {}
- end
-
- def additional_metrics
- return {} unless has_metrics?
-
- metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
- metrics&.merge(deployment_time: finished_at.to_i) || {}
- end
-
private
- def prometheus_adapter
- service = project.find_or_initialize_service('prometheus')
-
- if service.can_query?
- service
- else
- cluster_prometheus
- end
- end
-
- def cluster_prometheus
- cluster.application_prometheus if cluster&.application_prometheus_available?
- end
-
def ref_path
File.join(environment.ref_path, 'deployments', iid.to_s)
end
diff --git a/app/models/deployment_metrics.rb b/app/models/deployment_metrics.rb
new file mode 100644
index 00000000000..2056c8bc59c
--- /dev/null
+++ b/app/models/deployment_metrics.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+class DeploymentMetrics
+ include Gitlab::Utils::StrongMemoize
+
+ attr_reader :project, :deployment
+
+ delegate :cluster, to: :deployment
+
+ def initialize(project, deployment)
+ @project = project
+ @deployment = deployment
+ end
+
+ def has_metrics?
+ deployment.success? && prometheus_adapter&.can_query?
+ end
+
+ def metrics
+ return {} unless has_metrics?
+
+ metrics = prometheus_adapter.query(:deployment, deployment)
+ metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
+ end
+
+ def additional_metrics
+ return {} unless has_metrics?
+
+ metrics = prometheus_adapter.query(:additional_metrics_deployment, deployment)
+ metrics&.merge(deployment_time: deployment.finished_at.to_i) || {}
+ end
+
+ private
+
+ def prometheus_adapter
+ strong_memoize(:prometheus_adapter) do
+ service = project.find_or_initialize_service('prometheus')
+
+ if service.can_query?
+ service
+ else
+ cluster_prometheus
+ end
+ end
+ end
+
+ def cluster_prometheus
+ cluster.application_prometheus if cluster&.application_prometheus_available?
+ end
+end
diff --git a/app/models/environment_status.rb b/app/models/environment_status.rb
index 2c71520cec1..1b3c094901b 100644
--- a/app/models/environment_status.rb
+++ b/app/models/environment_status.rb
@@ -33,6 +33,10 @@ class EnvironmentStatus
end
end
+ def has_metrics?
+ DeploymentMetrics.new(project, deployment).has_metrics?
+ end
+
def changes
return [] if project.route_map_for(sha).nil?