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
TIMESERIES_DEFAULTS =
{ show_legend: false, legend_size: "0" }.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



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

def api_resource
  "dashboard"
end

.normalize(expected, actual) ⇒ Object



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

def normalize(expected, actual)
  super

  widgets_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[1].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 |widgets|
      widgets.each do |widget|
        if formats = widget.dig(:definition, :conditional_formats)
          widget[:definition][:conditional_formats] = formats.sort_by(&:hash)
        end
      end
    end

    ignore_definition_defaults_for_type pair, "timeseries", TIMESERIES_DEFAULTS

    ignore_request_defaults(*pair)

    # ids are kinda random so we always discard them
    pair.each { |widgets| widgets.each { |w| w.delete(:id) } }
  end
end

Instance Method Details

#as_jsonObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kennel/models/dashboard.rb', line 103

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kennel/models/dashboard.rb', line 127

def resolve_linked_tracking_ids(id_map)
  widgets = as_json[:widgets].flat_map { |w| [w, *w.dig(:definition, :widgets) || []] }
  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, :monitor, id_map) : id
        end
      end
    when "alert_graph"
      if (id = definition[:alert_id]) && tracking_id?(id)
        definition[:alert_id] = resolve_link(id, :monitor, id_map).to_s
      end
    when "slo"
      if (id = definition[:slo_id]) && tracking_id?(id)
        definition[:slo_id] = resolve_link(id, :slo, id_map).to_s
      end
    end
  end
end

#url(id) ⇒ Object



123
124
125
# File 'lib/kennel/models/dashboard.rb', line 123

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