Class: Kennel::Models::Slo

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

Constant Summary collapse

READONLY_ATTRIBUTES =
[
  *superclass::READONLY_ATTRIBUTES,
  :type_id, :monitor_tags
].freeze
TRACKING_FIELD =
:description
DEFAULTS =
{
  description: nil,
  query: nil,
  groups: nil,
  monitor_ids: [],
  thresholds: [],
  primary: nil
}.freeze

Constants inherited from Record

Record::ALLOWED_KENNEL_ID_CHARS, Record::ALLOWED_KENNEL_ID_FULL, Record::ALLOWED_KENNEL_ID_REGEX, Record::ALLOWED_KENNEL_ID_SEGMENT, Record::LOCK, Record::MARKER_TEXT, Record::TITLE_FIELDS, Record::TRACKING_FIELDS

Constants included from OptionalValidations

OptionalValidations::UNIGNORABLE, OptionalValidations::UNUSED_IGNORES

Constants inherited from Base

Base::SETTING_OVERRIDABLE_METHODS

Constants included from SettingsAsMethods

SettingsAsMethods::AS_PROCS, SettingsAsMethods::SETTING_OVERRIDABLE_METHODS

Instance Attribute Summary

Attributes inherited from Record

#as_json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#add_tracking_id, #allowed_update_error, api_resource_map, #build, #diff, #initialize, parse_any_url, parse_tracking_id, #remove_tracking_id, remove_tracking_id, #safe_tracking_id, #tracking_id

Methods included from OptionalValidations

filter_validation_errors, included, #initialize, #invalid!, valid?

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



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

def self.api_resource
  "slo"
end

.normalize(expected, actual) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/kennel/models/slo.rb', line 86

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!

  # do not show these in the diff if we automatically pick the primary timeframe,
  # or we will have a permanent `something -> nil` diff
  unless expected[:timeframe]
    [:timeframe, :warning_threshold, :target_threshold].each { |k| actual.delete k }
  end

  # datadog always sets this, but we only set it for time_slice, so discard it for everything else to avoid diff
  if expected[:type] != "time_slice"
    actual.delete :sli_specification
  end

  ignore_default(expected, actual, DEFAULTS)
end

.parse_url(url) ⇒ Object



75
76
77
# File 'lib/kennel/models/slo.rb', line 75

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

.url(id) ⇒ Object



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

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

Instance Method Details

#build_jsonObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kennel/models/slo.rb', line 33

def build_json
  data = super.merge(
    name: "#{name}#{LOCK}",
    description: description,
    thresholds: thresholds,
    monitor_ids: monitor_ids,
    tags: tags,
    type: type
  )

  # add top level timeframe and threshold settings based on `primary`
  if (p = primary)
    data[:timeframe] = p
    threshold =
      thresholds.detect { |t| t[:timeframe] == p } ||
      raise(ArgumentError, "#{tracking_id} unable to find threshold with timeframe #{p}")
    data[:warning_threshold] = threshold[:warning]
    data[:target_threshold] = threshold[:target]
  end

  # TODO: if user set sli_specification but we don't use it we should raise, but sadly we can't detect that easily
  if type == "time_slice"
    data[:sli_specification] = sli_specification
  elsif (v = query)
    data[:query] = v
  end

  if (v = groups)
    data[:groups] = v
  end

  data
end

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



79
80
81
82
83
84
# File 'lib/kennel/models/slo.rb', line 79

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