mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 09:42:21 +00:00
Upstream's layers and changes are carried onto the ports, with no Cesium outside src/render and no reach into the viewer: - Shipwrecks draw as one icon set, clicked through the input dispatcher. - News draws through draw sets, the camera port and the input dispatcher; its beacons pulse each frame through the frame port. - The scene occlusion probe moves into src/render/cesium and reaches detection through a new application hook, createOcclusionProbe; its private Cesium reads go through internals.js. - Flight and military trails, the stale and SIGNAL LOST cue, coasting and following the world clock run on the draw and frame ports. GPU icons follow the scene clock while the world clock is followed, and fade when stale. A report that holds is not planned again every tick. - Vessels are keyed by their record id. The stub boot now enables military, shipwrecks and news as well.
145 lines
5.5 KiB
JavaScript
145 lines
5.5 KiB
JavaScript
import { adaptFirmsRecords } from '../../data/firmsAdapt.js';
|
|
import { fireDetectionKey } from '../../data/firmsLabels.js';
|
|
import { filterFires } from '../../data/firmsIntensity.js';
|
|
import { msg } from '../../i18n/index.js';
|
|
|
|
/** A time a Date can print: epoch ms within JavaScript's date range. */
|
|
const isSnapshotEnd = (value) =>
|
|
Number.isFinite(value) && Math.abs(value) <= 8_640_000_000_000_000;
|
|
|
|
export function createIngestion({
|
|
layerState,
|
|
services,
|
|
components,
|
|
config,
|
|
feed,
|
|
}) {
|
|
const { clearSelectedEntityContextForLayer } = services.context;
|
|
const { id } = config;
|
|
|
|
/** Replace a validated snapshot while preserving source freshness and selection identity. */
|
|
|
|
async function loadHeatmap() {
|
|
if (!layerState._cells || !layerState._enabled) return;
|
|
layerState.request?.abort();
|
|
const request = new AbortController();
|
|
layerState.request = request;
|
|
layerState._loading = true;
|
|
|
|
try {
|
|
const payload = await feed.getSnapshot({ signal: request.signal });
|
|
if (
|
|
request.signal.aborted ||
|
|
layerState.request !== request ||
|
|
!layerState._enabled
|
|
)
|
|
return;
|
|
if (payload.keyRequired) {
|
|
layerState._keyRequired = true;
|
|
layerState._at = null;
|
|
layerState._error = null;
|
|
layerState._stale = false;
|
|
return;
|
|
}
|
|
layerState._keyRequired = false;
|
|
layerState._error = null;
|
|
layerState._stale = Boolean(payload?.stale);
|
|
// A snapshot of the 24 hours to a set time names it; one of the
|
|
// trailing 24 hours to now does not.
|
|
layerState._at = isSnapshotEnd(payload?.at) ? payload.at : null;
|
|
const previousSelection = layerState._selectedFire;
|
|
layerState._selectedFire = null;
|
|
layerState._allFires = adaptFirmsRecords(payload?.fires);
|
|
// Sorted once per payload; applyFilter only filters, so a slider drag
|
|
// over 200k detections never re-sorts.
|
|
layerState._allFiresByFrp = [...layerState._allFires].sort(
|
|
(a, b) => b.frp - a.frp,
|
|
);
|
|
applyFilter();
|
|
// Data age, not response age: a stale proxy payload truthfully reads old.
|
|
layerState._lastUpdate = Number.isFinite(payload?.fetchedAt)
|
|
? payload.fetchedAt
|
|
: Date.now();
|
|
// Settle the previous selection BEFORE the LOD rebuild. renderCurrentLod
|
|
// runs refreshContextRegistrations(), which deletes every context record
|
|
// not in the new top-N — including the one the store still points at.
|
|
// Clearing after that deletion fails the ownership guard inside
|
|
// clearSelectedEntityContextForLayer and emits nothing at all, so an
|
|
// eviction-aware readout never hears that its subject is gone.
|
|
const reselected = findMatchingFire(previousSelection);
|
|
if (!reselected && previousSelection) {
|
|
// The selected detection is not in the new payload: it left the feed
|
|
// rather than being deselected. Eviction-aware readouts hold their
|
|
// last-known values for this instead of tearing down.
|
|
clearSelectedEntityContextForLayer(id, { evicted: true });
|
|
}
|
|
components.rendering.renderCurrentLod(true);
|
|
if (reselected) components.selection.selectFire(reselected, false);
|
|
layerState._rowControlsListener?.();
|
|
} catch (error) {
|
|
if (
|
|
request.signal.aborted ||
|
|
layerState.request !== request ||
|
|
!layerState._enabled
|
|
)
|
|
return;
|
|
console.warn(`[Data:${id}] FIRMS live load failed:`, error);
|
|
// English data (stats reach the voice tools); the panel translates it.
|
|
layerState._error = msg('live feed unavailable');
|
|
} finally {
|
|
if (layerState.request === request) {
|
|
layerState.request = null;
|
|
layerState._loading = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Run the intensity filter over the loaded payload. This is the ONE place
|
|
* the shown set is derived: `_fires` / `_firesByFrp` feed markers, cards,
|
|
* the heatmap, the context store, the top-fires query and the analyst
|
|
* records, so none of them can disagree about which fires exist. Cells and
|
|
* the heat raster are built per shown set, so their caches go with it.
|
|
*/
|
|
|
|
function applyFilter() {
|
|
layerState._fires = filterFires(layerState._allFires, layerState._filter);
|
|
layerState._cellCacheByGrid.clear();
|
|
layerState._heatRaster = null;
|
|
layerState._firesByFrp = filterFires(
|
|
layerState._allFiresByFrp,
|
|
layerState._filter,
|
|
);
|
|
layerState._count = layerState._fires.length;
|
|
layerState._totalCount = layerState._allFires.length;
|
|
}
|
|
|
|
/**
|
|
* Find the record in the freshly-loaded set matching a previous selection.
|
|
* Identity is the detection itself (lat/lon/acquisition time) — indices
|
|
* are regenerated every fetch, so object/index identity cannot be used.
|
|
* @param {?Object} previous - Previously selected fire record.
|
|
* @returns {?Object} Matching new record.
|
|
*/
|
|
|
|
function findMatchingFire(previous) {
|
|
if (!previous) return null;
|
|
const key = fireDetectionKey(previous);
|
|
return (
|
|
layerState._fires.find((fire) => fireDetectionKey(fire) === key) || null
|
|
);
|
|
}
|
|
const methods = {
|
|
async update() {
|
|
if (layerState._destroyed || !layerState._enabled || layerState._loading)
|
|
return;
|
|
// Scheduled 10-minute poll (and the manager's immediate first update):
|
|
// refetch live fires through the proxy and re-render. Viewport-driven
|
|
// re-renders between polls are handled by the LOD watcher.
|
|
await loadHeatmap();
|
|
},
|
|
};
|
|
|
|
return { loadHeatmap, applyFilter, findMatchingFire, methods };
|
|
}
|