Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/performance_monitoring')
-rw-r--r--app/models/performance_monitoring/prometheus_dashboard.rb43
-rw-r--r--app/models/performance_monitoring/prometheus_metric.rb26
-rw-r--r--app/models/performance_monitoring/prometheus_panel.rb25
-rw-r--r--app/models/performance_monitoring/prometheus_panel_group.rb21
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