Commit fc7eba33 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '199997-move-prometheus-alert-from-ee-to-ce' into 'master'

Move PrometheusAlert from EE to CE

Closes #199997

See merge request gitlab-org/gitlab!24360
parents 82fd2d65 c6d7f014
......@@ -58,5 +58,12 @@ module PrometheusAdapter
def build_query_args(*args)
args.map { |arg| arg.respond_to?(:id) ? arg.id : arg }
end
def clear_prometheus_reactive_cache!(query_name, *args)
query_class = query_klass_for(query_name)
query_args = build_query_args(*args)
clear_reactive_cache!(query_class.name, *query_args)
end
end
end
......@@ -11,6 +11,7 @@ class Environment < ApplicationRecord
has_many :deployments, -> { visible }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :successful_deployments, -> { success }, class_name: 'Deployment'
has_many :prometheus_alerts, inverse_of: :environment
has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
has_one :last_deployable, through: :last_deployment, source: 'deployable', source_type: 'CommitStatus'
......@@ -105,6 +106,14 @@ class Environment < ApplicationRecord
find_or_create_by(name: name)
end
def clear_prometheus_reactive_cache!(query_name)
cluster_prometheus_adapter&.clear_prometheus_reactive_cache!(query_name, self)
end
def cluster_prometheus_adapter
@cluster_prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).cluster_prometheus_adapter
end
def predefined_variables
Gitlab::Ci::Variables::Collection.new
.append(key: 'CI_ENVIRONMENT_NAME', value: name)
......
......@@ -244,6 +244,7 @@ class Project < ApplicationRecord
has_many :management_clusters, class_name: 'Clusters::Cluster', foreign_key: :management_project_id, inverse_of: :management_project
has_many :prometheus_metrics
has_many :prometheus_alerts, inverse_of: :project
# Container repositories need to remove data from the container registry,
# which is not managed by the DB. Hence we're still using dependent: :destroy
......
......@@ -9,9 +9,9 @@ class PrometheusAlert < ApplicationRecord
gt: ">"
}.freeze
belongs_to :environment, required: true, validate: true, inverse_of: :prometheus_alerts
belongs_to :project, required: true, validate: true, inverse_of: :prometheus_alerts
belongs_to :prometheus_metric, required: true, validate: true, inverse_of: :prometheus_alerts
belongs_to :environment, validate: true, inverse_of: :prometheus_alerts
belongs_to :project, validate: true, inverse_of: :prometheus_alerts
belongs_to :prometheus_metric, validate: true, inverse_of: :prometheus_alerts
has_many :prometheus_alert_events, inverse_of: :prometheus_alert
has_many :related_issues, through: :prometheus_alert_events
......@@ -19,10 +19,11 @@ class PrometheusAlert < ApplicationRecord
after_save :clear_prometheus_adapter_cache!
after_destroy :clear_prometheus_adapter_cache!
validates :environment, :project, :prometheus_metric, presence: true
validate :require_valid_environment_project!
validate :require_valid_metric_project!
enum operator: [:lt, :eq, :gt]
enum operator: { lt: 0, eq: 1, gt: 2 }
delegate :title, :query, to: :prometheus_metric
......
......@@ -2,13 +2,5 @@
module EE
module PrometheusAdapter
extend ::Gitlab::Utils::Override
def clear_prometheus_reactive_cache!(query_name, *args)
query_class = query_klass_for(query_name)
query_args = build_query_args(*args)
clear_reactive_cache!(query_class.name, *query_args)
end
end
end
......@@ -7,7 +7,6 @@ module EE
include ::Gitlab::Utils::StrongMemoize
prepended do
has_many :prometheus_alerts, inverse_of: :environment
has_many :self_managed_prometheus_alert_events, inverse_of: :environment
# Returns environments where its latest deployment is to a cluster
......@@ -76,14 +75,6 @@ module EE
end
end
def clear_prometheus_reactive_cache!(query_name)
cluster_prometheus_adapter&.clear_prometheus_reactive_cache!(query_name, self)
end
def cluster_prometheus_adapter
@cluster_prometheus_adapter ||= ::Gitlab::Prometheus::Adapter.new(project, deployment_platform&.cluster).cluster_prometheus_adapter
end
def protected?
project.protected_environment_by_name(name).present?
end
......
......@@ -83,7 +83,6 @@ module EE
has_many :webide_pipelines, -> { webide_source }, class_name: 'Ci::Pipeline', inverse_of: :project
has_many :prometheus_alerts, inverse_of: :project
has_many :prometheus_alert_events, inverse_of: :project
has_many :self_managed_prometheus_alert_events, inverse_of: :project
......
......@@ -31,8 +31,8 @@ describe PrometheusAlert do
end
describe 'associations' do
it { is_expected.to belong_to(:project).required }
it { is_expected.to belong_to(:environment).required }
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:environment) }
end
describe 'project validations' do
......@@ -43,6 +43,10 @@ describe PrometheusAlert do
build(:prometheus_alert, prometheus_metric: metric, environment: environment, project: project)
end
it { is_expected.to validate_presence_of(:environment) }
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:prometheus_metric) }
context 'when environment and metric belongs same project' do
it { is_expected.to be_valid }
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