mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-21 18:52:18 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
829d1c1f7e | ||
|
|
e79d972389 |
No files matched your search
@@ -1,3 +0,0 @@
|
||||
- A still-image CCTV camera no longer keeps the scene rendering 60 times a
|
||||
second. Only a video feed holds continuous rendering; a still redraws when a
|
||||
new image arrives (mazy1998, #1710).
|
||||
@@ -0,0 +1,2 @@
|
||||
- `/api/firms` keeps its response bytes and sends them gzipped instead of
|
||||
serialising the whole fire set for every request (mazy1998, #1711).
|
||||
@@ -3297,6 +3297,15 @@ The `get_active_fires` tool reports acquisition times as UTC ISO timestamps,
|
||||
including zero-padded hours and minutes; missing or invalid dates and times
|
||||
remain `null`, including impossible calendar dates from a custom source.
|
||||
|
||||
`/api/firms` keeps its last response body as bytes and reuses it for requests
|
||||
that would get the same payload: the same cache entry and `stale` flag, while
|
||||
the clock stays inside the span over which the trailing-24-hour filter keeps
|
||||
the same detections. That span ends when the oldest kept acquisition minute
|
||||
turns 24 hours old, so requests minutes apart are still serialised afresh. A
|
||||
client that sends `Accept-Encoding: gzip` gets the body gzipped (compressed
|
||||
once per kept body, `Content-Encoding: gzip`); every response carries
|
||||
`Vary: Accept-Encoding`. The JSON is unchanged.
|
||||
|
||||
The FIRMS proxy records source success after appending its rows. If aggregation
|
||||
throws, that source reports failure without a contradictory success entry.
|
||||
The existing per-record append continues to support large feeds; sequential
|
||||
|
||||
@@ -376,6 +376,7 @@
|
||||
"modules": [
|
||||
"server/providers/firms.js",
|
||||
"server/providers/common/http.js",
|
||||
"server/providers/common/compress.js",
|
||||
"src/sources/httpBody.js",
|
||||
"src/i18n/marker.js",
|
||||
"src/data/firmsCsv.js"
|
||||
|
||||
+58
-10
@@ -3,7 +3,12 @@ import https from 'node:https';
|
||||
import { promises as fsp } from 'node:fs';
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
import { filterTrailing24h, parseFirmsCsv } from '../../src/data/firmsCsv.js';
|
||||
import {
|
||||
filterTrailing24h,
|
||||
parseFirmsCsv,
|
||||
trailing24hWindow,
|
||||
} from '../../src/data/firmsCsv.js';
|
||||
import { acceptsGzip, encodedBody, gzipBody } from './common/compress.js';
|
||||
import { readResponseTextCapped } from './common/http.js';
|
||||
|
||||
const NULL_BODY_STATUSES = new Set([204, 205, 304]);
|
||||
@@ -179,6 +184,11 @@ export function firmsProxy() {
|
||||
let statusCache = null;
|
||||
/** @type {?Promise<?{used: number, limit: number}>} */
|
||||
let statusInflight = null;
|
||||
/**
|
||||
* The last /api/firms payload as bytes, with its gzip. One slot.
|
||||
* @type {?{entry: object, stale: boolean, from: number, until: number, body: Buffer, gzip: ?Promise<?Buffer>}}
|
||||
*/
|
||||
let served = null;
|
||||
|
||||
const mapKey = () => String(process.env.FIRMS_MAP_KEY || '').trim();
|
||||
|
||||
@@ -259,12 +269,8 @@ export function firmsProxy() {
|
||||
return { at: now, sources, fires };
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache entry → response payload. Fires are RE-filtered to the trailing
|
||||
* 24 h at serve time so a stale cache never serves >24h-old detections.
|
||||
*/
|
||||
function buildPayload(entry, stale) {
|
||||
const fires = filterTrailing24h(entry.fires, Date.now());
|
||||
/** Cache entry and the fires to serve from it → response payload. */
|
||||
function buildPayload(entry, stale, fires) {
|
||||
return {
|
||||
fetchedAt: entry.at,
|
||||
stale,
|
||||
@@ -275,6 +281,32 @@ export function firmsProxy() {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The payload as the bytes to send. Serialising the whole fire set is tens
|
||||
* of MB in a heavy season, so it is done once and kept, not once per request.
|
||||
*
|
||||
* Fires are RE-filtered to the trailing 24 h at serve time so a stale cache
|
||||
* never serves >24h-old detections. The kept bytes are therefore reused only
|
||||
* for the same entry and stale flag while the clock is inside the span over
|
||||
* which that filter keeps the same records: a request never gets a body that
|
||||
* serialising for it would not have produced.
|
||||
*/
|
||||
function payloadBody(entry, stale) {
|
||||
const now = Date.now();
|
||||
if (
|
||||
served?.entry === entry &&
|
||||
served.stale === stale &&
|
||||
now >= served.from &&
|
||||
now <= served.until
|
||||
) {
|
||||
return served;
|
||||
}
|
||||
const { kept, from, until } = trailing24hWindow(entry.fires, now);
|
||||
const body = Buffer.from(JSON.stringify(buildPayload(entry, stale, kept)));
|
||||
served = { entry, stale, from, until, body, gzip: null };
|
||||
return served;
|
||||
}
|
||||
|
||||
/** mapkey_status transactions, cached 5 min, best-effort (null on failure). */
|
||||
function getTransactions(key) {
|
||||
const now = Date.now();
|
||||
@@ -327,6 +359,22 @@ export function firmsProxy() {
|
||||
});
|
||||
res.end(JSON.stringify(obj));
|
||||
};
|
||||
const sendPayload = async (entry, stale) => {
|
||||
const kept = payloadBody(entry, stale);
|
||||
// Compressed once per kept body, on the first request that accepts
|
||||
// gzip, so an identity response never pays or waits for it.
|
||||
const gzip = acceptsGzip(req)
|
||||
? await (kept.gzip ??= gzipBody(kept.body).catch(() => null))
|
||||
: null;
|
||||
const encoded = encodedBody(req, kept.body, gzip);
|
||||
if (res.headersSent) return;
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
...encoded.headers,
|
||||
});
|
||||
res.end(encoded.body);
|
||||
};
|
||||
try {
|
||||
const subPath = String(req.url || '').split('?')[0];
|
||||
const key = mapKey();
|
||||
@@ -363,7 +411,7 @@ export function firmsProxy() {
|
||||
|
||||
const entry = mem;
|
||||
if (entry && Date.now() - entry.at < TTL_MS) {
|
||||
sendJson(200, buildPayload(entry, false));
|
||||
await sendPayload(entry, false);
|
||||
return;
|
||||
}
|
||||
// Stale or missing → refresh, single-flight (concurrent requests
|
||||
@@ -389,9 +437,9 @@ export function firmsProxy() {
|
||||
const pending = inflight;
|
||||
const fresh = await pending;
|
||||
if (fresh) {
|
||||
sendJson(200, buildPayload(fresh, false));
|
||||
await sendPayload(fresh, false);
|
||||
} else if (entry) {
|
||||
sendJson(200, buildPayload(entry, true)); // upstream down — stale beats empty
|
||||
await sendPayload(entry, true); // upstream down — stale beats empty
|
||||
} else {
|
||||
sendJson(502, {
|
||||
error: 'firms fetch failed and no cache available',
|
||||
|
||||
+27
-2
@@ -182,19 +182,44 @@ export function acquisitionMsUtc(acqDate, acqTime) {
|
||||
* @returns {Array<Object>} Filtered records (original objects, order preserved).
|
||||
*/
|
||||
export function filterTrailing24h(records, nowMs) {
|
||||
if (!Array.isArray(records) || !Number.isFinite(nowMs)) return [];
|
||||
return trailing24hWindow(records, nowMs).kept;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link filterTrailing24h} with the span of `nowMs` values, inclusive, over
|
||||
* which it keeps exactly these records. A caller holding something built from
|
||||
* `kept` can reuse it while its clock stays inside `from..until`.
|
||||
* @param {Array<{acqDate: string, acqTime: string|number}>} records - Parser records.
|
||||
* @param {number} nowMs - Reference epoch milliseconds.
|
||||
* @returns {{kept: Array<Object>, from: number, until: number}} Span in epoch
|
||||
* ms; NaN (never reusable) for unusable input.
|
||||
*/
|
||||
export function trailing24hWindow(records, nowMs) {
|
||||
if (!Array.isArray(records) || !Number.isFinite(nowMs))
|
||||
return { kept: [], from: NaN, until: NaN };
|
||||
const oldest = nowMs - WINDOW_MS;
|
||||
const newest = nowMs + FORWARD_SLACK_MS;
|
||||
let from = -Infinity;
|
||||
let until = Infinity;
|
||||
const memo = new Map();
|
||||
return records.filter((record) => {
|
||||
const kept = records.filter((record) => {
|
||||
const key = `${record.acqDate}:${record.acqTime}`;
|
||||
let ms = memo.get(key);
|
||||
if (ms === undefined) {
|
||||
ms = acquisitionMsUtc(record.acqDate, record.acqTime);
|
||||
memo.set(key, ms);
|
||||
// Each distinct time narrows the span to the clock values that leave
|
||||
// it on the same side of the window.
|
||||
if (ms < oldest) from = Math.max(from, ms + WINDOW_MS + 1);
|
||||
else if (ms > newest) until = Math.min(until, ms - FORWARD_SLACK_MS - 1);
|
||||
else if (Number.isFinite(ms)) {
|
||||
from = Math.max(from, ms - FORWARD_SLACK_MS);
|
||||
until = Math.min(until, ms + WINDOW_MS);
|
||||
}
|
||||
}
|
||||
return Number.isFinite(ms) && ms >= oldest && ms <= newest;
|
||||
});
|
||||
return { kept, from, until };
|
||||
}
|
||||
|
||||
/** Trimmed string cell at index, '' for missing columns. */
|
||||
|
||||
@@ -7,11 +7,15 @@ import dnsLookup from 'node:dns';
|
||||
import https from 'node:https';
|
||||
import net from 'node:net';
|
||||
import { Readable } from 'node:stream';
|
||||
import { filterTrailing24h, parseFirmsCsv } from './firmsCsv.js';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import { filterTrailing24h, parseFirmsCsv, trailing24hWindow } from './firmsCsv.js';
|
||||
import {
|
||||
describeFetchError,
|
||||
fetchFirms,
|
||||
fetchIpv4,
|
||||
firmsProxy,
|
||||
isAddressSelectionFailure,
|
||||
} from '../../server/providers/firms.js';
|
||||
|
||||
@@ -316,3 +320,119 @@ test('FIRMS requests only the satellites NASA still delivers', () => {
|
||||
// a MAP_KEY transaction.
|
||||
assert.ok(!SOURCES.includes('VIIRS_SNPP_NRT'));
|
||||
});
|
||||
|
||||
// The /api/firms handler itself, over a disk cache seeded in a temp directory:
|
||||
// no server, no upstream, and a placeholder MAP_KEY that is never sent anywhere.
|
||||
const TTL_MS = 30 * 60_000;
|
||||
const fire = (acqDate, acqTime, lat = 1) => ({ lat, lon: 2, frp: 4.5, confidence: 'n', brightness: 330.1,
|
||||
brightnessTi5: 290.2, daynight: 'D', acqDate, acqTime, satellite: 'N20', instrument: 'VIIRS' });
|
||||
const SERVED_SOURCES = SOURCES.map((source, index) => ({ source, count: index === 0 ? 20_001 : 0, ok: true }));
|
||||
// 20,000 detections an hour old and one that turns 24 h old five minutes after NOW.
|
||||
const SERVED_FIRES = [fire('2026-09-10', '1205'), ...Array.from({ length: 20_000 }, (_, i) => fire('2026-09-11', '1100', i / 1000))];
|
||||
const LEAVES_AT = NOW + 5 * 60_000;
|
||||
/** What the handler sent before it kept a body: one JSON.stringify of the payload per request. */
|
||||
const unfixedBody = (at, stale, now) => {
|
||||
const fires = filterTrailing24h(SERVED_FIRES, now);
|
||||
return JSON.stringify({ fetchedAt: at, stale, ttlMs: TTL_MS, sources: SERVED_SOURCES, count: fires.length, fires });
|
||||
};
|
||||
|
||||
function createFirmsHandler(t, at) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'firms-proxy-'));
|
||||
fs.mkdirSync(path.join(dir, '.gev-cache'));
|
||||
fs.writeFileSync(path.join(dir, '.gev-cache', 'firms.json'), JSON.stringify({ at, sources: SERVED_SOURCES, fires: SERVED_FIRES }));
|
||||
const cwd = process.cwd();
|
||||
const key = process.env.FIRMS_MAP_KEY;
|
||||
const realFetch = globalThis.fetch;
|
||||
process.env.FIRMS_MAP_KEY = 'unit-test-placeholder';
|
||||
// A refresh must never reach NASA from a test: every upstream call fails.
|
||||
globalThis.fetch = async () => { throw new Error('upstream down'); };
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
t.mock.timers.enable({ apis: ['Date'], now: NOW });
|
||||
t.after(() => {
|
||||
globalThis.fetch = realFetch;
|
||||
if (key === undefined) delete process.env.FIRMS_MAP_KEY; else process.env.FIRMS_MAP_KEY = key;
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
let handler;
|
||||
process.chdir(dir); // the proxy resolves its cache directory when created
|
||||
try {
|
||||
firmsProxy().configureServer({ middlewares: { use: (route, fn) => { handler = fn; } } });
|
||||
} finally {
|
||||
process.chdir(cwd);
|
||||
}
|
||||
return (acceptEncoding) => new Promise((resolve, reject) => {
|
||||
const res = {
|
||||
headersSent: false,
|
||||
writeHead(status, headers) { this.headersSent = true; this.status = status; this.headers = headers; },
|
||||
end(body) { resolve({ status: this.status, headers: this.headers, body }); },
|
||||
};
|
||||
handler({ url: '/', headers: acceptEncoding === undefined ? {} : { 'accept-encoding': acceptEncoding } }, res).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
test('/api/firms serialises once for requests that share a payload', async (t) => {
|
||||
const request = createFirmsHandler(t, NOW);
|
||||
const first = await request();
|
||||
t.mock.timers.setTime(LEAVES_AT); // exactly 24 h old is still inside the window
|
||||
const second = await request();
|
||||
assert.equal(first.status, 200);
|
||||
assert.ok(Buffer.isBuffer(first.body), 'the body is kept as bytes');
|
||||
assert.equal(second.body, first.body, 'the second request sends the kept bytes, not a new serialisation');
|
||||
assert.equal(first.body.toString(), unfixedBody(NOW, false, NOW), 'byte-identical to the per-request serialisation');
|
||||
assert.equal(JSON.parse(first.body).count, 20_001);
|
||||
});
|
||||
|
||||
test('/api/firms sends gzip only to a client that accepts it', async (t) => {
|
||||
const request = createFirmsHandler(t, NOW);
|
||||
const identity = await request();
|
||||
const gzipped = await request('gzip, deflate, br');
|
||||
const refused = await request('gzip;q=0, br');
|
||||
assert.deepEqual(identity.headers,
|
||||
{ 'Content-Type': 'application/json', 'Cache-Control': 'no-store', Vary: 'Accept-Encoding' });
|
||||
assert.deepEqual(gzipped.headers, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store',
|
||||
'Content-Encoding': 'gzip', Vary: 'Accept-Encoding' });
|
||||
assert.deepEqual(refused.headers, identity.headers);
|
||||
assert.equal(refused.body, identity.body);
|
||||
assert.ok(gzipped.body.byteLength < identity.body.byteLength / 4, 'the fire set compresses');
|
||||
assert.equal(gunzipSync(gzipped.body).toString(), unfixedBody(NOW, false, NOW), 'decodes to the identity bytes');
|
||||
assert.equal((await request('gzip')).body, gzipped.body, 'compressed once');
|
||||
});
|
||||
|
||||
test('/api/firms never reuses a body the request would not have produced', async (t) => {
|
||||
const request = createFirmsHandler(t, NOW - TTL_MS + 6 * 60_000);
|
||||
const at = NOW - TTL_MS + 6 * 60_000;
|
||||
const before = await request();
|
||||
t.mock.timers.setTime(LEAVES_AT + 1); // one detection is now older than 24 h
|
||||
const after = await request('gzip');
|
||||
assert.equal(before.body.toString(), unfixedBody(at, false, NOW));
|
||||
assert.equal(gunzipSync(after.body).toString(), unfixedBody(at, false, LEAVES_AT + 1));
|
||||
assert.equal(JSON.parse(gunzipSync(after.body)).count, 20_000);
|
||||
t.mock.timers.setTime(NOW + 6 * 60_000); // the entry reaches its TTL and the refresh fails
|
||||
const stale = await request();
|
||||
assert.equal(stale.body.toString(), unfixedBody(at, true, NOW + 6 * 60_000));
|
||||
assert.equal((await request()).body, stale.body, 'the stale body is kept too');
|
||||
});
|
||||
|
||||
test('trailing24hWindow spans the clock values that keep the same records', () => {
|
||||
const records = [fire('2026-09-10', '1205'), fire('2026-09-10', '1300'), fire('2026-09-11', '1100'),
|
||||
fire('2026-09-11', '1430'), fire('2026-09-11', '1600'), fire('bad-date', '1006')];
|
||||
const edges = records.map((r) => Date.parse(`${r.acqDate}T${r.acqTime.slice(0, 2)}:${r.acqTime.slice(2)}Z`))
|
||||
.filter(Number.isFinite).flatMap((ms) => [ms - 2 * 3600_000, ms + 24 * 3600_000]);
|
||||
assert.equal(edges.length, 10);
|
||||
const clocks = [NOW, ...edges.flatMap((ms) => [ms - 1, ms, ms + 1])];
|
||||
for (const now of clocks) {
|
||||
const { kept, from, until } = trailing24hWindow(records, now);
|
||||
assert.deepEqual(kept, filterTrailing24h(records, now));
|
||||
assert.ok(from <= now && now <= until, 'the span holds its own clock');
|
||||
for (const other of clocks) {
|
||||
const then = filterTrailing24h(records, other);
|
||||
const same = then.length === kept.length && then.every((r, i) => r === kept[i]);
|
||||
// Before the first record and after the last both keep nothing; the span covers one side.
|
||||
const bothEmpty = then.length === 0 && kept.length === 0;
|
||||
if (other >= from && other <= until) assert.ok(same, `${other} is inside the span of ${now}`);
|
||||
else assert.ok(!same || bothEmpty, `${other} is outside the span of ${now}`);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(trailing24hWindow([], NOW), { kept: [], from: -Infinity, until: Infinity });
|
||||
assert.deepEqual(trailing24hWindow(records, NaN), { kept: [], from: NaN, until: NaN });
|
||||
});
|
||||
@@ -144,35 +144,33 @@ export function createFrames({ state: layerState, services, parts, source }) {
|
||||
* the double-buffer canvas reference, throttled to PROJECTION_TEXTURE_SWAP_MS.
|
||||
*
|
||||
* @param {Object} record - Camera record with an initialized projection runtime.
|
||||
* @returns {boolean} Whether a new image was put on the plane.
|
||||
*/
|
||||
|
||||
function refreshProjectionTextures(record) {
|
||||
const runtime = record?.projection;
|
||||
if (!runtime || runtime.mode === 'video') return false;
|
||||
if (!runtime || runtime.mode === 'video') return;
|
||||
const now = Date.now();
|
||||
if (
|
||||
now - parts.model.safeNumber(runtime.lastTextureSwapAt, 0) <
|
||||
PROJECTION_TEXTURE_SWAP_MS
|
||||
)
|
||||
return false;
|
||||
return;
|
||||
|
||||
const planeShowing = !!(runtime.planeEntity?.show && runtime.planeMaterial);
|
||||
if (!planeShowing) return false;
|
||||
if (!planeShowing) return;
|
||||
|
||||
// Only swap when the canvas content actually changed since the last swap.
|
||||
// Frames land every ~10 s but this runs at 1 Hz — swapping an UNCHANGED
|
||||
// canvas re-uploads the texture for nothing, and each material image
|
||||
// reassignment is a flash opportunity on the live plane (owner field test
|
||||
// 2026-07-04: intermittent white flashes on the monitor plane).
|
||||
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return false;
|
||||
if (runtime.canvasStamp === runtime.lastSwappedCanvasStamp) return;
|
||||
|
||||
const buffer = paintNextProjectionBuffer(runtime);
|
||||
if (!buffer) return false;
|
||||
if (!buffer) return;
|
||||
runtime.lastTextureSwapAt = now;
|
||||
runtime.lastSwappedCanvasStamp = runtime.canvasStamp;
|
||||
runtime.planeMaterial.image = buffer;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -123,11 +123,6 @@ export const CCTV_CALIBRATION_STORAGE_KEY_V2 =
|
||||
|
||||
export const PROJECTION_TEXTURE_SWAP_MS = 1000;
|
||||
|
||||
// A still-image projection changes only when a new still is swapped onto the
|
||||
// plane, so the scene renders for this long after a swap (Cesium binds the new
|
||||
// image over the next frames) and is otherwise left idle.
|
||||
export const PROJECTION_SWAP_RENDER_MS = 500;
|
||||
|
||||
// V2 frustum geometry (design §2a/§6): the far-cap center + corners never sink
|
||||
// below groundAlt + this clearance, so a fabricated pitch (-24°) cannot bury
|
||||
// the monitor plane in the 3D tiles. Exported for the unit suite.
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
PLANE_OUTLINE_COLOR,
|
||||
PROJECTION_CANVAS_WIDTH,
|
||||
PROJECTION_CANVAS_HEIGHT,
|
||||
PROJECTION_SWAP_RENDER_MS,
|
||||
} from './policy.js';
|
||||
|
||||
export function createProjection({
|
||||
@@ -15,15 +14,7 @@ export function createProjection({
|
||||
parts,
|
||||
source,
|
||||
}) {
|
||||
const {
|
||||
holdContinuousRender,
|
||||
releaseContinuousRender,
|
||||
governorRequestRender = () => {},
|
||||
} = services.render;
|
||||
// Until this time (performance.now) the projection loop asks for a frame on
|
||||
// every tick: after a still is swapped onto the plane, and after an edge
|
||||
// that re-arms the loop (activation, projection shown).
|
||||
let renderUntil = 0;
|
||||
const { holdContinuousRender, releaseContinuousRender } = services.render;
|
||||
|
||||
/**
|
||||
* Build the protected label associated with one active monitor plane.
|
||||
@@ -361,9 +352,12 @@ export function createProjection({
|
||||
}
|
||||
|
||||
function startProjectionLoop() {
|
||||
renderUntil = performance.now() + PROJECTION_SWAP_RENDER_MS;
|
||||
if (layerState._projectionRaf) return;
|
||||
if (!parts.model.projectionLoopIsNeeded()) return;
|
||||
// The armed projection loop uploads video textures / runs focus fades per
|
||||
// frame — the scene must render continuously while it runs. Released when
|
||||
// the tick self-stops. (perf wave 2)
|
||||
holdContinuousRender('cctv-projection');
|
||||
|
||||
const tick = () => {
|
||||
if (!layerState._viewer || !parts.model.projectionLoopIsNeeded()) {
|
||||
@@ -372,30 +366,15 @@ export function createProjection({
|
||||
return;
|
||||
}
|
||||
|
||||
const now = performance.now();
|
||||
const restyled = parts.rendering.refreshCctvFocusStyles(now);
|
||||
parts.rendering.refreshCctvFocusStyles(performance.now());
|
||||
|
||||
const active = parts.selection.getActiveRecord();
|
||||
let video = false;
|
||||
if (layerState._enabled && layerState._showProjection && active) {
|
||||
ensureProjectionRuntime(active);
|
||||
if (active.projection && !active.projection.video) {
|
||||
parts.frames.drawProjectionFrame(active);
|
||||
if (parts.frames.refreshProjectionTextures(active))
|
||||
renderUntil = now + PROJECTION_SWAP_RENDER_MS;
|
||||
} else video = Boolean(active.projection?.video);
|
||||
}
|
||||
|
||||
// Only a video feed changes the scene on every frame, so only video
|
||||
// holds the continuous loop. A still lands about every ten seconds and
|
||||
// a focus fade restyles icons every 80 ms; each asks for the frames it
|
||||
// needs. Holding for stills rendered the scene 60 times a second for as
|
||||
// long as a camera was active. (perf wave 2 held for both.)
|
||||
if (video) holdContinuousRender('cctv-projection');
|
||||
else {
|
||||
releaseContinuousRender('cctv-projection');
|
||||
if (restyled || now < renderUntil)
|
||||
governorRequestRender('cctv-projection');
|
||||
parts.frames.refreshProjectionTextures(active);
|
||||
}
|
||||
}
|
||||
|
||||
layerState._projectionRaf = requestAnimationFrame(tick);
|
||||
|
||||
@@ -1,211 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createProjection } from './projection.js';
|
||||
import { createFrames } from './frames.js';
|
||||
import {
|
||||
PROJECTION_SWAP_RENDER_MS,
|
||||
PROJECTION_TEXTURE_SWAP_MS,
|
||||
} from './policy.js';
|
||||
|
||||
/** The projection loop on a hand-cranked clock, with the render governor's calls recorded. */
|
||||
function harness(t, { video = false, showProjection = true } = {}) {
|
||||
let clock = 1000;
|
||||
const frames = [];
|
||||
const original = {
|
||||
now: performance.now,
|
||||
request: globalThis.requestAnimationFrame,
|
||||
cancel: globalThis.cancelAnimationFrame,
|
||||
};
|
||||
performance.now = () => clock;
|
||||
globalThis.requestAnimationFrame = (step) => frames.push(step);
|
||||
globalThis.cancelAnimationFrame = () => frames.splice(0);
|
||||
t.after(() => {
|
||||
performance.now = original.now;
|
||||
globalThis.requestAnimationFrame = original.request;
|
||||
globalThis.cancelAnimationFrame = original.cancel;
|
||||
});
|
||||
|
||||
const holds = new Set();
|
||||
const calls = { hold: 0, requests: 0, draws: 0 };
|
||||
const record = {
|
||||
camera: { id: 'cam-1' },
|
||||
projection: { video: video ? {} : null, mode: video ? 'video' : 'image' },
|
||||
};
|
||||
const scene = { needed: true, swap: false, restyled: false };
|
||||
const state = {
|
||||
_viewer: {},
|
||||
_enabled: true,
|
||||
_showProjection: showProjection,
|
||||
_projectionRaf: 0,
|
||||
_projectionEntities: [],
|
||||
};
|
||||
const projection = createProjection({
|
||||
state,
|
||||
services: {
|
||||
render: {
|
||||
holdContinuousRender(owner) {
|
||||
calls.hold++;
|
||||
holds.add(owner);
|
||||
},
|
||||
releaseContinuousRender: (owner) => holds.delete(owner),
|
||||
governorRequestRender: () => calls.requests++,
|
||||
},
|
||||
},
|
||||
parts: {
|
||||
model: { projectionLoopIsNeeded: () => scene.needed },
|
||||
rendering: { refreshCctvFocusStyles: () => scene.restyled },
|
||||
selection: { getActiveRecord: () => record },
|
||||
frames: {
|
||||
drawProjectionFrame: () => calls.draws++,
|
||||
refreshProjectionTextures() {
|
||||
const swapped = scene.swap;
|
||||
scene.swap = false;
|
||||
return swapped;
|
||||
},
|
||||
},
|
||||
},
|
||||
source: {},
|
||||
});
|
||||
return {
|
||||
projection,
|
||||
state,
|
||||
record,
|
||||
scene,
|
||||
holds,
|
||||
calls,
|
||||
/** Run `count` animation frames 16 ms apart; returns the frames requested in them. */
|
||||
run(count = 1) {
|
||||
const before = calls.requests;
|
||||
for (let i = 0; i < count; i++) {
|
||||
clock += 16;
|
||||
frames.shift()?.();
|
||||
}
|
||||
return calls.requests - before;
|
||||
},
|
||||
advance(ms) {
|
||||
clock += ms;
|
||||
},
|
||||
get queued() {
|
||||
return frames.length;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('a still-image camera never holds the continuous render loop', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(600);
|
||||
assert.equal(h.calls.hold, 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
assert.equal(h.calls.draws, 600, 'the loop still polls for a new still');
|
||||
assert.equal(h.queued, 1, 'and stays armed');
|
||||
});
|
||||
|
||||
test('a new still on the plane is rendered, then the scene goes idle again', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
// Arming the loop (activation, projection shown) renders its first frames.
|
||||
assert.ok(h.run(5) === 5);
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||
assert.equal(h.run(120), 0, 'an unchanged still asks for no frame');
|
||||
|
||||
h.scene.swap = true;
|
||||
assert.equal(h.run(1), 1, 'the tick that swaps the image asks for a frame');
|
||||
const settling = Math.floor(PROJECTION_SWAP_RENDER_MS / 16) - 2;
|
||||
assert.equal(h.run(settling), settling, 'and so do the ticks while it binds');
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS);
|
||||
assert.equal(h.run(120), 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('activating another camera while the loop runs renders its first frames', (t) => {
|
||||
const h = harness(t);
|
||||
h.projection.startProjectionLoop();
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||
assert.equal(h.run(10), 0);
|
||||
h.projection.startProjectionLoop();
|
||||
assert.equal(h.run(10), 10);
|
||||
assert.equal(h.queued, 1, 'still one loop');
|
||||
});
|
||||
|
||||
test('a video camera holds continuous render until it stops being video', (t) => {
|
||||
const h = harness(t, { video: true });
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(3);
|
||||
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||
assert.equal(h.calls.draws, 0, 'Cesium updates the video texture itself');
|
||||
// The stream failed over to stills.
|
||||
h.record.projection.video = null;
|
||||
h.run(1);
|
||||
assert.equal(h.holds.size, 0);
|
||||
h.record.projection.video = {};
|
||||
h.run(1);
|
||||
assert.deepEqual([...h.holds], ['cctv-projection']);
|
||||
h.projection.stopProjectionLoop();
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('a focus fade asks for a frame only on the ticks that restyle an icon', (t) => {
|
||||
const h = harness(t, { showProjection: false });
|
||||
h.projection.startProjectionLoop();
|
||||
h.advance(PROJECTION_SWAP_RENDER_MS * 2);
|
||||
assert.equal(h.run(10), 0);
|
||||
h.scene.restyled = true;
|
||||
assert.equal(h.run(4), 4);
|
||||
h.scene.restyled = false;
|
||||
assert.equal(h.run(10), 0);
|
||||
assert.equal(h.holds.size, 0);
|
||||
});
|
||||
|
||||
test('the loop releases and stops when it has no work', (t) => {
|
||||
const h = harness(t, { video: true });
|
||||
h.projection.startProjectionLoop();
|
||||
h.run(2);
|
||||
h.scene.needed = false;
|
||||
h.run(1);
|
||||
assert.equal(h.holds.size, 0);
|
||||
assert.equal(h.state._projectionRaf, 0);
|
||||
assert.equal(h.queued, 0);
|
||||
});
|
||||
|
||||
test('refreshProjectionTextures reports whether it put a new image on the plane', (t) => {
|
||||
const previousDocument = globalThis.document;
|
||||
globalThis.document = {
|
||||
createElement: () => ({
|
||||
getContext: () => ({ clearRect() {}, drawImage() {} }),
|
||||
}),
|
||||
};
|
||||
t.after(() => {
|
||||
if (previousDocument === undefined) delete globalThis.document;
|
||||
else globalThis.document = previousDocument;
|
||||
});
|
||||
const frames = createFrames({
|
||||
state: {},
|
||||
services: {},
|
||||
parts: { model: { safeNumber: (value, fallback) => value ?? fallback } },
|
||||
source: {},
|
||||
});
|
||||
const runtime = {
|
||||
mode: 'image',
|
||||
canvas: {},
|
||||
canvasStamp: 1,
|
||||
planeEntity: { show: true },
|
||||
planeMaterial: { image: null },
|
||||
};
|
||||
const record = { projection: runtime };
|
||||
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||
const first = runtime.planeMaterial.image;
|
||||
assert.ok(first);
|
||||
// Unchanged canvas: nothing to render.
|
||||
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||
assert.equal(runtime.planeMaterial.image, first);
|
||||
runtime.canvasStamp = 2;
|
||||
assert.equal(frames.refreshProjectionTextures(record), true);
|
||||
assert.notEqual(runtime.planeMaterial.image, first);
|
||||
runtime.planeEntity.show = false;
|
||||
runtime.canvasStamp = 3;
|
||||
runtime.lastTextureSwapAt -= PROJECTION_TEXTURE_SWAP_MS;
|
||||
assert.equal(frames.refreshProjectionTextures(record), false);
|
||||
assert.equal(frames.refreshProjectionTextures({ projection: { mode: 'video' } }), false);
|
||||
});
|
||||
@@ -61,8 +61,8 @@ export function createRendering({
|
||||
!layerState._viewer ||
|
||||
!focusPassIsNeeded(target, layerState._activeFocusStyleCount)
|
||||
)
|
||||
return false;
|
||||
if (nowMs - layerState._lastFocusStyleAt < 80) return false;
|
||||
return;
|
||||
if (nowMs - layerState._lastFocusStyleAt < 80) return;
|
||||
layerState._lastFocusStyleAt = nowMs;
|
||||
const scene = layerState._viewer.scene;
|
||||
const camera = layerState._viewer.camera;
|
||||
@@ -82,8 +82,6 @@ export function createRendering({
|
||||
baseColorFor: cameraColorFor,
|
||||
});
|
||||
layerState._activeFocusStyleCount = result.activeCount;
|
||||
// Whether an icon was restyled, so the caller can ask for a frame.
|
||||
return result.writes > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user