diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 11:18:50 +0000 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/models/performance_monitoring | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) | |
download | gitlab-ce-8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781.tar.gz |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/models/performance_monitoring')
4 files changed, 77 insertions, 38 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 diff --git a/app/models/performance_monitoring/prometheus_metric.rb b/app/models/performance_monitoring/prometheus_metric.rb index 7b8bef906fa..d67b1809d93 100644 --- a/app/models/performance_monitoring/prometheus_metric.rb +++ b/app/models/performance_monitoring/prometheus_metric.rb @@ -10,16 +10,24 @@ module PerformanceMonitoring validates :query, presence: true, unless: :query_range validates :query_range, presence: true, unless: :query - def self.from_json(json_content) - metric = PrometheusMetric.new( - id: json_content['id'], - unit: json_content['unit'], - label: json_content['label'], - query: json_content['query'], - query_range: json_content['query_range'] - ) + class << self + def from_json(json_content) + build_from_hash(json_content).tap(&:validate!) + end - metric.tap(&:validate!) + private + + def build_from_hash(attributes) + return new unless attributes.is_a?(Hash) + + new( + id: attributes['id'], + unit: attributes['unit'], + label: attributes['label'], + query: attributes['query'], + query_range: attributes['query_range'] + ) + end end end end diff --git a/app/models/performance_monitoring/prometheus_panel.rb b/app/models/performance_monitoring/prometheus_panel.rb index 3fe029abda0..a16a68ba832 100644 --- a/app/models/performance_monitoring/prometheus_panel.rb +++ b/app/models/performance_monitoring/prometheus_panel.rb @@ -8,17 +8,24 @@ module PerformanceMonitoring validates :title, presence: true validates :metrics, presence: true + class << self + def from_json(json_content) + build_from_hash(json_content).tap(&:validate!) + end - def self.from_json(json_content) - panel = new( - type: json_content['type'], - title: json_content['title'], - y_label: json_content['y_label'], - weight: json_content['weight'], - metrics: json_content['metrics'].map { |metric| PrometheusMetric.from_json(metric) } - ) + private - panel.tap(&:validate!) + 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'], + metrics: attributes['metrics']&.map { |metric| PrometheusMetric.from_json(metric) } + ) + 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 e672545fce3..f88106f259b 100644 --- a/app/models/performance_monitoring/prometheus_panel_group.rb +++ b/app/models/performance_monitoring/prometheus_panel_group.rb @@ -8,15 +8,22 @@ module PerformanceMonitoring validates :group, presence: true validates :panels, presence: true + class << self + def from_json(json_content) + build_from_hash(json_content).tap(&:validate!) + end - def self.from_json(json_content) - panel_group = new( - group: json_content['group'], - priority: json_content['priority'], - panels: json_content['panels'].map { |panel| PrometheusPanel.from_json(panel) } - ) + private - panel_group.tap(&:validate!) + def build_from_hash(attributes) + return new unless attributes.is_a?(Hash) + + new( + group: attributes['group'], + priority: attributes['priority'], + panels: attributes['panels']&.map { |panel| PrometheusPanel.from_json(panel) } + ) + end end end end |