Class: Flapjack::Data::Medium

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

Constant Summary collapse

TRANSPORTS =
[
  'email',
  'jabber',
  'pagerduty',
  'sms',
  'slack',
  'sms_twilio',
  'sms_nexmo',
  'sms_aspsms',
  'sns'
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.destroy_state(medium_id, st_id) ⇒ Object



141
142
143
144
145
# File 'lib/flapjack/data/medium.rb', line 141

def self.destroy_state(medium_id, st_id)
  # won't be deleted if still referenced elsewhere -- see the State
  # before_destroy callback
  Flapjack::Data::State.intersect(:id => st_id).destroy_all
end

.jsonapi_associationsObject



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/flapjack/data/medium.rb', line 385

def self.jsonapi_associations
  unless instance_variable_defined?('@jsonapi_associations')
    @jsonapi_associations = {
      :alerting_checks => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'check',
        :klass => Flapjack::Data::Check,
        :callback_classes => [
          Flapjack::Data::Contact,
          Flapjack::Data::Rule,
          Flapjack::Data::ScheduledMaintenance
        ],
        :descriptions => {
          :get => "Returns all checks that have alerted through a medium (that are still failing."
        }
      ),
      :contact => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :post => true, :get => true,
        :number => :singular, :link => true, :includable => true,
        :descriptions => {
          :post => "Set a contact for a medium during medium creation (required).",
          :get => "Get the contact a medium belongs to."
        }
      ),
      :rules => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :post => true, :get => true, :patch => true, :delete => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :post => "Associate this medium with rules on medium creation.",
          :get => "Get the rules this medium is associated with.",
          :patch => "Update the rules this medium is associated with.",
          :delete => "Delete associations between this medium and rules."
        }

      )
    }
    populate_association_data(@jsonapi_associations)
  end
  @jsonapi_associations
end

.jsonapi_methodsObject



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/flapjack/data/medium.rb', line 347

def self.jsonapi_methods
  @jsonapi_methods ||= {
    :post => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:transport, :address, :interval, :rollup_threshold,
                      :pagerduty_subdomain, :pagerduty_token,
                      :pagerduty_ack_duration],
      :descriptions => {
        :singular => "Create a media record.",
        :multiple => "Create media records."
      }
    ),
    :get => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:transport, :address, :interval, :rollup_threshold,
                      :pagerduty_subdomain, :pagerduty_token,
                      :pagerduty_ack_duration],
      :descriptions => {
        :singular => "Returns data for a media record.",
        :multiple => "Returns data for media records."
      }
    ),
    :patch => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:transport, :address, :interval, :rollup_threshold,
                      :pagerduty_subdomain, :pagerduty_token,
                      :pagerduty_ack_duration],
      :descriptions => {
        :singular => "Update a media record.",
        :multiple => "Update media records."
      }
    ),
    :delete => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :descriptions => {
        :singular => "Delete a media record.",
        :multiple => "Delete media records."
      }
    )
  }
end

.swagger_included_classesObject



333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/flapjack/data/medium.rb', line 333

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

#alerting_checks(opts = {}) ⇒ Object

this can be called from the API (with no args) or from notifier.rb (which will pass an effective time)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flapjack/data/medium.rb', line 64

def alerting_checks(opts = {})
  time = opts[:time] || Time.now
  init_scope = Flapjack::Data::Check.intersect(:enabled => true, :alertable => true)
  ret = checks(:initial_scope => init_scope,
               :time => Time.now)

  return Flapjack::Data::Check.empty if ret.empty?

  start_range = Zermelo::Filters::IndexRange.new(nil, time, :by_score => true)
  end_range   = Zermelo::Filters::IndexRange.new(time, nil, :by_score => true)

  sched_maint_check_ids = Flapjack::Data::ScheduledMaintenance.
    intersect(:start_time => start_range, :end_time => end_range).
    associated_ids_for(:check).values

  ret = ret.diff(:id => sched_maint_check_ids) unless sched_maint_check_ids.empty?

  unsched_maint_check_ids = Flapjack::Data::UnscheduledMaintenance.
    intersect(:start_time => start_range, :end_time => end_range).
    associated_ids_for(:check).values

  ret = ret.diff(:id => unsched_maint_check_ids) unless unsched_maint_check_ids.empty?
  ret
end

#checks(opts = {}) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/flapjack/data/medium.rb', line 89

def checks(opts = {})
  time_zone = self.contact.time_zone
  time = opts[:time] || Time.now
  init_scope = opts[:initial_scope] || Flapjack::Data::Check.intersect(:enabled => true)

  # TODO maybe fold time validation into 'matching_checks'
  global_rejector_ids = self.rules.intersect(:enabled => true, :blackhole => true,
    :strategy => 'global').select {|rejector|

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

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

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

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

  acceptors = self.rules.intersect(:enabled => true, :blackhole => false).select {|acceptor|
    acceptor.is_occurring_at?(time, time_zone)
  }

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

  ret = init_scope

  if acceptors.none? {|a| 'global'.eql?(a.strategy) }
    # if no global acceptor, scope by tags for acceptors
    acceptor_checks = Flapjack::Data::Rule.matching_checks(acceptors.map(&:id))
    ret = ret.intersect(:id => acceptor_checks)
  end

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

  ret
end