Class: AlertManagement::Alert

Inherits:
ApplicationRecord show all
Includes:
AtomicInternalId, Gitlab::SQL::Pattern, Gitlab::Utils::StrongMemoize, IidRoutes, Noteable, Presentable, ShaAttribute, Sortable
Defined in:
app/models/alert_management/alert.rb

Constant Summary collapse

STATUSES =
{
  triggered: 0,
  acknowledged: 1,
  resolved: 2,
  ignored: 3
}.freeze
STATUS_EVENTS =
{
  triggered: :trigger,
  acknowledged: :acknowledge,
  resolved: :resolve,
  ignored: :ignore
}.freeze
OPEN_STATUSES =
[
  :triggered,
  :acknowledged
].freeze
HOSTS_MAX_LENGTH =
255

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD

Constants included from Noteable

Noteable::MAX_NOTES_LIMIT

Instance Attribute Summary

Attributes included from Noteable

#system_note_timestamp

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from Noteable

#after_note_created, #after_note_destroyed, #base_class_name, #capped_notes_count, #discussion_ids_relation, #discussion_notes, #discussions, #discussions_can_be_resolved_by?, #discussions_rendered_on_frontend?, #discussions_resolvable?, #discussions_resolved?, #discussions_to_be_resolved, #etag_caching_enabled?, #expire_note_etag_cache, #grouped_diff_discussions, #has_any_diff_note_positions?, #human_class_name, #lockable?, #note_etag_key, #preloads_discussion_diff_highlighting?, #resolvable_discussions, #supports_discussions?, #supports_replying_to_individual_notes?, #supports_resolvable_notes?, #supports_suggestion?

Methods included from AtomicInternalId

#internal_id_read_scope, #internal_id_scope_attrs, #internal_id_scope_usage

Methods included from IidRoutes

#to_param

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.last_prometheus_alert_by_project_idObject


168
169
170
171
# File 'app/models/alert_management/alert.rb', line 168

def self.last_prometheus_alert_by_project_id
  ids = select(arel_table[:id].maximum).group(:project_id)
  with_prometheus_alert.where(id: ids)
end

.sort_by_attribute(method) ⇒ Object


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/alert_management/alert.rb', line 151

def self.sort_by_attribute(method)
  case method.to_s
  when 'started_at_asc'     then order_start_time(:asc)
  when 'started_at_desc'    then order_start_time(:desc)
  when 'ended_at_asc'       then order_end_time(:asc)
  when 'ended_at_desc'      then order_end_time(:desc)
  when 'event_count_asc'    then order_event_count(:asc)
  when 'event_count_desc'   then order_event_count(:desc)
  when 'severity_asc'       then order_severity(:asc)
  when 'severity_desc'      then order_severity(:desc)
  when 'status_asc'         then order_status(:asc)
  when 'status_desc'        then order_status(:desc)
  else
    order_by(method)
  end
end

Instance Method Details

#execute_servicesObject


187
188
189
190
191
# File 'app/models/alert_management/alert.rb', line 187

def execute_services
  return unless project.has_active_services?(:alert_hooks)

  project.execute_services(hook_data, :alert_hooks)
end

#parsed_payloadObject

Representation of the alert's payload. Avoid accessing #payload attribute directly.


195
196
197
198
199
# File 'app/models/alert_management/alert.rb', line 195

def parsed_payload
  strong_memoize(:parsed_payload) do
    Gitlab::AlertManagement::Payload.parse(project, payload, monitoring_tool: monitoring_tool)
  end
end

#presentObject


201
202
203
204
205
# File 'app/models/alert_management/alert.rb', line 201

def present
  return super(presenter_class: AlertManagement::PrometheusAlertPresenter) if prometheus?

  super
end

#prometheus?Boolean

Returns:

  • (Boolean)

173
174
175
# File 'app/models/alert_management/alert.rb', line 173

def prometheus?
  monitoring_tool == Gitlab::AlertManagement::AlertParams::MONITORING_TOOLS[:prometheus]
end

#register_new_event!Object


177
178
179
# File 'app/models/alert_management/alert.rb', line 177

def register_new_event!
  increment!(:events)
end

#to_reference(_from = nil, full: false) ⇒ Object

required for todos (typically contains an identifier like issue iid)

no-op; we could use iid, but we don't have a reference prefix

183
184
185
# File 'app/models/alert_management/alert.rb', line 183

def to_reference(_from = nil, full: false)
  ''
end