Class: Kennel::Models::Record

Inherits:
Base
  • Object
show all
Defined in:
lib/kennel/models/record.rb

Direct Known Subclasses

Dashboard, Monitor, Slo, SyntheticTest

Constant Summary collapse

MARKER_TEXT =

Apart from if you just don’t like the default for some reason, overriding MARKER_TEXT allows for namespacing within the same Datadog account. If you run one Kennel setup with marker text A and another with marker text B (assuming that A isn’t a substring of B and vice versa), then the two Kennel setups will operate independently of each other, not trampling over each other’s objects.

This could be useful for allowing multiple products / projects / teams to share a Datadog account but otherwise largely operate independently of each other. In particular, it can be useful for running a “dev” or “staging” instance of Kennel in the same account as, but mostly isolated from, a “production” instance.

ENV.fetch("KENNEL_MARKER_TEXT", "Managed by kennel")
LOCK =
"\u{1F512}"
TRACKING_FIELDS =
[:message, :description].freeze
READONLY_ATTRIBUTES =
[
  :deleted, :id, :created, :created_at, :creator, :org_id, :modified, :modified_at,
  :klass, :tracking_id # added by syncer.rb
].freeze
ALLOWED_KENNEL_ID_CHARS =
"a-zA-Z_\\d.-"
ALLOWED_KENNEL_ID_FULL =
"[#{ALLOWED_KENNEL_ID_CHARS}]+:[#{ALLOWED_KENNEL_ID_CHARS}]+"
ALLOWED_KENNEL_ID_REGEX =
/\A#{ALLOWED_KENNEL_ID_FULL}\z/.freeze

Constants inherited from Base

Base::SETTING_OVERRIDABLE_METHODS

Constants included from SettingsAsMethods

SettingsAsMethods::AS_PROCS, SettingsAsMethods::SETTING_OVERRIDABLE_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#kennel_id, #name, #to_json

Methods included from SubclassTracking

#recursive_subclasses, #subclasses

Methods included from SettingsAsMethods

included

Constructor Details

#initialize(project, *args) ⇒ Record

Returns a new instance of Record.

Raises:

  • (ArgumentError)


77
78
79
80
81
# File 'lib/kennel/models/record.rb', line 77

def initialize(project, *args)
  raise ArgumentError, "First argument must be a project, not #{project.class}" unless project.is_a?(Project)
  @project = project
  super(*args)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

Class Method Details

.api_resource_mapObject



42
43
44
# File 'lib/kennel/models/record.rb', line 42

def api_resource_map
  subclasses.map { |s| [s.api_resource, s] }.to_h
end

.parse_any_url(url) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/kennel/models/record.rb', line 34

def parse_any_url(url)
  subclasses.detect do |s|
    if id = s.parse_url(url)
      break s.api_resource, id
    end
  end
end

.parse_tracking_id(a) ⇒ Object



46
47
48
# File 'lib/kennel/models/record.rb', line 46

def parse_tracking_id(a)
  a[self::TRACKING_FIELD].to_s[/-- #{Regexp.escape(MARKER_TEXT)} (#{ALLOWED_KENNEL_ID_FULL})/, 1]
end

.remove_tracking_id(a) ⇒ Object

TODO: combine with parse into a single method or a single regex



51
52
53
54
55
56
# File 'lib/kennel/models/record.rb', line 51

def remove_tracking_id(a)
  value = a[self::TRACKING_FIELD]
  a[self::TRACKING_FIELD] =
    value.dup.sub!(/\n?-- #{Regexp.escape(MARKER_TEXT)} .*/, "") ||
    raise("did not find tracking id in #{value}")
end

Instance Method Details

#add_tracking_idObject



108
109
110
111
112
113
114
115
116
# File 'lib/kennel/models/record.rb', line 108

def add_tracking_id
  json = as_json
  if self.class.parse_tracking_id(json)
    invalid! "remove \"-- #{MARKER_TEXT}\" line it from #{self.class::TRACKING_FIELD} to copy a resource"
  end
  json[self.class::TRACKING_FIELD] =
    "#{json[self.class::TRACKING_FIELD]}\n" \
    "-- #{MARKER_TEXT} #{tracking_id} in #{project.class.file_location}, do not modify manually".lstrip
end

#diff(actual) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kennel/models/record.rb', line 83

def diff(actual)
  expected = as_json
  expected.delete(:id)

  self.class.send(:normalize, expected, actual)

  # strict: ignore Integer vs Float
  # similarity: show diff when not 100% similar
  # use_lcs: saner output
  Hashdiff.diff(actual, expected, use_lcs: false, strict: false, similarity: 1)
end

#remove_tracking_idObject



118
119
120
# File 'lib/kennel/models/record.rb', line 118

def remove_tracking_id
  self.class.remove_tracking_id(as_json)
end

#resolve_linked_tracking_ids!Object



105
106
# File 'lib/kennel/models/record.rb', line 105

def resolve_linked_tracking_ids!(*)
end

#tracking_idObject



95
96
97
98
99
100
101
102
103
# File 'lib/kennel/models/record.rb', line 95

def tracking_id
  @tracking_id ||= begin
    id = "#{project.kennel_id}:#{kennel_id}"
    unless id.match?(ALLOWED_KENNEL_ID_REGEX) # <-> parse_tracking_id
      raise ValidationError, "#{id} must match #{ALLOWED_KENNEL_ID_REGEX}"
    end
    id
  end
end

#validate_update!Object



122
123
# File 'lib/kennel/models/record.rb', line 122

def validate_update!(*)
end