diff --git a/lib/microwaveprop/weather.ex b/lib/microwaveprop/weather.ex index 4bd393e7..b89b23f2 100644 --- a/lib/microwaveprop/weather.ex +++ b/lib/microwaveprop/weather.ex @@ -256,6 +256,33 @@ defmodule Microwaveprop.Weather do {Float.round(lat / 1.0, 2), Float.round(lon / 1.0, 2)} end + @doc """ + Delete HRRR grid profiles older than 48 hours that sit on the 0.125° grid. + Preserves QSO-linked profiles which are at arbitrary lat/lon positions. + """ + def prune_old_grid_profiles do + cutoff = DateTime.add(DateTime.utc_now(), -48, :hour) + step = 0.125 + + {deleted, _} = + Repo.delete_all( + from(h in HrrrProfile, + where: + h.valid_time < ^cutoff and + fragment("? = round(?::numeric / ? ) * ?", h.lat, h.lat, ^step, ^step) and + fragment("? = round(?::numeric / ? ) * ?", h.lon, h.lon, ^step, ^step) + ) + ) + + if deleted > 0 do + require Logger + + Logger.info("Pruned #{deleted} old HRRR grid profiles (keeping QSO profiles)") + end + + deleted + end + def round_to_iemre_grid(lat, lon) do {Float.round(lat * 8) / 8, Float.round(lon * 8) / 8} end diff --git a/lib/microwaveprop/workers/propagation_grid_worker.ex b/lib/microwaveprop/workers/propagation_grid_worker.ex index 64aa0d5e..963c9942 100644 --- a/lib/microwaveprop/workers/propagation_grid_worker.ex +++ b/lib/microwaveprop/workers/propagation_grid_worker.ex @@ -53,6 +53,8 @@ defmodule Microwaveprop.Workers.PropagationGridWorker do Logger.info("PropagationGrid: resuming backfill queues") Enum.each(@pause_queues, &Oban.resume_queue(queue: &1)) + Weather.prune_old_grid_profiles() + result end