feat: AIS stream follows the map viewport
All checks were successful
build-and-deploy / build (push) Successful in 2m32s

The Vessels layer now retunes the server-side AISStream subscription to the
client viewport instead of a static AISSTREAM_BBOX. The frontend POSTs its
quantized viewport box to /api/vessels/subscribe on moveend; the ais_stream
worker coalesces and applies it at the service's 1 subscription/s cap, then
last-known positions for the new area arrive within a couple of seconds (the
frontend does one follow-up fetch after retuning). Bounds the in-memory
vessel store across regions. Key stays server-side.
This commit is contained in:
Sirius DevOps 2026-08-27 21:46:37 -04:00
commit 32320635dc
6 changed files with 185 additions and 6 deletions

View file

@ -40,7 +40,7 @@ from schemas import (
FeedSourceCreate, FeedSourceOut,
KeyOut, KeyValueIn,
SearchResult, SentimentSummary, SourceType,
SearchQuery, TimelinePoint,
SearchQuery, TimelinePoint, VesselBboxUpdate,
)
from ingestor import ingest_event, fetch_and_process
from sources import ingest_rss_feed, ingest_gdelt, ingest_earthquakes, ingest_social_signals
@ -1176,6 +1176,34 @@ async def list_vessels(
raise HTTPException(422, str(exc)) from exc
@app.post("/api/vessels/subscribe")
async def vessels_subscribe(payload: VesselBboxUpdate):
"""Retune the server-side AISStream subscription to the client viewport.
The stream follows the map: after a moveend the frontend posts its bbox,
the worker re-subscribes (≤1/s upstream), and last-known vessels for the
new area start arriving within a second or two. Empty/null bbox resets to
the env AISSTREAM_BBOX default. The API key never reaches the browser.
"""
raw = (payload.bbox or "").strip()
if not raw:
from ais_stream import reset_viewport_bbox
await reset_viewport_bbox()
return {"ok": True, "bbox": None}
try:
minlon, minlat, maxlon, maxlat = parse_bbox(raw)
except ValueError as exc:
raise HTTPException(422, str(exc)) from exc
if not (-180 <= minlon <= 180 and -180 <= maxlon <= 180
and -90 <= minlat <= 90 and -90 <= maxlat <= 90):
raise HTTPException(422, "bbox coordinates out of range")
if minlon >= maxlon or minlat >= maxlat:
raise HTTPException(422, "bbox must have min < max")
from ais_stream import request_viewport_bbox
await request_viewport_bbox(minlon, minlat, maxlon, maxlat)
return {"ok": True, "bbox": raw}
@app.get("/api/fire-incidents")
async def list_fire_incidents(
bbox: str | None = Query(None),