Merge main through #1637 into the renderer seam

Ports the cockpit bridge view, the orbital view riding a satellite with
ground-site focus, satellite classes and draw-mark row actions onto the
renderer contract. The orbital view's imagery look-ahead pre-load is
dropped (no contract member); tiles load as they come on screen.
This commit is contained in:
Sameh Khamis committed 2026-11-10 01:35:00 -08:00
commit e48ed3eb9e
169 files changed
+22415 -681

No files matched your search

+231 -6
View File
@@ -3,6 +3,22 @@ import { msg } from '../../i18n/index.js';
import { fromDegrees } from '../../geo/ellipsoid.js';
import { noradIdFromSatnum, tleLineLaunchYear } from '../../sources/tle.js';
import { CATALOG_GROUPS, ISS_NORAD, POINT_STYLES } from './policy.js';
import { classifySatellite, parseSatcatCsv } from './classes.js';
import { matchImagingSensor } from './sensors.js';
/**
* How long a catalog rebuild waits for SATCAT before it classifies without
* it, ms. SATCAT can hang for the proxy's whole upstream timeout (20 s); the
* late answer reclassifies in place.
*/
export const SATCAT_WAIT_MS = 3000;
/**
* How long a complete, current SATCAT read is kept before the layer asks
* again, ms. SATCAT changes daily and the proxy caches it a day, so the
* five-minute TLE update does not read all eleven groups each time.
*/
export const SATCAT_KEEP_MS = 6 * 60 * 60 * 1000;
export function createIngestion({
state: layerState,
@@ -10,6 +26,158 @@ export function createIngestion({
parts,
source,
}) {
/**
* The local packs the classes and zoom steps are read from: the UCS pack
* and the imaging-sensor pack. Neither is needed to render: a failed read
* leaves the last pack (or none) and classification falls back to names
* and groups.
*/
async function _readLocalEvidence() {
const ucs = layerState._ucs
? Promise.resolve(layerState._ucs)
: Promise.resolve()
.then(() => source.readUcs?.())
.then((pack) => (pack instanceof Map ? pack : null))
.catch((error) => {
console.warn('[Data:Satellites] UCS pack unavailable:', error);
return null;
});
// The sensor pack only adds zoom steps in the orbital view; without it
// every satellite keeps the base steps.
const sensors = layerState._sensors
? Promise.resolve(layerState._sensors)
: Promise.resolve()
.then(() => source.readSensors?.())
.then((index) => (index?.byNorad instanceof Map ? index : null))
.catch((error) => {
console.warn(
'[Data:Satellites] Imaging sensor pack unavailable:',
error,
);
return null;
});
const [pack, sensorIndex] = await Promise.all([ucs, sensors]);
if (pack) layerState._ucs = pack;
if (sensorIndex) layerState._sensors = sensorIndex;
}
/**
* CelesTrak SATCAT for every core group: type and status, which refine the
* classes. A group whose read failed gives null.
* @returns {Promise<Array<{records: Map, stale: boolean}|null>>}
*/
function _readSatcat(signal) {
if (typeof source.readSatcat !== 'function') return Promise.resolve([]);
// A complete, current read is kept; a stale or failed one is asked again.
if (
layerState._satcatState === 'current' &&
Date.now() - layerState._satcatReadAtMs < SATCAT_KEEP_MS
)
return Promise.resolve([]);
return Promise.all(
CATALOG_GROUPS.map((groupDef) =>
source
.readSatcat(groupDef.path, { signal })
.then((res) =>
res?.ok
? { records: parseSatcatCsv(res.text), stale: Boolean(res.stale) }
: null,
)
.catch((error) => {
if (signal.aborted || error?.name === 'AbortError') throw error;
return null;
}),
),
);
}
/**
* Merge SATCAT reads and say how current they are: 'current', 'stale' (a
* group came from the proxy's last copy) or 'unavailable' (a group failed;
* its earlier records, if any, stay).
*/
function _mergeSatcat(groups) {
let satcatState = 'current';
for (const group of groups) {
if (!group) {
satcatState = 'unavailable';
continue;
}
if (group.stale && satcatState === 'current') satcatState = 'stale';
for (const [noradId, record] of group.records) {
// A group served from the proxy's last copy never overrules a
// current read of the same object, from this merge or an earlier one.
const held = layerState._satcat.get(noradId);
if (group.stale && held && !held.stale) continue;
layerState._satcat.set(
noradId,
group.stale ? { ...record, stale: true } : record,
);
}
}
if (!groups.length) return;
const changed = layerState._satcatState !== satcatState;
layerState._satcatState = satcatState;
layerState._satcatReadAtMs = Date.now();
if (satcatState !== 'current')
console.warn(`[Data:Satellites] SATCAT ${satcatState}: classes may lag`);
// The row's info line says so (getRowControls).
if (changed) parts.controls._notifyRowControls();
}
/**
* Classify the catalog again once SATCAT lands after it was built: only
* entries whose SATCAT record changed are read again; a changed class
* restyles its point and refreshes visibility, the legend and the overlay.
*/
function _reclassifyFromSatcat() {
let changed = false;
for (const [noradId, sat] of layerState._catalog) {
if (sat.group === 'dense') continue;
const satcat = layerState._satcat.get(noradId) || null;
if (satcat === sat.satcat) continue;
sat.satcat = satcat;
// The Cockpit identity (owner, COSPAR id) reads SATCAT too.
sat.cockpitIdentity = undefined;
const next = classifySatellite({
noradId,
name: sat.name,
group: sat.group,
groups: sat.groups,
ucs: sat.ucs,
satcat,
launchYear: sat.launchYear,
});
if (next === sat.satelliteClass) continue;
sat.satelliteClass = next;
changed = true;
if (layerState._points.has(noradId))
parts.rendering._setPointStyle(
noradId,
parts.controls._pointStyleFor(noradId, sat.group),
);
}
if (!changed) return;
parts.controls._applyClassVisibility();
layerState._catalogRevision++;
parts.controls._notifyRowControls();
}
/** Resolve null after `ms`, or at once when `signal` aborts. */
function _waitOrAbort(ms, signal) {
return new Promise((resolve) => {
const timer = setTimeout(resolve, ms, null);
signal.addEventListener(
'abort',
() => {
clearTimeout(timer);
resolve(null);
},
{ once: true },
);
});
}
const methods = {
async update(viewer, { signal = null } = {}) {
const trackingRefreshEpoch = ++layerState._trackingRefreshEpoch;
@@ -27,6 +195,10 @@ export function createIngestion({
updateSignal.throwIfAborted();
// Load all core groups in parallel; a failed/empty group degrades
// gracefully (parseTLE of an upstream error body yields []).
const localEvidence = _readLocalEvidence();
const satcat = _readSatcat(updateSignal);
// An abort can reject this before the groups settle and it is awaited.
satcat.catch(() => {});
const results = await Promise.all(
CATALOG_GROUPS.map(async (groupDef) => {
try {
@@ -49,7 +221,23 @@ export function createIngestion({
}
}),
);
await localEvidence;
// SATCAT only refines classes: wait a moment for it, then classify
// with what is held and let a late answer reclassify in place.
const satcatInTime = await Promise.race([
satcat.then((groups) => ({ groups })),
_waitOrAbort(SATCAT_WAIT_MS, updateSignal),
]).catch(() => null);
updateSignal.throwIfAborted();
if (satcatInTime) _mergeSatcat(satcatInTime.groups);
else
satcat
.then((groups) => {
if (updateSignal.aborted || !layerState._enabled) return;
_mergeSatcat(groups);
_reclassifyFromSatcat();
})
.catch(() => {});
const failed = results.filter((r) => !r.ok).map((r) => r.path);
if (failed.length > 0) {
@@ -62,7 +250,7 @@ export function createIngestion({
);
// CelesTrak outage guard (H3): if EVERY group failed, bail BEFORE clearing.
// Wiping the collection + catalog here would blank all 838 satellites while
// Wiping the collection + catalog here would blank every satellite while
// the chip still read "just now". Keep the existing (stale) catalog on
// screen and surface the outage instead — do NOT stamp _lastUpdate.
if (results.every((r) => !r.ok)) {
@@ -109,6 +297,18 @@ export function createIngestion({
allEntries.push({ ...e, group: r.tag, stale: r.stale });
}
// Every group each TLE came in, keyed by its catalog-number column:
// the shown tag is the first, but classification reads them all.
const groupsByNumber = new Map();
for (const entry of allEntries) {
const number = String(entry.line1 || '')
.slice(2, 7)
.trim();
const groups = groupsByNumber.get(number);
if (!groups) groupsByNumber.set(number, [entry.group]);
else if (!groups.includes(entry.group)) groups.push(entry.group);
}
// Deduplicate by NORAD ID — first (most specific) group tag wins
const seen = new Set();
let coreStale = false;
@@ -122,12 +322,38 @@ export function createIngestion({
seen.add(noradId);
// Store in catalog
const launchYear = tleLineLaunchYear(entry.line1);
const ucs = layerState._ucs?.get(noradId) || null;
const satcat = layerState._satcat.get(noradId) || null;
const groups = groupsByNumber.get(
String(entry.line1 || '')
.slice(2, 7)
.trim(),
) || [entry.group];
layerState._catalog.set(noradId, {
name: entry.name,
satrec,
group: entry.group,
groups,
// satellite.js does not keep the designator the year match reads.
launchYear: tleLineLaunchYear(entry.line1),
launchYear,
line1: entry.line1,
line2: entry.line2,
satelliteClass: classifySatellite({
noradId,
name: entry.name,
group: entry.group,
groups,
ucs,
satcat,
launchYear,
}),
ucs,
satcat,
sensor: matchImagingSensor(layerState._sensors, {
noradId,
name: entry.name,
}),
});
coreStale ||= entry.stale;
@@ -137,20 +363,19 @@ export function createIngestion({
if (!pos) continue;
// Add its dot (styling from the shared table, WS-D3)
// Hidden classes keep their dot, so a toggle needs no rebuild.
parts.rendering._addPoint(
noradId,
fromDegrees(pos.longitude, pos.latitude, pos.altitude),
parts.controls._pointStyleFor(noradId, entry.group),
parts.controls._classVisible(noradId),
);
}
// Show ISS orbital path by default
if (layerState._catalog.has(ISS_NORAD)) {
parts.rendering._showOrbitPath(ISS_NORAD, POINT_STYLES.iss.color);
parts.rendering._setOrbitPathShown(
ISS_NORAD,
layerState._params.showOrbits,
);
parts.controls._syncIssOrbit();
parts.labels._syncIssOverlay();
}