prop/test/microwaveprop/workers/pskr_calibration_worker_test.exs
Graham McIntire b08fc5e449
feat(pskr): rolling-window calibration sampling, every 5 min
Old design fired hourly at HH:25 and built one whole hour of cells
in a single batch. During PSKR peaks (50k+ spots/hour) that batch
held the DB pool long enough to look like a backlog, and a pod
restart between fires lost a full hour because the next cron only
caught the most-recent-prev hour.

- Default window is now current hour + 1 prior. Every 5-min fire
  spreads work across the in-flight hour and always covers the
  boundary without waiting for the next cron tick.
- lookback_hours arg widens the window for catch-up
  (e.g. {"lookback_hours": 24} sweeps the last day after an
  outage).
- Unique period 3600 → 240 so consecutive 5-min cron fires actually
  slot in.

UPSERT is already idempotent so the ~12 redundant passes per hour
cost only the read+merge. Latency from spot ingestion to sample
creation drops from up-to-60min to ≤5min.
2026-05-04 16:46:59 -05:00

101 lines
3.4 KiB
Elixir

defmodule Microwaveprop.Workers.PskrCalibrationWorkerTest do
use Microwaveprop.DataCase, async: true
use Oban.Testing, repo: Microwaveprop.Repo
alias Microwaveprop.Pskr.CalibrationSample
alias Microwaveprop.Pskr.SpotHourly
alias Microwaveprop.Repo
alias Microwaveprop.Workers.PskrCalibrationWorker
defp insert_spot!(hour_utc, opts \\ []) do
sender_grid = Keyword.get(opts, :sender_grid, "EM12KL")
midpoint_lat = Keyword.get(opts, :midpoint_lat, 33.0)
midpoint_lon = Keyword.get(opts, :midpoint_lon, -97.0)
{:ok, _} =
%SpotHourly{}
|> SpotHourly.changeset(%{
hour_utc: hour_utc,
band: "10000",
sender_grid: sender_grid,
receiver_grid: "DM43ST",
spot_count: 1,
midpoint_lat: midpoint_lat,
midpoint_lon: midpoint_lon,
distance_km: 200.0,
modes: ["FT8"]
})
|> Repo.insert()
end
defp truncate_to_hour(dt), do: %{dt | minute: 0, second: 0, microsecond: {0, 0}}
describe "perform/1 with explicit hour_utc" do
test "samples just that hour" do
hour = ~U[2026-05-04 18:00:00Z]
insert_spot!(hour)
assert :ok =
perform_job(PskrCalibrationWorker, %{"hour_utc" => DateTime.to_iso8601(hour)})
[sample] = Repo.all(CalibrationSample)
assert sample.hour_utc == hour
end
test "succeeds when the hour has no spots" do
assert :ok =
perform_job(PskrCalibrationWorker, %{"hour_utc" => "2026-05-04T03:00:00Z"})
assert Repo.aggregate(CalibrationSample, :count) == 0
end
end
describe "perform/1 default rolling window" do
test "samples both the current hour and the previous hour" do
# Frequent processing means a single fire must cover the hour
# boundary: spots in the just-ended hour and spots in the
# in-progress hour both get sample rows. Otherwise spots that
# arrive in the last few minutes of an hour wait up to a full
# cron interval before they're sampled.
now = truncate_to_hour(DateTime.utc_now())
previous = DateTime.add(now, -3600, :second)
insert_spot!(now)
insert_spot!(previous, sender_grid: "EM10AA")
assert :ok = perform_job(PskrCalibrationWorker, %{})
hours = CalibrationSample |> Repo.all() |> Enum.map(& &1.hour_utc) |> Enum.sort()
assert hours == [previous, now]
end
test "is idempotent — re-running merges into the same rows" do
now = truncate_to_hour(DateTime.utc_now())
insert_spot!(now)
assert :ok = perform_job(PskrCalibrationWorker, %{})
first_count = Repo.aggregate(CalibrationSample, :count)
assert :ok = perform_job(PskrCalibrationWorker, %{})
assert Repo.aggregate(CalibrationSample, :count) == first_count
end
end
describe "perform/1 with explicit lookback_hours" do
test "extends the rolling window to backfill recent gaps" do
# Operator sets lookback_hours=3 to recover from a short pod
# outage. The worker fires once and produces samples for the
# current hour plus the three before it.
now = truncate_to_hour(DateTime.utc_now())
Enum.each(0..3, fn offset ->
hour = DateTime.add(now, -offset * 3600, :second)
insert_spot!(hour, sender_grid: "EM1#{offset}AA")
end)
assert :ok = perform_job(PskrCalibrationWorker, %{"lookback_hours" => 3})
assert Repo.aggregate(CalibrationSample, :count) == 4
end
end
end