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, separate_connection: true) ⇒ Object

Dispatch the message template to administrators of the org.

Parameters:

  • message_template (Webhookdb::Message::Template)
  • separate_connection (true, false) (defaults to: true)

    If true, send the alert on a separate connection. See Webhookdb::Idempotency. Defaults to true since this is an alert method and we don’t want it to error accidentally, if the code is called from an unexpected situation.



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

def dispatch_alert(message_template, separate_connection: true)
  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|
    idem = Webhookdb::Idempotency.every(Webhookdb::Organization::Alerting.interval)
    idem = idem.using_seperate_connection if separate_connection
    idem.under_key("orgalert-#{signature}-#{c.id}") 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