fix: deduplicate Preseem sync entries in activity feed

Group sync entries by minute to show only one entry per minute instead
of multiple duplicate entries with the same timestamp. This aggregates:
- Total records synced in that minute
- Average sync duration
- Most recent status

This keeps the activity feed informative while reducing noise from
rapid syncs happening within the same minute.
This commit is contained in:
Graham McIntire 2026-03-04 15:18:03 -06:00
parent 74ad58bada
commit 89949fdfd6

View file

@ -296,19 +296,19 @@ defmodule Towerops.ActivityFeed do
# --- Preseem Syncs ---
defp fetch_source(:sync, org_id, after_dt, before_dt, limit) do
# Group by minute to show only one sync entry per minute (deduplicate rapid syncs)
query =
from(sl in SyncLog,
where: sl.organization_id == ^org_id,
# Only show failed syncs in activity feed, not successful ones
where: sl.status != "success",
select: %{
id: sl.id,
timestamp: sl.inserted_at,
status: sl.status,
records_synced: sl.records_synced,
duration_ms: sl.duration_ms
id: max(sl.id),
timestamp: fragment("date_trunc('minute', ?)", sl.inserted_at),
status: fragment("array_agg(? ORDER BY ? DESC)[1]", sl.status, sl.inserted_at),
records_synced: sum(sl.records_synced),
duration_ms: avg(sl.duration_ms)
},
order_by: [desc: sl.inserted_at],
group_by: fragment("date_trunc('minute', ?)", sl.inserted_at),
order_by: [desc: fragment("date_trunc('minute', ?)", sl.inserted_at)],
limit: ^limit
)