Class: Webhookdb::Organization::Alerting

Inherits:
Object
  • Object
show all
Includes:
Appydays::Configurable
Defined in:
lib/webhookdb/organization/alerting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(org) ⇒ Alerting

Returns a new instance of Alerting.



20
21
22
# File 'lib/webhookdb/organization/alerting.rb', line 20

def initialize(org)
  @org = org
end

Instance Attribute Details

#orgObject (readonly)

Returns the value of attribute org.



18
19
20
# File 'lib/webhookdb/organization/alerting.rb', line 18

def org
  @org
end

Instance Method Details

#dispatch_alert(message_template) ⇒ Object

Dispatch the message template to administrators of the org.

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webhookdb/organization/alerting.rb', line 26

def dispatch_alert(message_template)
  unless message_template.respond_to?(:signature)
    raise Webhookdb::InvalidPrecondition,
          "message template #{message_template.template_name} must define a #signature method, " \
          "which is a unique identity for this error type, used for grouping and idempotency"
  end
  signature = message_template.signature
  max_alerts_per_customer_per_day = Webhookdb::Organization::Alerting.max_alerts_per_customer_per_day
  yesterday = Time.now - 24.hours
  self.org.admin_customers.each do |c|
    idemkey = "orgalert-#{signature}-#{c.id}"
    Webhookdb::Idempotency.every(Webhookdb::Organization::Alerting.interval).under_key(idemkey) do
      sent_last_day = Webhookdb::Message::Delivery.
        where(template: message_template.full_template_name, recipient: c).
        where { created_at > yesterday }.
        limit(max_alerts_per_customer_per_day).
        count
      next unless sent_last_day < max_alerts_per_customer_per_day
      message_template.dispatch_email(c)
    end
  end
end