Class: Gitlab::Alerting::Alert

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Utils::StrongMemoize, Presentable
Defined in:
lib/gitlab/alerting/alert.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Attribute Details

#am_alertObject

Returns the value of attribute am_alert


10
11
12
# File 'lib/gitlab/alerting/alert.rb', line 10

def am_alert
  @am_alert
end

#payloadObject

Returns the value of attribute payload


10
11
12
# File 'lib/gitlab/alerting/alert.rb', line 10

def payload
  @payload
end

#projectObject

Returns the value of attribute project


10
11
12
# File 'lib/gitlab/alerting/alert.rb', line 10

def project
  @project
end

Class Method Details

.for_alert_management_alert(project:, alert:) ⇒ Object


12
13
14
15
16
17
18
19
20
# File 'lib/gitlab/alerting/alert.rb', line 12

def self.for_alert_management_alert(project:, alert:)
  params = if alert.prometheus?
             alert.payload
           else
             Gitlab::Alerting::NotificationPayloadParser.call(alert.payload.to_h, alert.project)
           end

  self.new(project: project, payload: params, am_alert: alert)
end

Instance Method Details

#alert_markdownObject


94
95
96
97
98
# File 'lib/gitlab/alerting/alert.rb', line 94

def alert_markdown
  strong_memoize(:alert_markdown) do
    parse_alert_markdown_from_payload
  end
end

#annotationsObject


58
59
60
61
62
# File 'lib/gitlab/alerting/alert.rb', line 58

def annotations
  strong_memoize(:annotations) do
    parse_annotations_from_payload || []
  end
end

#descriptionObject


46
47
48
49
50
# File 'lib/gitlab/alerting/alert.rb', line 46

def description
  strong_memoize(:description) do
    parse_description_from_payload
  end
end

#ends_atObject


76
77
78
79
80
# File 'lib/gitlab/alerting/alert.rb', line 76

def ends_at
  strong_memoize(:ends_at) do
    parse_datetime_from_payload('endsAt')
  end
end

#environmentObject


52
53
54
55
56
# File 'lib/gitlab/alerting/alert.rb', line 52

def environment
  strong_memoize(:environment) do
    gitlab_alert&.environment || parse_environment_from_payload
  end
end

#firing?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'lib/gitlab/alerting/alert.rb', line 106

def firing?
  status == 'firing'
end

#full_queryObject


82
83
84
85
86
# File 'lib/gitlab/alerting/alert.rb', line 82

def full_query
  strong_memoize(:full_query) do
    gitlab_alert&.full_query || parse_expr_from_payload
  end
end

#gitlab_alertObject


22
23
24
25
26
# File 'lib/gitlab/alerting/alert.rb', line 22

def gitlab_alert
  strong_memoize(:gitlab_alert) do
    parse_gitlab_alert_from_payload
  end
end

#gitlab_fingerprintObject


118
119
120
# File 'lib/gitlab/alerting/alert.rb', line 118

def gitlab_fingerprint
  Gitlab::AlertManagement::Fingerprint.generate(plain_gitlab_fingerprint)
end

#gitlab_managed?Boolean

Returns:

  • (Boolean)

114
115
116
# File 'lib/gitlab/alerting/alert.rb', line 114

def gitlab_managed?
  metric_id.present?
end

#gitlab_prometheus_alert_idObject


34
35
36
37
38
# File 'lib/gitlab/alerting/alert.rb', line 34

def gitlab_prometheus_alert_id
  strong_memoize(:gitlab_prometheus_alert_id) do
    payload&.dig('labels', 'gitlab_prometheus_alert_id')
  end
end

#metric_idObject


28
29
30
31
32
# File 'lib/gitlab/alerting/alert.rb', line 28

def metric_id
  strong_memoize(:metric_id) do
    payload&.dig('labels', 'gitlab_alert_id')
  end
end

#presentObject


126
127
128
# File 'lib/gitlab/alerting/alert.rb', line 126

def present
  super(presenter_class: Projects::Prometheus::AlertPresenter)
end

#resolved?Boolean

Returns:

  • (Boolean)

110
111
112
# File 'lib/gitlab/alerting/alert.rb', line 110

def resolved?
  status == 'resolved'
end

#starts_atObject


64
65
66
67
68
# File 'lib/gitlab/alerting/alert.rb', line 64

def starts_at
  strong_memoize(:starts_at) do
    parse_datetime_from_payload('startsAt')
  end
end

#starts_at_rawObject


70
71
72
73
74
# File 'lib/gitlab/alerting/alert.rb', line 70

def starts_at_raw
  strong_memoize(:starts_at_raw) do
    payload&.dig('startsAt')
  end
end

#statusObject


100
101
102
103
104
# File 'lib/gitlab/alerting/alert.rb', line 100

def status
  strong_memoize(:status) do
    payload&.dig('status')
  end
end

#titleObject


40
41
42
43
44
# File 'lib/gitlab/alerting/alert.rb', line 40

def title
  strong_memoize(:title) do
    gitlab_alert&.title || parse_title_from_payload
  end
end

#valid?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'lib/gitlab/alerting/alert.rb', line 122

def valid?
  payload.respond_to?(:dig) && project && title && starts_at
end

#y_labelObject


88
89
90
91
92
# File 'lib/gitlab/alerting/alert.rb', line 88

def y_label
  strong_memoize(:y_label) do
    parse_y_label_from_payload || title
  end
end