fix(adsb): keep aircraft ingest alive with no browser viewport #68
3 changed files with 108 additions and 2 deletions
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.
commit
366ee12b46
|
|
@ -107,3 +107,12 @@ VESSELAPI_MAX_CALLS_PER_DAY = int(os.getenv("VESSELAPI_MAX_CALLS_PER_DAY", "5"))
|
|||
# Run the VesselAPI poller inside the dashboard process (default on, like AIS).
|
||||
VESSELAPI_IN_APP = os.getenv("VESSELAPI_IN_APP", "1").lower() in ("1", "true", "yes")
|
||||
VESSELAPI_IN_INGEST = os.getenv("VESSELAPI_IN_INGEST", "0").lower() in ("1", "true", "yes")
|
||||
|
||||
# ADS-B (ADSB.lol, no key). The dashboard's refresh loop follows browser
|
||||
# viewports; with no tab open it persisted nothing and aircraft_positions went
|
||||
# silent for a day. ADSB_WATCH_ENABLED=1 makes the loop sweep the chokepoint
|
||||
# presets while idle, so the hypertable keeps advancing unattended.
|
||||
ADSB_REFRESH_INTERVAL = float(os.getenv("ADSB_REFRESH_INTERVAL", "8"))
|
||||
ADSB_WATCH_ENABLED = os.getenv("ADSB_WATCH_ENABLED", "1").lower() in ("1", "true", "yes")
|
||||
# Idle sweep cadence; 30 s over 5 preset boxes ≈ 0.17 req/s to ADSB.lol.
|
||||
ADSB_WATCH_INTERVAL = float(os.getenv("ADSB_WATCH_INTERVAL", "30"))
|
||||
|
|
|
|||
39
app/main.py
39
app/main.py
|
|
@ -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):
|
||||
|
|
|
|||
62
tests/test_adsb_watch.py
Normal file
62
tests/test_adsb_watch.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
"""Tests for the idle ADS-B watch sweep.
|
||||
|
||||
Regression: the refresh loop only persisted aircraft for *connected browser
|
||||
viewports*, so ``aircraft_positions`` went silent for a day whenever nobody had
|
||||
the map open (observed 2026-11-20 04:58 -> 2026-11-21). When no viewport is
|
||||
registered the loop must sweep the chokepoint presets instead.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from chokepoints import chokepoints
|
||||
from live_layers import bbox_center_radius_nm, parse_bbox
|
||||
from main import _adsb_watch_boxes, _adsb_refresh_loop
|
||||
from ws_manager import manager
|
||||
|
||||
|
||||
def test_watch_boxes_cover_every_chokepoint() -> None:
|
||||
boxes = _adsb_watch_boxes()
|
||||
assert len(boxes) == len(chokepoints())
|
||||
for box in boxes:
|
||||
minlon, minlat, maxlon, maxlat = box
|
||||
# Preset catalog order is minlat,minlon,maxlat,maxlon: a lon/lat swap
|
||||
# would put e.g. Hormuz at lat 56 lon 26.
|
||||
assert -90 <= minlat < maxlat <= 90
|
||||
assert -180 <= minlon < maxlon <= 180
|
||||
# ADSB.lol 403s any dist > 250 nm; live_layer clamps to 150 nm.
|
||||
assert bbox_center_radius_nm(minlon, minlat, maxlon, maxlat)[2] <= 150
|
||||
|
||||
|
||||
def test_watch_box_order_matches_catalog() -> None:
|
||||
hormuz = next(p for p in chokepoints() if p["id"] == "hormuz")
|
||||
minlat, minlon, maxlat, maxlon = (float(v) for v in hormuz["bbox"].split(","))
|
||||
assert (minlon, minlat, maxlon, maxlat) in _adsb_watch_boxes()
|
||||
# parse_bbox (the app's viewport order) agrees with the converted box.
|
||||
assert parse_bbox(f"{minlon},{minlat},{maxlon},{maxlat}") == (minlon, minlat, maxlon, maxlat)
|
||||
|
||||
|
||||
def test_idle_loop_polls_watch_boxes(monkeypatch) -> None:
|
||||
"""With no client viewport the loop must call fetch_aircraft(persist=True)."""
|
||||
calls: list[tuple[str, bool]] = []
|
||||
|
||||
async def fake_fetch(bbox: str, limit: int = 2000, persist: bool = False):
|
||||
calls.append((bbox, persist))
|
||||
return []
|
||||
|
||||
monkeypatch.setattr("live_layers.fetch_aircraft", fake_fetch)
|
||||
assert manager.viewports() == []
|
||||
|
||||
async def run() -> None:
|
||||
try:
|
||||
await asyncio.wait_for(_adsb_refresh_loop(), 0.5)
|
||||
except asyncio.TimeoutError:
|
||||
pass
|
||||
|
||||
asyncio.run(run())
|
||||
assert calls, "idle loop persisted nothing"
|
||||
assert all(persist for _, persist in calls)
|
||||
assert {bbox for bbox, _ in calls} == {
|
||||
f"{b[0]},{b[1]},{b[2]},{b[3]}" for b in _adsb_watch_boxes()
|
||||
}
|
||||
Loading…
Reference in a new issue