debian-mirror-gitlab/app/models/prometheus_metric.rb

82 lines
2.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class PrometheusMetric < ApplicationRecord
2021-01-03 14:25:43 +05:30
include EachBatch
2018-11-20 20:47:30 +05:30
belongs_to :project, validate: true, inverse_of: :prometheus_metrics
2020-03-13 15:44:24 +05:30
has_many :prometheus_alerts, inverse_of: :prometheus_metric
2018-11-20 20:47:30 +05:30
2020-11-24 15:15:51 +05:30
enum group: Enums::PrometheusMetric.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?
2020-11-24 15:15:51 +05:30
scope :for_dashboard_path, -> (dashboard_path) { where(dashboard_path: dashboard_path) }
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) }
2020-11-24 15:15:51 +05:30
scope :not_identifier, -> (identifier) { where.not(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)
2020-11-24 15:15:51 +05:30
Enums::PrometheusMetric.group_details.fetch(group.to_sym)
2019-02-15 15:39:39 +05:30
end
2018-11-20 20:47:30 +05:30
end