diff options
Diffstat (limited to 'app/models/performance_monitoring/prometheus_metric.rb')
-rw-r--r-- | app/models/performance_monitoring/prometheus_metric.rb | 26 |
1 files changed, 17 insertions, 9 deletions
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 |