summaryrefslogtreecommitdiff
path: root/app/models/performance_monitoring/prometheus_panel_group.rb
blob: 7f3d2a1b8f42173b17585bb7e826e8378da1a766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

module PerformanceMonitoring
  class PrometheusPanelGroup
    include ActiveModel::Model

    attr_accessor :group, :priority, :panels

    validates :group, presence: true
    validates :panels, array_members: { member_class: PerformanceMonitoring::PrometheusPanel }

    class << self
      def from_json(json_content)
        build_from_hash(json_content).tap(&:validate!)
      end

      private

      def build_from_hash(attributes)
        return new unless attributes.is_a?(Hash)

        new(
          group: attributes['group'],
          priority: attributes['priority'],
          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