prop/lib/microwaveprop/workers/radar_frame_worker.ex
Graham McIntire b6b5945002
feat(radar): batch per NEXRAD frame instead of per contact
CommonVolumeRadarWorker was enqueuing one job per contact — every
job fetched + decoded the ~5 MB n0q PNG for its own 5-min frame.
At current backlog depth that's 17,588 contacts across just 1,747
distinct 5-min frames, i.e. ~10 contacts per frame paying for the
same PNG decode over and over.

New RadarFrameWorker takes a batch of contact_ids sharing one frame,
fetches + decodes the frame ONCE, then walks the in-memory pixel
buffer per contact. build_radar_jobs/1 in ContactWeatherEnqueueWorker
now groups the input contacts by their rounded 5-min timestamp and
emits one RadarFrameWorker job per frame instead of one
CommonVolumeRadarWorker per contact.

process_frame/4 is public so the same code path is used both by
perform/1 (production fetch) and tests (pre-decoded pixel buffer);
keeps the happy-path test hermetic without hand-crafting a PNG.

CommonVolumeRadarWorker stays in the tree — still used from the
single-contact submit path where batching is pointless and the
aggregate_stats/5 pure helper is a dependency of the new batched
worker.

Expected impact on the backfill: ~10x fewer fetch + decode cycles,
so the 17k queued-contact backlog drains in minutes instead of ~50.
2026-04-20 12:12:52 -05:00

119 lines
3.8 KiB
Elixir

defmodule Microwaveprop.Workers.RadarFrameWorker do
@moduledoc """
Batched NEXRAD common-volume radar enrichment.
Each job processes every contact whose QSO timestamp rounds to the
same 5-minute NEXRAD frame — fetch + decode happens *once* per job
and the decoded pixel buffer is walked per contact. This collapses
the 1-job-per-contact backfill pattern (`CommonVolumeRadarWorker`)
down by roughly 10x at current backlog depth (17k contacts / 1.7k
distinct frames).
`CommonVolumeRadarWorker` stays around for the submit-time single-
contact path where batching is pointless. Both workers end in the
same state transitions so they can coexist safely — a contact
marked `:complete` by one is a no-op for the other.
"""
use Oban.Worker,
queue: :radar,
max_attempts: 3
import Ecto.Query
alias Microwaveprop.Radio.Contact
alias Microwaveprop.Radio.ContactCommonVolumeRadar
alias Microwaveprop.Repo
alias Microwaveprop.Weather.NexradClient
alias Microwaveprop.Workers.CommonVolumeRadarWorker
require Logger
@terminal_statuses [:complete, :unavailable]
@impl Oban.Worker
def perform(%Oban.Job{args: %{"frame_ts" => frame_iso, "contact_ids" => contact_ids}}) do
{:ok, frame_ts, _} = DateTime.from_iso8601(frame_iso)
rounded = NexradClient.round_to_5min(frame_ts)
contacts =
Contact
|> where([c], c.id in ^contact_ids)
|> where([c], c.radar_status not in ^@terminal_statuses)
|> Repo.all()
case contacts do
[] ->
:ok
contacts ->
run_batch(contacts, rounded)
end
end
defp run_batch(contacts, rounded) do
case NexradClient.fetch_decoded_frame(rounded) do
{:ok, pixels, width} ->
process_frame(contacts, rounded, pixels, width)
{:error, reason} ->
Enum.each(contacts, &mark_status(&1, :unavailable))
Logger.info(
"RadarFrameWorker: no frame for #{DateTime.to_iso8601(rounded)} (#{length(contacts)} contacts): #{inspect(reason)}"
)
end
:ok
end
@doc """
Process a batch of contacts against an already-decoded frame. Exposed
for tests and for any caller that has the pixel buffer in hand.
"""
@spec process_frame([Contact.t()], DateTime.t(), binary(), pos_integer()) :: :ok
def process_frame(contacts, rounded, pixels, width) do
Enum.each(contacts, fn contact ->
process_contact(contact, pixels, width, rounded)
end)
Logger.info("RadarFrameWorker: #{length(contacts)} contacts ingested for frame #{DateTime.to_iso8601(rounded)}")
:ok
end
defp process_contact(%Contact{pos1: p1, pos2: p2} = contact, pixels, width, rounded) when is_map(p1) and is_map(p2) do
pos1 = to_latlon(p1)
pos2 = to_latlon(p2)
stats = CommonVolumeRadarWorker.aggregate_stats(pixels, width, pos1, pos2)
upsert_row!(contact, rounded, stats)
mark_status(contact, :complete)
end
defp process_contact(contact, _pixels, _width, _rounded) do
# Positions missing — can't compute a common volume, but we got
# this far because the enqueue filter thought it was enrichable.
# Mark unavailable so the backfill cron stops re-queueing it.
mark_status(contact, :unavailable)
end
defp upsert_row!(%Contact{id: contact_id}, observed_at, stats) do
%ContactCommonVolumeRadar{}
|> ContactCommonVolumeRadar.changeset(Map.merge(stats, %{contact_id: contact_id, observed_at: observed_at}))
|> Repo.insert(
on_conflict: {:replace_all_except, [:id, :contact_id, :inserted_at]},
conflict_target: :contact_id
)
end
defp mark_status(%Contact{} = contact, status) do
contact
|> Ecto.Changeset.change(%{radar_status: status})
|> Repo.update!()
end
defp to_latlon(%{"lat" => lat, "lon" => lon}) when is_number(lat) and is_number(lon) do
{lat / 1.0, lon / 1.0}
end
end