Class: Kennel::Models::Dashboard

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

Constant Summary collapse

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
WIDGET_DEFAULTS =
{
  "timeseries" => { show_legend: false, legend_size: "0" },
  "note" => { background_color: "white", font_size: "14", show_tick: false, tick_edge: "left", tick_pos: "50%", text_align: "left" }
}.freeze
SUPPORTED_DEFINITION_OPTIONS =
[:events, :markers, :precision].freeze
DEFAULTS =
{
  template_variable_presets: nil,
  reflow_type: "auto"
}.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

api_resource_map, #diff, #initialize, parse_any_url, #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



37
38
39
# File 'lib/kennel/models/dashboard.rb', line 37

def api_resource
  "dashboard"
end

.normalize(expected, actual) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kennel/models/dashboard.rb', line 41

def normalize(expected, actual)
  super

  ignore_default(expected, actual, DEFAULTS)

  widgets_pairs(expected, actual).each do |pair|
    # 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_widget_defaults pair

    ignore_request_defaults(*pair)

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

.parse_url(url) ⇒ Object



132
133
134
# File 'lib/kennel/models/dashboard.rb', line 132

def self.parse_url(url)
  url[/\/dashboard\/([a-z\d-]+)/, 1]
end

.url(id) ⇒ Object



128
129
130
# File 'lib/kennel/models/dashboard.rb', line 128

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

Instance Method Details

#as_jsonObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kennel/models/dashboard.rb', line 106

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

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

  @json[:id] = id if id

  validate_json(@json) if validate

  @json
end

#resolve_linked_tracking_ids!(id_map, **args) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/kennel/models/dashboard.rb', line 136

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