2018-09-04 05:42:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class PrometheusMetric < ApplicationRecord
|
2018-08-31 12:53:50 -04:00
|
|
|
belongs_to :project, validate: true, inverse_of: :prometheus_metrics
|
|
|
|
|
2019-06-27 15:11:55 -04:00
|
|
|
enum group: PrometheusMetricEnums.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
|
|
|
|
|
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
|
|
|
|
|
|
|
scope :common, -> { where(common: true) }
|
|
|
|
|
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)
|
2019-06-27 15:11:55 -04:00
|
|
|
PrometheusMetricEnums.group_details.fetch(group.to_sym)
|
2018-12-21 06:11:58 -05:00
|
|
|
end
|
2018-08-31 12:53:50 -04:00
|
|
|
end
|