2020-01-28 13:08:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PerformanceMonitoring
|
|
|
|
class PrometheusPanel
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
2020-04-15 08:09:18 -04:00
|
|
|
attr_accessor :type, :title, :y_label, :weight, :metrics, :y_axis, :max_value
|
2020-01-28 13:08:35 -05:00
|
|
|
|
|
|
|
validates :title, presence: true
|
2020-06-18 20:09:12 -04:00
|
|
|
validates :metrics, array_members: { member_class: PerformanceMonitoring::PrometheusMetric }
|
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
class << self
|
|
|
|
def from_json(json_content)
|
|
|
|
build_from_hash(json_content).tap(&:validate!)
|
|
|
|
end
|
2020-01-28 13:08:35 -05:00
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
private
|
2020-01-28 13:08:35 -05:00
|
|
|
|
2020-06-05 14:08:19 -04:00
|
|
|
def build_from_hash(attributes)
|
|
|
|
return new unless attributes.is_a?(Hash)
|
|
|
|
|
|
|
|
new(
|
|
|
|
type: attributes['type'],
|
|
|
|
title: attributes['title'],
|
|
|
|
y_label: attributes['y_label'],
|
|
|
|
weight: attributes['weight'],
|
2020-06-18 20:09:12 -04:00
|
|
|
metrics: initialize_children_collection(attributes['metrics'])
|
2020-06-05 14:08:19 -04:00
|
|
|
)
|
|
|
|
end
|
2020-06-18 20:09:12 -04:00
|
|
|
|
|
|
|
def initialize_children_collection(children)
|
|
|
|
return unless children.is_a?(Array)
|
|
|
|
|
|
|
|
children.map { |metrics| PerformanceMonitoring::PrometheusMetric.from_json(metrics) }
|
|
|
|
end
|
2020-01-28 13:08:35 -05:00
|
|
|
end
|
2020-04-10 11:09:50 -04:00
|
|
|
|
|
|
|
def id(group_title)
|
|
|
|
Digest::SHA2.hexdigest([group_title, type, title].join)
|
|
|
|
end
|
2020-01-28 13:08:35 -05:00
|
|
|
end
|
|
|
|
end
|