Class: Kennel::Models::Dashboard

Inherits:
Record show all
Includes:
OptionalValidations, TemplateVariables
Defined in:
lib/kennel/models/dashboard.rb

Constant Summary collapse

API_LIST_INCOMPLETE =
true
DASHBOARD_DEFAULTS =
{ template_variables: [] }.freeze
READONLY_ATTRIBUTES =
superclass::READONLY_ATTRIBUTES + [
  :author_handle, :author_name, :modified_at, :url, :is_read_only, :notify_list
]
REQUEST_DEFAULTS =
{
  style: { line_width: "normal", palette: "dog_classic", line_type: "solid" }
}.freeze
SUPPORTED_DEFINITION_OPTIONS =
[:events, :markers, :precision].freeze

Constants inherited from Record

Record::LOCK

Constants inherited from Base

Base::SETTING_OVERRIDABLE_METHODS

Constants included from SettingsAsMethods

SettingsAsMethods::SETTING_OVERRIDABLE_METHODS

Instance Attribute Summary

Attributes inherited from Record

#project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionalValidations

included

Methods included from TemplateVariables

included

Methods inherited from Record

#diff, #initialize, #tracking_id

Methods inherited from Base

#kennel_id, #name, #to_json

Methods included from SubclassTracking

#recursive_subclasses, #subclasses

Methods included from SettingsAsMethods

included, #initialize, #raise_with_location

Constructor Details

This class inherits a constructor from Kennel::Models::Record

Class Method Details

.api_resourceObject



28
29
30
# File 'lib/kennel/models/dashboard.rb', line 28

def api_resource
  "dashboard"
end

.normalize(expected, actual) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kennel/models/dashboard.rb', line 32

def normalize(expected, actual)
  super

  base_pairs(expected, actual).each do |pair|
    # datadog always adds 2 to slo widget height
    # need to check fir layout since some monitors have height/width in their definition
    pair.dig(1, :widgets)&.each do |widget|
      if widget.dig(:definition, :type) == "slo" && widget.dig(:layout, :height)
        widget[:layout][:height] -= 2
      end
    end

    # conditional_formats ordering is randomly changed by datadog, compare a stable ordering
    pair.each do |b|
      b[:widgets]&.each do |w|
        if formats = w.dig(:definition, :conditional_formats)
          w[:definition][:conditional_formats] = formats.sort_by(&:hash)
        end
      end
    end

    ignore_request_defaults(*pair, :widgets, :definition)
    pair.each { |dash| dash[:widgets]&.each { |w| w.delete(:id) } }
  end
end

Instance Method Details

#as_jsonObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kennel/models/dashboard.rb', line 73

def as_json
  return @json if @json
  all_widgets = render_definitions + widgets
  expand_q all_widgets

  @json = {
    layout_type: layout_type,
    title: "#{title}#{LOCK}",
    description: description,
    template_variables: render_template_variables,
    widgets: all_widgets
  }

  @json[:id] = id if id

  validate_json(@json) if validate

  @json
end

#resolve_linked_tracking_ids(id_map) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kennel/models/dashboard.rb', line 97

def resolve_linked_tracking_ids(id_map)
  as_json[:widgets].each do |widget|
    next unless definition = widget[:definition]
    case definition[:type]
    when "uptime"
      if ids = definition[:monitor_ids]
        definition[:monitor_ids] = ids.map do |id|
          tracking_id?(id) ? resolve_link(id, id_map) : id
        end
      end
    when "alert_graph"
      if (id = definition[:alert_id]) && tracking_id?(id)
        definition[:alert_id] = resolve_link(id, id_map).to_s
      end
    when "slo"
      if (id = definition[:slo_id]) && tracking_id?(id)
        definition[:slo_id] = resolve_link(id, id_map).to_s
      end
    end
  end
end

#url(id) ⇒ Object



93
94
95
# File 'lib/kennel/models/dashboard.rb', line 93

def url(id)
  Utils.path_to_url "/dashboard/#{id}"
end