Permette di scegliere 12 ore, 24 ore o la settimana nello storico invii.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -16,7 +16,7 @@ class MailingsController < ApplicationController
|
||||
@paused_mailings = @window_paused_mailings
|
||||
@completed_today = @mailings.count { |mailing| mailing.sent? && mailing.completed_at&.to_date == Time.zone.today }
|
||||
@failed_open = @mailings.sum(&:failed_count)
|
||||
@hourly_throughput = Mailings::HourlyThroughput.new(current_project)
|
||||
@hourly_throughput = Mailings::HourlyThroughput.new(current_project, range: params[:history])
|
||||
end
|
||||
|
||||
def new
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
class Mailings::HourlyThroughput
|
||||
HOUR = 1.hour
|
||||
RECENT = 15.minutes
|
||||
HISTORY_HOURS = 12
|
||||
DEFAULT_RANGE = "12h"
|
||||
RANGES = {
|
||||
"12h" => { grain: :hour, periods: 12, short: "12h", label: "ultime 12 ore" },
|
||||
"24h" => { grain: :hour, periods: 24, short: "24h", label: "ultime 24 ore" },
|
||||
"7d" => { grain: :day, periods: 7, short: "7g", label: "ultima settimana" }
|
||||
}.freeze
|
||||
|
||||
def initialize(project, now: Time.current)
|
||||
def self.normalize_range(value)
|
||||
key = value.to_s
|
||||
RANGES.key?(key) ? key : DEFAULT_RANGE
|
||||
end
|
||||
|
||||
def initialize(project, now: Time.current, range: DEFAULT_RANGE)
|
||||
@project = project
|
||||
@now = now
|
||||
@range_key = self.class.normalize_range(range)
|
||||
end
|
||||
|
||||
attr_reader :range_key
|
||||
|
||||
def range
|
||||
RANGES.fetch(@range_key)
|
||||
end
|
||||
|
||||
def sent
|
||||
@@ -24,33 +41,54 @@ 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
|
||||
def history
|
||||
@history ||= buckets_for_range
|
||||
end
|
||||
|
||||
def history_total
|
||||
hourly_history.sum { |bucket| bucket[:count] }
|
||||
history.sum { |bucket| bucket[:count] }
|
||||
end
|
||||
|
||||
def history_max
|
||||
[hourly_history.map { |bucket| bucket[:count] }.max, 1].max
|
||||
[history.map { |bucket| bucket[:count] }.max, 1].max
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def buckets_for_range
|
||||
from, step = history_origin
|
||||
keyed = recipients.sent
|
||||
.where(sent_at: from..@now)
|
||||
.pluck(:sent_at)
|
||||
.each_with_object(Hash.new(0)) do |sent_at, counts|
|
||||
counts[bucket_at(sent_at)] += 1
|
||||
end
|
||||
|
||||
range[:periods].times.map do |index|
|
||||
at = from + (step * index)
|
||||
{ at: at, count: keyed[at] || 0, label: bucket_label(at) }
|
||||
end
|
||||
end
|
||||
|
||||
def history_origin
|
||||
if range[:grain] == :day
|
||||
from = (@now.to_date - (range[:periods] - 1)).in_time_zone.beginning_of_day
|
||||
[from, 1.day]
|
||||
else
|
||||
from = (@now - (range[:periods] - 1).hours).beginning_of_hour
|
||||
[from, 1.hour]
|
||||
end
|
||||
end
|
||||
|
||||
def bucket_at(sent_at)
|
||||
time = sent_at.in_time_zone
|
||||
range[:grain] == :day ? time.beginning_of_day : time.beginning_of_hour
|
||||
end
|
||||
|
||||
def bucket_label(at)
|
||||
range[:grain] == :day ? at.strftime("%d/%m") : at.strftime("%H:%M")
|
||||
end
|
||||
|
||||
def sent_since(window)
|
||||
recipients.sent.where(sent_at: (@now - window)..@now).count
|
||||
end
|
||||
|
||||
@@ -42,16 +42,27 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="<%= card_class %> p-4">
|
||||
<div class="text-xs uppercase tracking-wide text-zinc-500">Storico invio</div>
|
||||
<div class="mt-2 flex items-end gap-0.5" style="height: 2.5rem" role="img" aria-label="Invii confermati nelle ultime 12 ore">
|
||||
<% @hourly_throughput.hourly_history.each do |bucket| %>
|
||||
<div class="flex items-center justify-between gap-1">
|
||||
<div class="text-xs uppercase tracking-wide text-zinc-500">Storico invio</div>
|
||||
<div class="inline-flex items-center gap-1">
|
||||
<% Mailings::HourlyThroughput::RANGES.each do |key, config| %>
|
||||
<% if @hourly_throughput.range_key == key %>
|
||||
<span class="rounded-md bg-zinc-900 px-1.5 py-0.5 text-[10px] font-medium leading-none text-white dark:bg-zinc-100 dark:text-zinc-900"><%= config[:short] %></span>
|
||||
<% else %>
|
||||
<%= link_to config[:short], dashboard_mailings_path(history: key), class: "rounded-md px-1.5 py-0.5 text-[10px] font-medium leading-none text-zinc-500 hover:bg-zinc-100 dark:text-zinc-400 dark:hover:bg-zinc-800" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-2 flex items-end gap-0.5" style="height: 2.5rem" role="img" aria-label="Invii confermati, <%= @hourly_throughput.range[:label] %>">
|
||||
<% @hourly_throughput.history.each do |bucket| %>
|
||||
<% pct = (bucket[:count].to_f / @hourly_throughput.history_max * 100).round %>
|
||||
<div class="flex-1 rounded <%= bucket[:count].positive? ? "bg-emerald-500" : "bg-zinc-200 dark:bg-zinc-700" %>"
|
||||
style="height: <%= bucket[:count].positive? ? [pct, 12].max : 8 %>%"
|
||||
title="<%= bucket[:at].strftime("%H:%M") %>: <%= bucket[:count] %>"></div>
|
||||
title="<%= bucket[:label] %>: <%= bucket[:count] %>"></div>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="mt-1 text-xs text-zinc-500"><%= @hourly_throughput.history_total %> inviate · ultime 12 ore</div>
|
||||
<div class="mt-1 text-xs text-zinc-500"><%= @hourly_throughput.history_total %> inviate · <%= @hourly_throughput.range[:label] %></div>
|
||||
</div>
|
||||
<div class="<%= card_class %> p-4">
|
||||
<div class="text-xs uppercase tracking-wide text-zinc-500">In pausa (fascia)</div>
|
||||
|
||||
Reference in New Issue
Block a user