fix(adsb): keep aircraft ingest alive with no browser viewport

The ADSB refresh loop only persisted positions for registered WS viewports,
so aircraft_positions went silent for a day whenever nobody had the map open
(max(ts) frozen at 2026-11-20 04:58 while USGS/news ingest kept running).

When no client viewport is registered the loop now sweeps the chokepoint
presets (ADSB_WATCH_ENABLED=1, ADSB_WATCH_INTERVAL=30s, 5 boxes ~65-81 nm)
through the same fetch_aircraft(persist=True) path, so the hypertable and
aircraft_tracks_1min keep advancing unattended. Viewport polling keeps its
8 s cadence when a tab is connected.
This commit is contained in:
Sirius DevOps 2026-11-21 17:30:47 -05:00
commit 366ee12b46
3 changed files with 108 additions and 2 deletions

View file

@ -126,14 +126,49 @@ app.include_router(auth_router)
STATIC_DIR = Path(__file__).parent / "static"
def _adsb_watch_boxes() -> list[tuple[float, float, float, float]]:
"""Chokepoint presets as viewport boxes (minlon, minlat, maxlon, maxlat).
Used only while no browser viewport is registered: without it the refresh
loop persists nothing and ``aircraft_positions`` silently stalls whenever
nobody has the map open.
"""
from chokepoints import chokepoints
out: list[tuple[float, float, float, float]] = []
for preset in chokepoints():
try:
# Preset bboxes are minlat,minlon,maxlat,maxlon (VesselAPI order).
minlat, minlon, maxlat, maxlon = (float(v) for v in str(preset["bbox"]).split(","))
except (KeyError, TypeError, ValueError):
continue
out.append((minlon, minlat, maxlon, maxlat))
return out
async def _adsb_refresh_loop() -> None:
"""Poll ADSB.lol for connected viewports — never from GET /api/aircraft."""
"""Poll ADSB.lol for connected viewports — never from GET /api/aircraft.
When no client viewport is registered the loop sweeps the watch boxes
(chokepoint presets) at ``ADSB_WATCH_INTERVAL`` so aircraft ingest keeps
advancing with no map open.
"""
from ws_manager import manager
from live_layers import fetch_aircraft
from config import ADSB_REFRESH_INTERVAL, ADSB_WATCH_ENABLED, ADSB_WATCH_INTERVAL
watch_boxes: list[tuple[float, float, float, float]] | None = None
while True:
interval = ADSB_REFRESH_INTERVAL
try:
boxes = manager.viewports()
if not boxes and ADSB_WATCH_ENABLED:
if watch_boxes is None:
watch_boxes = _adsb_watch_boxes()
boxes = watch_boxes
interval = ADSB_WATCH_INTERVAL
logger.debug("adsb_idle_watch", boxes=len(boxes))
for box in boxes:
bbox = f"{box[0]},{box[1]},{box[2]},{box[3]}"
try:
@ -142,7 +177,7 @@ async def _adsb_refresh_loop() -> None:
logger.warning("adsb_refresh_failed", error=str(exc))
except Exception as exc: # noqa: BLE001
logger.warning("adsb_refresh_loop", error=str(exc))
await asyncio.sleep(8)
await asyncio.sleep(interval)
class CachedStaticFiles(StaticFiles):