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
]
TRACKING_FIELD =
:description
REQUEST_DEFAULTS =
{
  style: { line_width: "normal", palette: "dog_classic", line_type: "solid" }
}.freeze
WIDGET_DEFAULTS =
{
  "timeseries" => {
    legend_size: "0",
    markers: [],
    legend_columns: [
      "avg",
      "min",
      "max",
      "value",
      "sum"
    ],
    legend_layout: "auto",
    yaxis: {
      include_zero: true,
      label: "",
      scale: "linear",
      min: "auto",
      max: "auto"
    },
    show_legend: true,
    time: {},
    title_align: "left",
    title_size: "16"
  },
  "note" => {
    show_tick: false,
    tick_edge: "left",
    tick_pos: "50%",
    text_align: "left",
    has_padding: true,
    background_color: "white",
    font_size: "14"
  },
  "query_value" => {
    autoscale: true,
    time: {},
    title_align: "left",
    title_size: "16"
  },
  "free_text" => {
    font_size: "auto"
  },
  "check_status" => {
    title_align: "left",
    title_size: "16"
  },
  "slo" => {
    global_time_target: "0",
    title_align: "left",
    title_size: "16"
  }
}.freeze
SUPPORTED_DEFINITION_OPTIONS =
[:events, :markers, :precision].freeze
DEFAULTS =
{
  template_variable_presets: nil
}.freeze

Constants inherited from Record

Record::LOCK, Record::TRACKING_FIELDS

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

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



85
86
87
# File 'lib/kennel/models/dashboard.rb', line 85

def api_resource
  "dashboard"
end

.normalize(expected, actual) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kennel/models/dashboard.rb', line 89

def normalize(expected, actual)
  super

  ignore_default expected, actual, DEFAULTS
  ignore_default expected, actual, reflow_type: "auto" if expected[:layout_type] == "ordered"

  widgets_pairs(expected, actual).each do |pair|
    pair.each { |w| sort_conditional_formats w }
    ignore_widget_defaults(*pair)
    ignore_request_defaults(*pair)
    pair.each { |widget| widget&.delete(:id) } # ids are kinda random so we always discard them
  end
end

.parse_url(url) ⇒ Object



172
173
174
# File 'lib/kennel/models/dashboard.rb', line 172

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

.url(id) ⇒ Object



168
169
170
# File 'lib/kennel/models/dashboard.rb', line 168

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

Instance Method Details

#as_jsonObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/kennel/models/dashboard.rb', line 145

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,
    template_variables: render_template_variables,
    template_variable_presets: template_variable_presets,
    widgets: all_widgets
  }

  @json[:reflow_type] = reflow_type if reflow_type # setting nil breaks create with "ordered"

  @json[:id] = id if id

  validate_json(@json) if validate

  @json
end

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/kennel/models/dashboard.rb', line 176

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