summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-06 09:09:17 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-06 09:09:17 +0000
commiteaea945e0355826c58c3dcf887496ea91064f85c (patch)
tree0f20e03304d35e68375e99a606b9b94483e37ee5 /app/services
parentcce8cf03d3bebe8b05375e4db0004328f84b28a2 (diff)
downloadgitlab-ce-eaea945e0355826c58c3dcf887496ea91064f85c.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r--app/services/projects/prometheus/metrics/base_service.rb48
-rw-r--r--app/services/projects/prometheus/metrics/destroy_service.rb14
-rw-r--r--app/services/projects/prometheus/metrics/update_service.rb29
3 files changed, 91 insertions, 0 deletions
diff --git a/app/services/projects/prometheus/metrics/base_service.rb b/app/services/projects/prometheus/metrics/base_service.rb
new file mode 100644
index 00000000000..be1783dde70
--- /dev/null
+++ b/app/services/projects/prometheus/metrics/base_service.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module Projects
+ module Prometheus
+ module Metrics
+ class BaseService
+ include Gitlab::Utils::StrongMemoize
+
+ def initialize(metric, params = {})
+ @metric = metric
+ @project = metric.project
+ @params = params.dup
+ end
+
+ protected
+
+ attr_reader :metric, :project, :params
+
+ def application
+ alert.environment.cluster_prometheus_adapter
+ end
+
+ def schedule_alert_update
+ return unless alert
+ return unless alert.environment
+
+ ::Clusters::Applications::ScheduleUpdateService.new(
+ alert.environment.cluster_prometheus_adapter, project).execute
+ end
+
+ def alert
+ strong_memoize(:alert) { find_alert(metric) }
+ end
+
+ def find_alert(metric)
+ Projects::Prometheus::AlertsFinder
+ .new(project: project, metric: metric)
+ .execute
+ .first
+ end
+
+ def has_alert?
+ alert.present?
+ end
+ end
+ end
+ end
+end
diff --git a/app/services/projects/prometheus/metrics/destroy_service.rb b/app/services/projects/prometheus/metrics/destroy_service.rb
new file mode 100644
index 00000000000..6a46eb5516c
--- /dev/null
+++ b/app/services/projects/prometheus/metrics/destroy_service.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+module Projects
+ module Prometheus
+ module Metrics
+ class DestroyService < Metrics::BaseService
+ def execute
+ schedule_alert_update if has_alert?
+ metric.destroy
+ end
+ end
+ end
+ end
+end
diff --git a/app/services/projects/prometheus/metrics/update_service.rb b/app/services/projects/prometheus/metrics/update_service.rb
new file mode 100644
index 00000000000..9b51f4ab47d
--- /dev/null
+++ b/app/services/projects/prometheus/metrics/update_service.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Projects
+ module Prometheus
+ module Metrics
+ class UpdateService < Metrics::BaseService
+ def execute
+ metric.update!(params)
+ schedule_alert_update if requires_alert_update?
+ metric
+ end
+
+ private
+
+ def requires_alert_update?
+ has_alert? && (changing_title? || changing_query?)
+ end
+
+ def changing_title?
+ metric.previous_changes.include?(:title)
+ end
+
+ def changing_query?
+ metric.previous_changes.include?(:query)
+ end
+ end
+ end
+ end
+end