feat: AIS stream follows the map viewport
All checks were successful
build-and-deploy / build (push) Successful in 2m32s
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:
parent
c58f66770c
commit
32320635dc
6 changed files with 185 additions and 6 deletions
|
|
@ -1652,6 +1652,8 @@ let overlayReq = {ac:0, trains:0, vessels:0, alerts:0, perim:0, incidents:0, sto
|
|||
let moveDebounce = null;
|
||||
let overlayAbort = null;
|
||||
let lastCell = '';
|
||||
let lastVesselSubBox = ''; // last viewport box sent to the AIS stream
|
||||
let vesselRefollowTimer = null; // one follow-up fetch after a retune
|
||||
function bboxCell() {
|
||||
if (!map) return '';
|
||||
return currentBBox().split(',').map(n => Number(n).toFixed(2)).join(',') + '@' + map.getZoom();
|
||||
|
|
@ -1917,6 +1919,17 @@ function currentBBox() {
|
|||
}
|
||||
return `${west.toFixed(4)},${south.toFixed(4)},${east.toFixed(4)},${north.toFixed(4)}`;
|
||||
}
|
||||
function quantizeBBox(bbox, decimals = 1) {
|
||||
// Round outward (west/south floor, east/north ceil) so the box always
|
||||
// covers the viewport; a coarse box keeps AIS stream retunes sparse.
|
||||
const [w, s, e, n] = bbox.split(',').map(Number);
|
||||
const p = Math.pow(10, decimals);
|
||||
const wq = Math.floor(w * p) / p;
|
||||
const sq = Math.floor(s * p) / p;
|
||||
const eq = Math.ceil(e * p) / p;
|
||||
const nq = Math.ceil(n * p) / p;
|
||||
return [wq, sq, eq, nq].map(v => v.toFixed(decimals)).join(',');
|
||||
}
|
||||
function toggleLayerPanel() {
|
||||
const panel = document.getElementById('layer-panel');
|
||||
panel.classList.toggle('collapsed');
|
||||
|
|
@ -2576,8 +2589,13 @@ async function loadTrains() {
|
|||
}
|
||||
async function toggleVessels() {
|
||||
vesselsOn = document.getElementById('lp-vessels-on').checked;
|
||||
if (vesselsOn) await loadVessels();
|
||||
else vesselsGroup = dropLayer(vesselsGroup);
|
||||
if (vesselsOn) {
|
||||
lastVesselSubBox = '';
|
||||
await loadVessels();
|
||||
} else {
|
||||
vesselsGroup = dropLayer(vesselsGroup);
|
||||
clearTimeout(vesselRefollowTimer);
|
||||
}
|
||||
}
|
||||
async function loadVessels() {
|
||||
if (!map) return;
|
||||
|
|
@ -2586,8 +2604,25 @@ async function loadVessels() {
|
|||
return;
|
||||
}
|
||||
const req = ++overlayReq.vessels;
|
||||
const bb = currentBBox();
|
||||
// Retune the server-side AIS stream to this viewport. Skip whole-world
|
||||
// boxes (zoom out / dateline wrap) so the free tier is not flooded; the
|
||||
// worker coalesces retunes and caps them at 1/s upstream.
|
||||
const [bw, bs, be, bn] = bb.split(',').map(Number);
|
||||
const isWorld = (be - bw) >= 300 || bb.startsWith('-180.0000,-85.0000,180.0000,85.0000');
|
||||
const subKey = isWorld ? '' : quantizeBBox(bb);
|
||||
let retuned = false;
|
||||
if (subKey && subKey !== lastVesselSubBox) {
|
||||
lastVesselSubBox = subKey;
|
||||
retuned = true;
|
||||
fetch(`${API}/api/vessels/subscribe`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ bbox: subKey })
|
||||
}).catch(() => {});
|
||||
}
|
||||
try {
|
||||
const r = await overlayFetch(`${API}/api/vessels?bbox=${currentBBox()}`);
|
||||
const r = await overlayFetch(`${API}/api/vessels?bbox=${bb}`);
|
||||
const pts = await r.json();
|
||||
if (req !== overlayReq.vessels) return;
|
||||
vesselsGroup = renderPoints(vesselsGroup, Array.isArray(pts) ? pts : [], p => {
|
||||
|
|
@ -2596,6 +2631,11 @@ async function loadVessels() {
|
|||
}, true, 'vessel');
|
||||
document.getElementById('lp-vessels-count').textContent = (pts.length || 0).toLocaleString();
|
||||
addExtraAttrib('AISStream');
|
||||
// First positions for a new zone arrive ~1-3s after the retune.
|
||||
if (retuned) {
|
||||
clearTimeout(vesselRefollowTimer);
|
||||
vesselRefollowTimer = setTimeout(() => { if (vesselsOn) loadVessels(); }, 2200);
|
||||
}
|
||||
} catch (e) {
|
||||
if (isAbort(e)) return;
|
||||
console.error('Vessels load failed', e);
|
||||
|
|
|
|||
Loading…
Reference in a new issue