2018-09-04 05:42:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class PrometheusMetric < ApplicationRecord
|
2020-10-02 14:08:56 -04:00
|
|
|
include EachBatch
|
|
|
|
|
2018-08-31 12:53:50 -04:00
|
|
|
belongs_to :project, validate: true, inverse_of: :prometheus_metrics
|
2020-01-31 13:09:11 -05:00
|
|
|
has_many :prometheus_alerts, inverse_of: :prometheus_metric
|
2018-08-31 12:53:50 -04:00
|
|
|
|
2020-08-24 17:10:17 -04:00
|
|
|
enum group: Enums::PrometheusMetric.groups
|
2018-12-21 06:11:58 -05:00
|
|
|
|
2018-08-31 12:53:50 -04:00
|
|
|
validates :title, presence: true
|
|
|
|
validates :query, presence: true
|
|
|
|
validates :group, presence: true
|
|
|
|
validates :y_label, presence: true
|
|
|
|
validates :unit, presence: true
|
2020-07-08 08:09:33 -04:00
|
|
|
validates :identifier, uniqueness: { scope: :project_id }, allow_nil: true
|
2018-08-31 12:53:50 -04:00
|
|
|
|
2018-09-04 05:50:58 -04:00
|
|
|
validates :project, presence: true, unless: :common?
|
|
|
|
validates :project, absence: true, if: :common?
|
2018-08-31 12:53:50 -04:00
|
|
|
|
2020-09-02 14:10:40 -04:00
|
|
|
scope :for_dashboard_path, -> (dashboard_path) { where(dashboard_path: dashboard_path) }
|
2019-11-15 07:06:12 -05:00
|
|
|
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-09-02 14:10:40 -04:00
|
|
|
scope :not_identifier, -> (identifier) { where.not(identifier: identifier) }
|
2018-08-31 12:53:50 -04:00
|
|
|
scope :common, -> { where(common: true) }
|
2019-11-15 07:06:12 -05:00
|
|
|
scope :ordered, -> { reorder(created_at: :asc) }
|
2018-08-31 12:53:50 -04:00
|
|
|
|
2018-12-21 06:11:58 -05:00
|
|
|
def priority
|
|
|
|
group_details(group).fetch(:priority)
|
|
|
|
end
|
2018-08-31 12:53:50 -04:00
|
|
|
|
|
|
|
def group_title
|
2018-12-21 06:11:58 -05:00
|
|
|
group_details(group).fetch(:group_title)
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_metrics
|
2018-12-21 06:11:58 -05:00
|
|
|
group_details(group).fetch(:required_metrics, []).map(&:to_s)
|
2018-08-31 12:53:50 -04:00
|
|
|
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-08-07 12:17:35 -04:00
|
|
|
def to_metric_hash
|
|
|
|
queries.first.merge(metric_id: id)
|
|
|
|
end
|
|
|
|
|
2018-08-31 12:53:50 -04:00
|
|
|
def queries
|
|
|
|
[
|
|
|
|
{
|
|
|
|
query_range: query,
|
|
|
|
unit: unit,
|
|
|
|
label: legend,
|
|
|
|
series: query_series
|
2018-09-04 17:09:29 -04:00
|
|
|
}.compact
|
2018-08-31 12:53:50 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_series
|
|
|
|
case legend
|
|
|
|
when 'Status Code'
|
2018-09-05 09:35:08 -04:00
|
|
|
[{
|
2018-08-31 12:53:50 -04:00
|
|
|
label: 'status_code',
|
|
|
|
when: [
|
|
|
|
{ value: '2xx', color: 'green' },
|
|
|
|
{ value: '4xx', color: 'orange' },
|
|
|
|
{ value: '5xx', color: 'red' }
|
|
|
|
]
|
2018-09-05 09:35:08 -04:00
|
|
|
}]
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|
|
|
|
end
|
2018-12-21 06:11:58 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_details(group)
|
2020-08-24 17:10:17 -04:00
|
|
|
Enums::PrometheusMetric.group_details.fetch(group.to_sym)
|
2018-12-21 06:11:58 -05:00
|
|
|
end
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|