Class: Kennel::Models::Slo

Inherits:
Record show all
Defined in:
lib/kennel/models/slo.rb

Constant Summary collapse

READONLY_ATTRIBUTES =
superclass::READONLY_ATTRIBUTES + [:type_id, :monitor_tags]
DEFAULTS =
{
  description: nil,
  query: nil,
  monitor_ids: [],
  thresholds: []
}.freeze

Constants inherited from Record

Record::API_LIST_INCOMPLETE, 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 inherited from Record

#diff, #tracking_id

Methods inherited from Base

#kennel_id, #name, #to_json

Methods included from SubclassTracking

#recursive_subclasses, #subclasses

Methods included from SettingsAsMethods

included, #raise_with_location

Constructor Details

#initializeSlo

Returns a new instance of Slo.



24
25
26
27
28
29
# File 'lib/kennel/models/slo.rb', line 24

def initialize(*)
  super
  if thresholds.any? { |t| t[:warning] && t[:warning].to_f <= t[:critical].to_f }
    raise ValidationError, "Threshold warning must be greater-than critical value"
  end
end

Class Method Details

.api_resourceObject



48
49
50
# File 'lib/kennel/models/slo.rb', line 48

def self.api_resource
  "slo"
end

.normalize(expected, actual) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kennel/models/slo.rb', line 62

def self.normalize(expected, actual)
  super

  # remove readonly values
  actual[:thresholds]&.each do |threshold|
    threshold.delete(:warning_display)
    threshold.delete(:target_display)
  end

  # tags come in a semi-random order and order is never updated
  expected[:tags]&.sort!
  actual[:tags].sort!

  ignore_default(expected, actual, DEFAULTS)
end

Instance Method Details

#as_jsonObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kennel/models/slo.rb', line 31

def as_json
  return @as_json if @as_json
  data = {
    name: "#{name}#{LOCK}",
    description: description,
    thresholds: thresholds,
    monitor_ids: monitor_ids,
    tags: tags.uniq,
    type: type
  }

  data[:query] = query if query
  data[:id] = id if id

  @as_json = data
end

#resolve_linked_tracking_ids(id_map) ⇒ Object



56
57
58
59
60
# File 'lib/kennel/models/slo.rb', line 56

def resolve_linked_tracking_ids(id_map)
  as_json[:monitor_ids] = as_json[:monitor_ids].map do |id|
    id.is_a?(String) ? resolve_link(id, id_map) : id
  end
end

#url(id) ⇒ Object



52
53
54
# File 'lib/kennel/models/slo.rb', line 52

def url(id)
  Utils.path_to_url "/slo?slo_id=#{id}"
end