Class: Flapjack::Data::Contact

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, Extensions::Associations, Extensions::ShortName, Swagger::Blocks, Zermelo::Records::RedisSet
Defined in:
lib/flapjack/data/contact.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.jsonapi_associationsObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/flapjack/data/contact.rb', line 258

def self.jsonapi_associations
  unless instance_variable_defined?('@jsonapi_associations')
    @jsonapi_associations = {
      :checks => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'check',
        :klass => Flapjack::Data::Check,
        :descriptions => {
          :get => "Returns checks which this contact's notification " \
                  "rules allow it to receive notifications."
        }
      ),
      :media => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns media belonging to the contact."
        }
      ),
      :rules => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns rules belonging to the contact."
        }
      ),
      :tags => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :post => true, :get => true, :patch => true, :delete => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :post => "Associate tags with this contact.",
          :get => "Returns all tags linked to this contact.",
          :patch => "Update the tags associated with this contact.",
          :delete => "Delete associations between tags and this contact."
        }
      )
    }
    populate_association_data(@jsonapi_associations)
  end
  @jsonapi_associations
end

.jsonapi_methodsObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/flapjack/data/contact.rb', line 226

def self.jsonapi_methods
  @jsonapi_methods ||= {
    :post => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Create a contact.",
        :multiple => "Create contacts."
      }
    ),
    :get => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Get data for a contact.",
        :multiple => "Get data for multiple contacts."
      }
    ),
    :patch => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Update a contact record.",
        :multiple => "Update contact records."
      }
    ),
    :delete => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :descriptions => {
        :singular => "Delete a contact.",
        :multiple => "Delete contacts."
      }
    ),
  }
end

.swagger_included_classesObject



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/flapjack/data/contact.rb', line 212

def self.swagger_included_classes
  # hack -- hardcoding for now
  [
    Flapjack::Data::Check,
    Flapjack::Data::Contact,
    Flapjack::Data::Medium,
    Flapjack::Data::Rule,
    Flapjack::Data::ScheduledMaintenance,
    Flapjack::Data::State,
    Flapjack::Data::Tag,
    Flapjack::Data::UnscheduledMaintenance
  ]
end

Instance Method Details

#checksObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/flapjack/data/contact.rb', line 63

def checks
  time = Time.now

  global_acceptors = self.rules.intersect(:enabled => true,
    :blackhole => false, :strategy => 'global')

  global_rejector_ids = self.rules.intersect(:enabled => true,
    :blackhole => true, :strategy => 'global').select {|rejector|

    rejector.is_occurring_at?(time, timezone)
  }.map(&:id)

  # global blackhole
  return Flapjack::Data::Check.empty unless global_rejector_ids.empty?

  tag_rejector_ids = self.rules.intersect(:enabled => true,
    :blackhole => true, :strategy => ['any_tag', 'all_tags', 'no_tag']).select {|rejector|

    rejector.is_occurring_at?(time, timezone)
  }.map(&:id)

  tag_acceptors = self.rules.intersect(:enabled => true, :blackhole => false,
    :strategy => ['any_tag', 'all_tags', 'no_tag']).select {|acceptor|

    acceptor.is_occurring_at?(time, timezone)
  }

  # no positives
  return Flapjack::Data::Check.empty if tag_acceptors.empty?


  # initial scope is all enabled
  linked_checks = Flapjack::Data::Check.intersect(:enabled => true)

  if global_acceptors.empty?
    # if no global acceptor, scope by matching tags
    tag_acceptor_checks = Flapjack::Data::Rule.matching_checks(tag_acceptors.map(&:id))
    linked_checks = linked_checks.intersect(:id => tag_acceptor_checks)
  end

  # then exclude by checks with tags matching rejector, if any
  tag_rejector_checks = Flapjack::Data::Rule.matching_checks(tag_rejector_ids)
  unless tag_rejector_checks.empty?
    linked_checks = linked_checks.diff(:id => tag_rejector_checks)
  end

  linked_checks
end

#remove_child_recordsObject



53
54
55
56
# File 'lib/flapjack/data/contact.rb', line 53

def remove_child_records
  self.media.each  {|medium| medium.destroy }
  self.rules.each  {|rule|   rule.destroy   }
end

#time_zoneObject



58
59
60
61
# File 'lib/flapjack/data/contact.rb', line 58

def time_zone
  return nil if self.timezone.nil?
  ActiveSupport::TimeZone[self.timezone]
end