debian-mirror-gitlab/app/models/performance_monitoring/prometheus_dashboard.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.7 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module PerformanceMonitoring
class PrometheusDashboard
include ActiveModel::Model
2020-06-23 00:09:42 +05:30
attr_accessor :dashboard, :panel_groups, :path, :environment, :priority, :templating, :links
2020-03-13 15:44:24 +05:30
validates :dashboard, presence: true
2020-07-28 23:09:34 +05:30
validates :panel_groups, array_members: { member_class: PerformanceMonitoring::PrometheusPanelGroup }
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
class << self
def from_json(json_content)
2020-06-23 00:09:42 +05:30
build_from_hash(json_content).tap(&:validate!)
2020-04-22 19:07:51 +05:30
end
def find_for(project:, user:, path:, options: {})
2020-06-23 00:09:42 +05:30
template = { path: path, environment: options[:environment] }
rsp = Gitlab::Metrics::Dashboard::Finder.find(project, user, options.merge(dashboard_path: path))
case rsp[:http_status] || rsp[:status]
when :success
new(template.merge(rsp[:dashboard] || {})) # when there is empty dashboard file returned rsp is still a success
when :unprocessable_entity
new(template) # validation error
else
nil # any other error
end
end
private
def build_from_hash(attributes)
return new unless attributes.is_a?(Hash)
2020-04-22 19:07:51 +05:30
new(
2020-06-23 00:09:42 +05:30
dashboard: attributes['dashboard'],
2020-07-28 23:09:34 +05:30
panel_groups: initialize_children_collection(attributes['panel_groups'])
2020-04-22 19:07:51 +05:30
)
end
2020-07-28 23:09:34 +05:30
def initialize_children_collection(children)
return unless children.is_a?(Array)
children.map { |group| PerformanceMonitoring::PrometheusPanelGroup.from_json(group) }
end
2020-03-13 15:44:24 +05:30
end
def to_yaml
2020-04-22 19:07:51 +05:30
self.as_json(only: yaml_valid_attributes).to_yaml
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
# This method is planned to be refactored as a part of https://gitlab.com/gitlab-org/gitlab/-/issues/219398
# implementation. For new existing logic was reused to faster deliver MVC
def schema_validation_warnings
2020-07-28 23:09:34 +05:30
self.class.from_json(reload_schema)
2020-11-24 15:15:51 +05:30
[]
2022-08-27 11:52:29 +05:30
rescue Gitlab::Metrics::Dashboard::Errors::LayoutError => e
[e.message]
rescue ActiveModel::ValidationError => e
e.model.errors.map { |attr, error| "#{attr}: #{error}" }
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
private
2020-07-28 23:09:34 +05:30
# dashboard finder methods are somehow limited, #find includes checking if
# user is authorised to view selected dashboard, but modifies schema, which in some cases may
# cause false positives returned from validation, and #find_raw does not authorise users
def reload_schema
project = environment&.project
project.nil? ? self.as_json : Gitlab::Metrics::Dashboard::Finder.find_raw(project, dashboard_path: path)
end
2020-04-22 19:07:51 +05:30
def yaml_valid_attributes
2020-03-13 15:44:24 +05:30
%w(panel_groups panels metrics group priority type title y_label weight id unit label query query_range dashboard)
end
end
end