Class: PrometheusAlert

Inherits:
ApplicationRecord show all
Includes:
Presentable, Sortable, UsageStatistics
Defined in:
app/models/prometheus_alert.rb

Constant Summary collapse

OPERATORS_MAP =
{
  lt: "<",
  eq: "==",
  gt: ">"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

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

.distinct_projectsObject


39
40
41
42
# File 'app/models/prometheus_alert.rb', line 39

def self.distinct_projects
  sub_query = self.group(:project_id).select(1)
  self.from(sub_query)
end

.operator_to_enum(op) ⇒ Object


44
45
46
# File 'app/models/prometheus_alert.rb', line 44

def self.operator_to_enum(op)
  OPERATORS_MAP.invert.fetch(op)
end

Instance Method Details

#computed_operatorObject


52
53
54
# File 'app/models/prometheus_alert.rb', line 52

def computed_operator
  OPERATORS_MAP.fetch(operator.to_sym)
end

#full_queryObject


48
49
50
# File 'app/models/prometheus_alert.rb', line 48

def full_query
  "#{query} #{computed_operator} #{threshold}"
end

#to_paramObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/prometheus_alert.rb', line 56

def to_param
  {
    "alert" => title,
    "expr" => full_query,
    "for" => "5m",
    "labels" => {
      "gitlab" => "hook",
      "gitlab_alert_id" => prometheus_metric_id,
      "gitlab_prometheus_alert_id" => id
    },
    "annotations" => {
      "runbook" => runbook_url
    }
  }
end