Class: PrometheusAlert

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

Constant Summary collapse

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

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.distinct_projectsObject



41
42
43
44
# File 'app/models/prometheus_alert.rb', line 41

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

.operator_to_enum(op) ⇒ Object



46
47
48
# File 'app/models/prometheus_alert.rb', line 46

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

Instance Method Details

#computed_operatorObject



54
55
56
# File 'app/models/prometheus_alert.rb', line 54

def computed_operator
  OPERATORS_MAP.fetch(operator.to_sym)
end

#full_queryObject



50
51
52
# File 'app/models/prometheus_alert.rb', line 50

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

#to_paramObject



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

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