2020-01-28 18:08:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PerformanceMonitoring
|
|
|
|
class PrometheusDashboard
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
2020-05-29 21:08:35 +00:00
|
|
|
attr_accessor :dashboard, :panel_groups, :path, :environment, :priority, :templating, :links
|
2020-01-28 18:08:35 +00:00
|
|
|
|
|
|
|
validates :dashboard, presence: true
|
|
|
|
validates :panel_groups, presence: true
|
|
|
|
|
2020-04-10 18:09:32 +00:00
|
|
|
class << self
|
|
|
|
def from_json(json_content)
|
2020-06-05 18:08:19 +00:00
|
|
|
build_from_hash(json_content).tap(&:validate!)
|
2020-04-10 18:09:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_for(project:, user:, path:, options: {})
|
|
|
|
dashboard_response = Gitlab::Metrics::Dashboard::Finder.find(project, user, options.merge(dashboard_path: path))
|
|
|
|
return unless dashboard_response[:status] == :success
|
|
|
|
|
|
|
|
new(
|
|
|
|
{
|
|
|
|
path: path,
|
|
|
|
environment: options[:environment]
|
|
|
|
}.merge(dashboard_response[:dashboard])
|
|
|
|
)
|
|
|
|
end
|
2020-06-05 18:08:19 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_from_hash(attributes)
|
|
|
|
return new unless attributes.is_a?(Hash)
|
|
|
|
|
|
|
|
new(
|
|
|
|
dashboard: attributes['dashboard'],
|
|
|
|
panel_groups: attributes['panel_groups']&.map { |group| PrometheusPanelGroup.from_json(group) }
|
|
|
|
)
|
|
|
|
end
|
2020-01-28 18:08:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_yaml
|
2020-04-10 18:09:32 +00:00
|
|
|
self.as_json(only: yaml_valid_attributes).to_yaml
|
2020-01-28 18:08:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-10 18:09:32 +00:00
|
|
|
def yaml_valid_attributes
|
2020-01-28 18:08:35 +00:00
|
|
|
%w(panel_groups panels metrics group priority type title y_label weight id unit label query query_range dashboard)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|