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,
  groups: 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.



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

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



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

def self.api_resource
  "slo"
end

.normalize(expected, actual) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kennel/models/slo.rb', line 71

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



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

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
  }

  if v = query
    data[:query] = v
  end
  if v = id
    data[:id] = v
  end
  if v = groups
    data[:groups] = v
  end

  @as_json = data
end

#resolve_linked_tracking_ids(id_map) ⇒ Object



65
66
67
68
69
# File 'lib/kennel/models/slo.rb', line 65

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, :monitor, id_map) : id
  end
end

#url(id) ⇒ Object



61
62
63
# File 'lib/kennel/models/slo.rb', line 61

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