diff --git a/app/services/mailings/hourly_throughput.rb b/app/services/mailings/hourly_throughput.rb index 94040d9..f9aa9ca 100644 --- a/app/services/mailings/hourly_throughput.rb +++ b/app/services/mailings/hourly_throughput.rb @@ -1,6 +1,7 @@ class Mailings::HourlyThroughput HOUR = 1.hour RECENT = 15.minutes + HISTORY_HOURS = 12 def initialize(project, now: Time.current) @project = project @@ -23,6 +24,31 @@ class Mailings::HourlyThroughput sent + failed end + def hourly_history + @hourly_history ||= begin + from = (@now - (HISTORY_HOURS - 1).hours).beginning_of_hour + keyed = recipients.sent + .where(sent_at: from..@now) + .pluck(:sent_at) + .each_with_object(Hash.new(0)) do |sent_at, counts| + counts[sent_at.in_time_zone.beginning_of_hour] += 1 + end + + HISTORY_HOURS.times.map do |index| + at = from + index.hours + { at: at, count: keyed[at] || 0 } + end + end + end + + def history_total + hourly_history.sum { |bucket| bucket[:count] } + end + + def history_max + [hourly_history.map { |bucket| bucket[:count] }.max, 1].max + end + private def sent_since(window) diff --git a/app/views/mailings/dashboard.html.erb b/app/views/mailings/dashboard.html.erb index 977465e..9438549 100644 --- a/app/views/mailings/dashboard.html.erb +++ b/app/views/mailings/dashboard.html.erb @@ -26,7 +26,7 @@ <% end %> -