36 lines
622 B
Ruby
36 lines
622 B
Ruby
class Mailings::HourlyThroughput
|
|
HOUR = 1.hour
|
|
RECENT = 15.minutes
|
|
|
|
def initialize(project, now: Time.current)
|
|
@project = project
|
|
@now = now
|
|
end
|
|
|
|
def sent
|
|
@sent ||= sent_since(HOUR)
|
|
end
|
|
|
|
def recent_sent
|
|
@recent_sent ||= sent_since(RECENT)
|
|
end
|
|
|
|
def failed
|
|
@failed ||= recipients.failed.where(updated_at: (@now - HOUR)..@now).count
|
|
end
|
|
|
|
def drained
|
|
sent + failed
|
|
end
|
|
|
|
private
|
|
|
|
def sent_since(window)
|
|
recipients.sent.where(sent_at: (@now - window)..@now).count
|
|
end
|
|
|
|
def recipients
|
|
MailingRecipient.joins(:mailing).merge(Mailing.for_project(@project))
|
|
end
|
|
end
|