summaryrefslogtreecommitdiff
path: root/app/models/self_managed_prometheus_alert_event.rb
blob: cf26563e92d02332526abe8c2e779cdfb9cae4d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# frozen_string_literal: true

class SelfManagedPrometheusAlertEvent < ApplicationRecord
  include AlertEventLifecycle

  belongs_to :project, validate: true, inverse_of: :self_managed_prometheus_alert_events
  belongs_to :environment, validate: true, inverse_of: :self_managed_prometheus_alert_events
  has_and_belongs_to_many :related_issues, class_name: 'Issue', join_table: :issues_self_managed_prometheus_alert_events # rubocop:disable Rails/HasAndBelongsToMany

  validates :started_at, presence: true
  validates :payload_key, uniqueness: { scope: :project_id }

  def self.find_or_initialize_by_payload_key(project, payload_key)
    find_or_initialize_by(project: project, payload_key: payload_key) do |event|
      yield event if block_given?
    end
  end
end