Prune propagation scores per valid_time to avoid massive single DELETE

This commit is contained in:
Graham McIntire 2026-04-03 10:46:17 -05:00
parent 33490d37d0
commit 8d84bd2d18
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -151,13 +151,26 @@ defmodule Microwaveprop.Propagation do
@doc "Remove scores with valid_times older than 1 hour before the earliest current forecast."
def prune_old_scores do
# Keep all forecast hours from the latest run + 1 hour buffer for the previous run
cutoff = DateTime.add(DateTime.utc_now(), -2, :hour)
{deleted, _} = Repo.delete_all(from(gs in GridScore, where: gs.valid_time < ^cutoff))
# Get distinct old valid_times and delete one at a time to avoid massive single DELETEs
old_times =
Repo.all(
from(gs in GridScore,
where: gs.valid_time < ^cutoff,
select: gs.valid_time,
distinct: gs.valid_time
)
)
if deleted > 0 do
Logger.info("PropagationScores: pruned #{deleted} old scores (valid_time < #{cutoff})")
total =
Enum.reduce(old_times, 0, fn vt, acc ->
{deleted, _} = Repo.delete_all(from(gs in GridScore, where: gs.valid_time == ^vt))
acc + deleted
end)
if total > 0 do
Logger.info("PropagationScores: pruned #{total} old scores across #{length(old_times)} valid_times")
end
end