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,
  sli_specification: 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

#abstract_class?, #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



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

def self.api_resource
  "slo"
end

.normalize(expected, actual) ⇒ Object



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
112
113
114
115
116
# File 'lib/kennel/models/slo.rb', line 87

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

  # discard deprecated query which stays in datadog forever when we are trying to set sli_specification
  # (downgrading to query by setting sli_specification=nil is not supported in the api)
  actual.delete :query if expected[:sli_specification]

  # user set query so let's not worry about sli_specification even if this might be hiding bugs
  # ideally we'd validate and tell the user that this will have no effect (see importer logic)
  # but I'm not confident this will always be right
  actual.delete :sli_specification if expected[:query]

  ignore_default(expected, actual, DEFAULTS)
end

.parse_url(url) ⇒ Object



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

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



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

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

Instance Method Details

#build_jsonObject



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
66
# File 'lib/kennel/models/slo.rb', line 35

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

  if (v = sli_specification)
    data[:sli_specification] = v
  elsif (v = query)
    data[:query] = v
  end

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

  data
end

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



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

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