2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class PrometheusMetric < ApplicationRecord
|
2018-11-20 20:47:30 +05:30
|
|
|
belongs_to :project, validate: true, inverse_of: :prometheus_metrics
|
2020-03-09 13:42:32 +05:30
|
|
|
has_many :prometheus_alerts, inverse_of: :prometheus_metric
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
enum group: PrometheusMetricEnums.groups
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
validates :title, presence: true
|
|
|
|
validates :query, presence: true
|
|
|
|
validates :group, presence: true
|
|
|
|
validates :y_label, presence: true
|
|
|
|
validates :unit, presence: true
|
2020-07-28 23:09:34 +05:30
|
|
|
validates :identifier, uniqueness: { scope: :project_id }, allow_nil: true
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
validates :project, presence: true, unless: :common?
|
|
|
|
validates :project, absence: true, if: :common?
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
scope :for_project, -> (project) { where(project: project) }
|
|
|
|
scope :for_group, -> (group) { where(group: group) }
|
|
|
|
scope :for_title, -> (title) { where(title: title) }
|
|
|
|
scope :for_y_label, -> (y_label) { where(y_label: y_label) }
|
|
|
|
scope :for_identifier, -> (identifier) { where(identifier: identifier) }
|
2018-11-20 20:47:30 +05:30
|
|
|
scope :common, -> { where(common: true) }
|
2019-12-26 22:10:19 +05:30
|
|
|
scope :ordered, -> { reorder(created_at: :asc) }
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def priority
|
|
|
|
group_details(group).fetch(:priority)
|
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
def group_title
|
2019-02-15 15:39:39 +05:30
|
|
|
group_details(group).fetch(:group_title)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def required_metrics
|
2019-02-15 15:39:39 +05:30
|
|
|
group_details(group).fetch(:required_metrics, []).map(&:to_s)
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def to_query_metric
|
|
|
|
Gitlab::Prometheus::Metric.new(id: id, title: title, required_metrics: required_metrics, weight: 0, y_label: y_label, queries: queries)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def to_metric_hash
|
|
|
|
queries.first.merge(metric_id: id)
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def queries
|
|
|
|
[
|
|
|
|
{
|
|
|
|
query_range: query,
|
|
|
|
unit: unit,
|
|
|
|
label: legend,
|
|
|
|
series: query_series
|
|
|
|
}.compact
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_series
|
|
|
|
case legend
|
|
|
|
when 'Status Code'
|
|
|
|
[{
|
|
|
|
label: 'status_code',
|
|
|
|
when: [
|
|
|
|
{ value: '2xx', color: 'green' },
|
|
|
|
{ value: '4xx', color: 'orange' },
|
|
|
|
{ value: '5xx', color: 'red' }
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_details(group)
|
2019-09-30 21:07:59 +05:30
|
|
|
PrometheusMetricEnums.group_details.fetch(group.to_sym)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|