diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-19 00:09:12 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-19 00:09:12 +0000 |
commit | b9f288cdfa03f429ebdb03320c5eed9778af0795 (patch) | |
tree | 4c21e2583416ba802a17abdd6b74feb7dd3a75e1 /app/models/performance_monitoring | |
parent | d7b7232142ffcca72b37f2da76d10a31af4cca94 (diff) | |
download | gitlab-ce-b9f288cdfa03f429ebdb03320c5eed9778af0795.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/performance_monitoring')
3 files changed, 35 insertions, 7 deletions
diff --git a/app/models/performance_monitoring/prometheus_dashboard.rb b/app/models/performance_monitoring/prometheus_dashboard.rb index b04e7e689cd..bf87d2c3916 100644 --- a/app/models/performance_monitoring/prometheus_dashboard.rb +++ b/app/models/performance_monitoring/prometheus_dashboard.rb @@ -7,7 +7,7 @@ module PerformanceMonitoring attr_accessor :dashboard, :panel_groups, :path, :environment, :priority, :templating, :links validates :dashboard, presence: true - validates :panel_groups, presence: true + validates :panel_groups, array_members: { member_class: PerformanceMonitoring::PrometheusPanelGroup } class << self def from_json(json_content) @@ -35,9 +35,15 @@ module PerformanceMonitoring new( dashboard: attributes['dashboard'], - panel_groups: attributes['panel_groups']&.map { |group| PrometheusPanelGroup.from_json(group) } + panel_groups: initialize_children_collection(attributes['panel_groups']) ) end + + def initialize_children_collection(children) + return unless children.is_a?(Array) + + children.map { |group| PerformanceMonitoring::PrometheusPanelGroup.from_json(group) } + end end def to_yaml @@ -47,7 +53,7 @@ module PerformanceMonitoring # 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) + self.class.from_json(reload_schema) nil rescue ActiveModel::ValidationError => exception exception.model.errors.map { |attr, error| "#{attr}: #{error}" } @@ -55,6 +61,14 @@ module PerformanceMonitoring private + # 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 + def yaml_valid_attributes %w(panel_groups panels metrics group priority type title y_label weight id unit label query query_range dashboard) end diff --git a/app/models/performance_monitoring/prometheus_panel.rb b/app/models/performance_monitoring/prometheus_panel.rb index a16a68ba832..b33c09001ae 100644 --- a/app/models/performance_monitoring/prometheus_panel.rb +++ b/app/models/performance_monitoring/prometheus_panel.rb @@ -7,7 +7,8 @@ module PerformanceMonitoring attr_accessor :type, :title, :y_label, :weight, :metrics, :y_axis, :max_value validates :title, presence: true - validates :metrics, presence: true + validates :metrics, array_members: { member_class: PerformanceMonitoring::PrometheusMetric } + class << self def from_json(json_content) build_from_hash(json_content).tap(&:validate!) @@ -23,9 +24,15 @@ module PerformanceMonitoring title: attributes['title'], y_label: attributes['y_label'], weight: attributes['weight'], - metrics: attributes['metrics']&.map { |metric| PrometheusMetric.from_json(metric) } + metrics: initialize_children_collection(attributes['metrics']) ) end + + def initialize_children_collection(children) + return unless children.is_a?(Array) + + children.map { |metrics| PerformanceMonitoring::PrometheusMetric.from_json(metrics) } + end end def id(group_title) diff --git a/app/models/performance_monitoring/prometheus_panel_group.rb b/app/models/performance_monitoring/prometheus_panel_group.rb index f88106f259b..7f3d2a1b8f4 100644 --- a/app/models/performance_monitoring/prometheus_panel_group.rb +++ b/app/models/performance_monitoring/prometheus_panel_group.rb @@ -7,7 +7,8 @@ module PerformanceMonitoring attr_accessor :group, :priority, :panels validates :group, presence: true - validates :panels, presence: true + validates :panels, array_members: { member_class: PerformanceMonitoring::PrometheusPanel } + class << self def from_json(json_content) build_from_hash(json_content).tap(&:validate!) @@ -21,9 +22,15 @@ module PerformanceMonitoring new( group: attributes['group'], priority: attributes['priority'], - panels: attributes['panels']&.map { |panel| PrometheusPanel.from_json(panel) } + panels: initialize_children_collection(attributes['panels']) ) end + + def initialize_children_collection(children) + return unless children.is_a?(Array) + + children.map { |panels| PerformanceMonitoring::PrometheusPanel.from_json(panels) } + end end end end |