Prune propagation scores per valid_time to avoid massive single DELETE
This commit is contained in:
parent
33490d37d0
commit
8d84bd2d18
1 changed files with 17 additions and 4 deletions
|
|
@ -151,13 +151,26 @@ defmodule Microwaveprop.Propagation do
|
||||||
|
|
||||||
@doc "Remove scores with valid_times older than 1 hour before the earliest current forecast."
|
@doc "Remove scores with valid_times older than 1 hour before the earliest current forecast."
|
||||||
def prune_old_scores do
|
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)
|
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
|
total =
|
||||||
Logger.info("PropagationScores: pruned #{deleted} old scores (valid_time < #{cutoff})")
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue