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]
TRACKING_FIELD =
:description
DEFAULTS =
{
  description: nil,
  query: nil,
  groups: nil,
  monitor_ids: [],
  thresholds: []
}.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 inherited from Record

#add_tracking_id, api_resource_map, #diff, 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, #raise_with_location

Constructor Details

#initializeSlo

Returns a new instance of Slo.



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

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



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

def self.api_resource
  "slo"
end

.normalize(expected, actual) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kennel/models/slo.rb', line 77

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

.parse_url(url) ⇒ Object



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

def self.parse_url(url)
  url[/\/slo(\?.*slo_id=|\/edit\/)([a-z\d]{10,})(&|$)/, 2]
end

.url(id) ⇒ Object



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

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

Instance Method Details

#as_jsonObject



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

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, **args) ⇒ Object



70
71
72
73
74
75
# File 'lib/kennel/models/slo.rb', line 70

def resolve_linked_tracking_ids!(id_map, **args)
  return unless as_json[:monitor_ids] # ignore_default can remove it
  as_json[:monitor_ids] = as_json[:monitor_ids].map do |id|
    id.is_a?(String) ? (resolve_link(id, :monitor, id_map, **args) || id) : id
  end
end