diff options
Diffstat (limited to 'app/models/performance_monitoring/prometheus_dashboard.rb')
-rw-r--r-- | app/models/performance_monitoring/prometheus_dashboard.rb | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/app/models/performance_monitoring/prometheus_dashboard.rb b/app/models/performance_monitoring/prometheus_dashboard.rb index 57222c61b36..b04e7e689cd 100644 --- a/app/models/performance_monitoring/prometheus_dashboard.rb +++ b/app/models/performance_monitoring/prometheus_dashboard.rb @@ -4,30 +4,38 @@ module PerformanceMonitoring class PrometheusDashboard include ActiveModel::Model - attr_accessor :dashboard, :panel_groups, :path, :environment, :priority, :templating + attr_accessor :dashboard, :panel_groups, :path, :environment, :priority, :templating, :links validates :dashboard, presence: true validates :panel_groups, presence: true class << self def from_json(json_content) - dashboard = new( - dashboard: json_content['dashboard'], - panel_groups: json_content['panel_groups'].map { |group| PrometheusPanelGroup.from_json(group) } - ) - - dashboard.tap(&:validate!) + build_from_hash(json_content).tap(&:validate!) 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 + 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) new( - { - path: path, - environment: options[:environment] - }.merge(dashboard_response[:dashboard]) + dashboard: attributes['dashboard'], + panel_groups: attributes['panel_groups']&.map { |group| PrometheusPanelGroup.from_json(group) } ) end end @@ -36,6 +44,15 @@ module PerformanceMonitoring self.as_json(only: yaml_valid_attributes).to_yaml end + # 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 + self.class.from_json(self.as_json) + nil + rescue ActiveModel::ValidationError => exception + exception.model.errors.map { |attr, error| "#{attr}: #{error}" } + end + private def yaml_valid_attributes |