mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-23 03:32:19 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dee0d4d2ab | ||
|
|
db7cc785df |
No files matched your search
@@ -0,0 +1,4 @@
|
||||
- Make a whole-globe view of Live Flights about three times faster on a slow
|
||||
CPU. Aircraft at cruise altitude no longer ask for the ground under them on
|
||||
every pass, and a ground floor read no longer formats numbers into text
|
||||
(mazy1998, #1708).
|
||||
@@ -1,2 +0,0 @@
|
||||
- `/api/firms` keeps its response bytes and sends them gzipped instead of
|
||||
serialising the whole fire set for every request (mazy1998, #1711).
|
||||
+7
-10
@@ -2949,7 +2949,13 @@ visits to one place share a key but does not bound how many distinct places a
|
||||
session visits, and the cache previously had no eviction, so a long session
|
||||
retained every coordinate it ever resolved. Eviction is least-recently-used:
|
||||
a consumer read, including a warm hit in a batch lookup, promotes its entry, so
|
||||
the cell being watched is not dropped for having been resolved early. The bound sits far above a city-scale session, so
|
||||
the cell being watched is not dropped for having been resolved early. A
|
||||
synchronous read (`cachedEllipsoidalGround`, `cachedRealEllipsoidalGround`)
|
||||
promotes its entry only once half the bound's worth of stores and promotions
|
||||
have passed since the entry was last newest, so an entry read at least that
|
||||
often is still never evicted while the per-frame floor reads no longer rewrite
|
||||
the cache; those reads find entries through a numeric key and format no
|
||||
strings. The bound sits far above a city-scale session, so
|
||||
normal use never evicts and issues no additional proxy requests. A batch's
|
||||
results are assembled from what that call resolved or found warm, so eviction
|
||||
during a large batch cannot report one of its points as unresolved. Re:Earth and
|
||||
@@ -3297,15 +3303,6 @@ 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,7 +376,6 @@
|
||||
"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"
|
||||
|
||||
+10
-58
@@ -3,12 +3,7 @@ import https from 'node:https';
|
||||
import { promises as fsp } from 'node:fs';
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
import {
|
||||
filterTrailing24h,
|
||||
parseFirmsCsv,
|
||||
trailing24hWindow,
|
||||
} from '../../src/data/firmsCsv.js';
|
||||
import { acceptsGzip, encodedBody, gzipBody } from './common/compress.js';
|
||||
import { filterTrailing24h, parseFirmsCsv } from '../../src/data/firmsCsv.js';
|
||||
import { readResponseTextCapped } from './common/http.js';
|
||||
|
||||
const NULL_BODY_STATUSES = new Set([204, 205, 304]);
|
||||
@@ -184,11 +179,6 @@ 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();
|
||||
|
||||
@@ -269,8 +259,12 @@ export function firmsProxy() {
|
||||
return { at: now, sources, fires };
|
||||
}
|
||||
|
||||
/** Cache entry and the fires to serve from it → response payload. */
|
||||
function buildPayload(entry, stale, 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());
|
||||
return {
|
||||
fetchedAt: entry.at,
|
||||
stale,
|
||||
@@ -281,32 +275,6 @@ 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();
|
||||
@@ -359,22 +327,6 @@ 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();
|
||||
@@ -411,7 +363,7 @@ export function firmsProxy() {
|
||||
|
||||
const entry = mem;
|
||||
if (entry && Date.now() - entry.at < TTL_MS) {
|
||||
await sendPayload(entry, false);
|
||||
sendJson(200, buildPayload(entry, false));
|
||||
return;
|
||||
}
|
||||
// Stale or missing → refresh, single-flight (concurrent requests
|
||||
@@ -437,9 +389,9 @@ export function firmsProxy() {
|
||||
const pending = inflight;
|
||||
const fresh = await pending;
|
||||
if (fresh) {
|
||||
await sendPayload(fresh, false);
|
||||
sendJson(200, buildPayload(fresh, false));
|
||||
} else if (entry) {
|
||||
await sendPayload(entry, true); // upstream down — stale beats empty
|
||||
sendJson(200, buildPayload(entry, true)); // upstream down — stale beats empty
|
||||
} else {
|
||||
sendJson(502, {
|
||||
error: 'firms fetch failed and no cache available',
|
||||
|
||||
+2
-27
@@ -182,44 +182,19 @@ export function acquisitionMsUtc(acqDate, acqTime) {
|
||||
* @returns {Array<Object>} Filtered records (original objects, order preserved).
|
||||
*/
|
||||
export function filterTrailing24h(records, nowMs) {
|
||||
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 };
|
||||
if (!Array.isArray(records) || !Number.isFinite(nowMs)) return [];
|
||||
const oldest = nowMs - WINDOW_MS;
|
||||
const newest = nowMs + FORWARD_SLACK_MS;
|
||||
let from = -Infinity;
|
||||
let until = Infinity;
|
||||
const memo = new Map();
|
||||
const kept = records.filter((record) => {
|
||||
return 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,15 +7,11 @@ import dnsLookup from 'node:dns';
|
||||
import https from 'node:https';
|
||||
import net from 'node:net';
|
||||
import { Readable } from 'node:stream';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import { filterTrailing24h, parseFirmsCsv, trailing24hWindow } from './firmsCsv.js';
|
||||
import { filterTrailing24h, parseFirmsCsv } from './firmsCsv.js';
|
||||
import {
|
||||
describeFetchError,
|
||||
fetchFirms,
|
||||
fetchIpv4,
|
||||
firmsProxy,
|
||||
isAddressSelectionFailure,
|
||||
} from '../../server/providers/firms.js';
|
||||
|
||||
@@ -320,119 +316,3 @@ 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 });
|
||||
});
|
||||
@@ -311,6 +311,10 @@ export const COAST_FLOOR_LIFT_M = 1.5;
|
||||
*/
|
||||
export function floorCoastedPosition(position, cachedGroundFloor) {
|
||||
if (typeof cachedGroundFloor !== 'function') return position;
|
||||
// Most coasting contacts are at cruise, and this runs for each of them on
|
||||
// every fleet pass. One above COAST_FLOOR_CEILING_M cannot be lifted by any
|
||||
// ground, so it needs neither the geodetic conversion nor the floor read.
|
||||
if (clearOfAllGround(position)) return position;
|
||||
const carto = Cesium.Cartographic.fromCartesian(
|
||||
position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
@@ -334,6 +338,36 @@ export function floorCoastedPosition(position, cachedGroundFloor) {
|
||||
|
||||
const _coastCarto = new Cesium.Cartographic();
|
||||
|
||||
/**
|
||||
* @constant {number} No ground reaches this height above the ellipsoid:
|
||||
* Everest's summit is about 8,821 m (8,849 m above a geoid that sits 28 m
|
||||
* below the ellipsoid there).
|
||||
*/
|
||||
export const COAST_FLOOR_CEILING_M = 9000;
|
||||
const _oneOverRadiiSq = Cesium.Ellipsoid.WGS84.oneOverRadiiSquared;
|
||||
const COAST_CLEAR_SCALED_SQ =
|
||||
(1 + COAST_FLOOR_CEILING_M / Cesium.Ellipsoid.WGS84.minimumRadius) ** 2;
|
||||
|
||||
/**
|
||||
* Whether a position is certainly more than COAST_FLOOR_CEILING_M above the
|
||||
* ellipsoid, from three multiplications. Scale the axes so the ellipsoid is
|
||||
* the unit sphere: a point h metres up its surface normal is then at most
|
||||
* 1 + h / (polar radius) from the centre, so one further out than
|
||||
* 1 + ceiling / (polar radius) is above the ceiling. At the equator this
|
||||
* answers yes from about 30 m above the ceiling; at the poles, from the
|
||||
* ceiling itself.
|
||||
* @param {Cesium.Cartesian3} position
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function clearOfAllGround(position) {
|
||||
return (
|
||||
position.x * position.x * _oneOverRadiiSq.x +
|
||||
position.y * position.y * _oneOverRadiiSq.y +
|
||||
position.z * position.z * _oneOverRadiiSq.z >
|
||||
COAST_CLEAR_SCALED_SQ
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seconds to coast past the last report: the elapsed time, bounded by the
|
||||
* source's coast limit and STALE_COAST_MAX_SEC; none for a contact that
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
liftRepeatedGroundFix, synthesizeForwardKinematicsFix,
|
||||
projectGroundArcLatLon, corridorPathLatLon,
|
||||
COURSE_TRACK_ONLY_MPS, COURSE_CHORD_ONLY_MPS, COURSE_MIN_DPS, TURN_MIN_SPEED_MPS,
|
||||
floorCoastedPosition, COAST_FLOOR_CEILING_M, COAST_FLOOR_LIFT_M,
|
||||
} from './motionModel.js';
|
||||
|
||||
const AUSTIN = { lat: 30.2672, lon: -97.7431, alt: 9000 };
|
||||
@@ -341,3 +342,79 @@ test('corridorPathLatLon returns nothing without a display position', () => {
|
||||
extrapolating: true, displayLat: NaN, displayLon: -97.66, courseDeg: 0, speedMps: 10, lookaheadSec: 60,
|
||||
}), []);
|
||||
});
|
||||
|
||||
/** floorCoastedPosition as it was before the cruise skip: always converts and reads. */
|
||||
function floorCoastedReference(position, groundAt) {
|
||||
const carto = Cesium.Cartographic.fromCartesian(position);
|
||||
if (!carto) return position;
|
||||
const ground = groundAt(
|
||||
Cesium.Math.toDegrees(carto.latitude),
|
||||
Cesium.Math.toDegrees(carto.longitude),
|
||||
);
|
||||
if (!Number.isFinite(ground) || carto.height >= ground + COAST_FLOOR_LIFT_M)
|
||||
return position;
|
||||
return Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, ground + COAST_FLOOR_LIFT_M);
|
||||
}
|
||||
|
||||
test('a coasting contact above any terrain is not looked up against the ground', () => {
|
||||
let reads = 0;
|
||||
const ground = () => {
|
||||
reads += 1;
|
||||
return 8821; // Everest's summit, the highest floor there is
|
||||
};
|
||||
// Just over the ceiling and at cruise, from pole to equator.
|
||||
for (let lat = -90; lat <= 90; lat += 7.5) {
|
||||
for (const lon of [0, 86.925, -78.8, 179.9]) {
|
||||
for (const height of [COAST_FLOOR_CEILING_M + 35, 10_000, 12_500, 40_000]) {
|
||||
const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
|
||||
const before = Cesium.Cartesian3.clone(position);
|
||||
assert.equal(floorCoastedPosition(position, ground), position);
|
||||
assert.ok(Cesium.Cartesian3.equals(position, before));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.equal(reads, 0);
|
||||
});
|
||||
|
||||
test('below the ceiling a coast is floored exactly as before', () => {
|
||||
// Terrain that rises under the coast, known in some places and not others.
|
||||
const ground = (lat, lon) => (Math.floor(lat * 10 + lon) % 3 === 0 ? null : 1500 + ((lat * 7 + lon * 3) % 900));
|
||||
let lifted = 0;
|
||||
let checked = 0;
|
||||
for (let lat = -89; lat <= 89; lat += 5.5) {
|
||||
for (let lon = -180; lon < 180; lon += 17) {
|
||||
for (const height of [-40, 0, 800, 1499, 1501.4, 2399, 2402, 5000, 8990, 9000, 9010, 11_000, 30_000]) {
|
||||
const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
|
||||
const expected = floorCoastedReference(Cesium.Cartesian3.clone(position), ground);
|
||||
const actual = floorCoastedPosition(position, ground);
|
||||
assert.ok(
|
||||
Cesium.Cartesian3.equals(actual, expected),
|
||||
`coast at ${lat}, ${lon}, ${height} m`,
|
||||
);
|
||||
checked += 1;
|
||||
if (!Cesium.Cartesian3.equals(expected, Cesium.Cartesian3.fromDegrees(lon, lat, height))) lifted += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.ok(lifted > 500, `${lifted} of ${checked} coasts were lifted onto the ground`);
|
||||
});
|
||||
|
||||
test('nothing at or below the ceiling skips the ground', () => {
|
||||
let reads = 0;
|
||||
const ground = () => {
|
||||
reads += 1;
|
||||
return null;
|
||||
};
|
||||
let asked = 0;
|
||||
for (let lat = -90; lat <= 90; lat += 2.5) {
|
||||
for (const height of [-400, 0, 4000, 8821, COAST_FLOOR_CEILING_M - 1, COAST_FLOOR_CEILING_M]) {
|
||||
floorCoastedPosition(Cesium.Cartesian3.fromDegrees(33, lat, height), ground);
|
||||
asked += 1;
|
||||
}
|
||||
}
|
||||
assert.equal(reads, asked);
|
||||
});
|
||||
|
||||
test('the ceiling clears the highest ground with room to spare', () => {
|
||||
assert.ok(COAST_FLOOR_CEILING_M > 8849 + 100);
|
||||
});
|
||||
+55
-11
@@ -45,6 +45,39 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
return { lat: Number(lat.toFixed(3)), lon: Number(lon.toFixed(3)) };
|
||||
}
|
||||
|
||||
/**
|
||||
* One coordinate's coarse cell as an integer: `Number(deg.toFixed(3))` in
|
||||
* thousandths of a degree, without the trip through a string. The floor is
|
||||
* read for every aircraft on every fleet pass, and `coarseFloorCoord`'s two
|
||||
* `toFixed` calls and the string keys built from them were most of the cost.
|
||||
* `toFixed` rounds the exact decimal value; `deg * 1000` is within 1e-10 of
|
||||
* it for |deg| < 1000, so the two can only round differently right at a
|
||||
* half, and there the string decides.
|
||||
* @param {number} deg
|
||||
* @returns {number}
|
||||
*/
|
||||
function coarseCellIndex(deg) {
|
||||
const scaled = deg * 1000;
|
||||
if (
|
||||
!(Math.abs(deg) < 1000) ||
|
||||
Math.abs(scaled - Math.floor(scaled) - 0.5) < 1e-6
|
||||
)
|
||||
return Math.round(Number(deg.toFixed(3)) * 1000);
|
||||
return Math.round(scaled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Key of a coarse cell in `_meshCells`: the two indices packed into one
|
||||
* number, or the text form for a longitude too far out of range to pack.
|
||||
* @param {number} latIndex @param {number} lonIndex
|
||||
* @returns {number|string}
|
||||
*/
|
||||
function meshCellKey(latIndex, lonIndex) {
|
||||
return Math.abs(lonIndex) < 500_000 && Math.abs(latIndex) < 1e9
|
||||
? latIndex * 1_000_000 + lonIndex
|
||||
: `${latIndex},${lonIndex}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure floor clamp.
|
||||
* - finite alt + finite ground → max(alt, ground + lift)
|
||||
@@ -424,8 +457,8 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
// reported here by meshFloorSampler.js. Design principle (owner): NEVER
|
||||
// below the visible surface; slightly above is always fine. The DEM stays
|
||||
// the instant global prior/fallback and the sanity gate for samples.
|
||||
/** @type {Map<string, Array<{lat: number, lon: number, h: number}>>}
|
||||
* coarse cell key -> rendered-surface readings (m, ellipsoidal), each kept
|
||||
/** @type {Map<number|string, Array<{lat: number, lon: number, h: number}>>}
|
||||
* coarse cell key (`meshCellKey`) -> rendered-surface readings (m, ellipsoidal), each kept
|
||||
* at the point it was probed. A read answers from the reading nearest the
|
||||
* asked point, so a viaduct and the street beside it in one cell keep their
|
||||
* own heights. */
|
||||
@@ -468,8 +501,7 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
!Number.isFinite(heightM)
|
||||
)
|
||||
return;
|
||||
const c = coarseFloorCoord(lat, lon);
|
||||
const key = `${c.lat},${c.lon}`;
|
||||
const key = meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon));
|
||||
if (!_meshCells.has(key)) _meshCells.set(key, [{ lat, lon, h: heightM }]);
|
||||
}
|
||||
|
||||
@@ -497,7 +529,7 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
const cell = coarseFloorCoord(lat, lon);
|
||||
const prior = cachedRealEllipsoidalGround(cell.lat, cell.lon);
|
||||
if (!meshFloorSampleWithinPrior(heightM, prior)) return false;
|
||||
const key = `${cell.lat},${cell.lon}`;
|
||||
const key = meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon));
|
||||
const readings = _meshCells.get(key);
|
||||
if (!readings) {
|
||||
_meshCells.set(key, [{ lat, lon, h: heightM }]);
|
||||
@@ -538,8 +570,16 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
function cachedMeshFloor(lat, lon) {
|
||||
if (!_meshPreferred) return null;
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
||||
const c = coarseFloorCoord(lat, lon);
|
||||
const readings = _meshCells.get(`${c.lat},${c.lon}`);
|
||||
return meshFloorInCell(
|
||||
lat,
|
||||
lon,
|
||||
meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon)),
|
||||
);
|
||||
}
|
||||
|
||||
/** `cachedMeshFloor` for a caller that already has the point's cell key. */
|
||||
function meshFloorInCell(lat, lon, key) {
|
||||
const readings = _meshCells.get(key);
|
||||
if (!readings) return null;
|
||||
if (readings.length === 1) return readings[0].h;
|
||||
const k = Math.cos((lat * Math.PI) / 180) ** 2;
|
||||
@@ -572,10 +612,14 @@ export function createGroundFloor({ terrain, signal }) {
|
||||
*/
|
||||
function cachedGroundFloor(lat, lon) {
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
||||
const mesh = cachedMeshFloor(lat, lon);
|
||||
if (mesh != null) return mesh;
|
||||
const c = coarseFloorCoord(lat, lon);
|
||||
return cachedRealEllipsoidalGround(c.lat, c.lon);
|
||||
// The cell is worked out once, as integers, for both reads.
|
||||
const latIndex = coarseCellIndex(lat);
|
||||
const lonIndex = coarseCellIndex(lon);
|
||||
if (_meshPreferred) {
|
||||
const mesh = meshFloorInCell(lat, lon, meshCellKey(latIndex, lonIndex));
|
||||
if (mesh != null) return mesh;
|
||||
}
|
||||
return cachedRealEllipsoidalGround(latIndex / 1000, lonIndex / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createGroundFloor } from './groundFloor.js';
|
||||
import { createTerrainHeights } from './terrainHeights.js';
|
||||
|
||||
/** Counts Number.prototype.toFixed calls made while `run` executes. */
|
||||
function countToFixed(run) {
|
||||
const original = Number.prototype.toFixed;
|
||||
let calls = 0;
|
||||
Number.prototype.toFixed = function (...args) {
|
||||
calls += 1;
|
||||
return original.apply(this, args);
|
||||
};
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
Number.prototype.toFixed = original;
|
||||
}
|
||||
return calls;
|
||||
}
|
||||
|
||||
/**
|
||||
* The floor read as it was before the cell became an integer: the coarse cell
|
||||
* through `toFixed(3)`, mesh cells and DEM cells under string keys.
|
||||
*/
|
||||
function referenceFloor() {
|
||||
const mesh = new Map();
|
||||
const dem = new Map();
|
||||
const coarse = (lat, lon) => ({
|
||||
lat: Number(lat.toFixed(3)),
|
||||
lon: Number(lon.toFixed(3)),
|
||||
});
|
||||
const demKey = (lat, lon) => `${lat.toFixed(5)},${lon.toFixed(5)}`;
|
||||
return {
|
||||
reportMesh(lat, lon, h) {
|
||||
const c = coarse(lat, lon);
|
||||
const key = `${c.lat},${c.lon}`;
|
||||
if (!mesh.has(key)) mesh.set(key, [{ lat, lon, h }]);
|
||||
},
|
||||
storeDem(lat, lon, h) {
|
||||
dem.set(demKey(lat, lon), h);
|
||||
},
|
||||
read(lat, lon, meshPreferred = true) {
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
||||
const c = coarse(lat, lon);
|
||||
const readings = meshPreferred ? mesh.get(`${c.lat},${c.lon}`) : null;
|
||||
if (readings) return readings[0].h;
|
||||
return dem.get(demKey(c.lat, c.lon)) ?? null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A real terrain cache whose every resolved point takes the height asked for. */
|
||||
async function warmTerrain(points) {
|
||||
const heights = new Map(points.map((p) => [`${p.lat},${p.lon}`, p.h]));
|
||||
const terrain = createTerrainHeights({
|
||||
source: {
|
||||
async getHeights(chunk) {
|
||||
return chunk.map((item) => ({
|
||||
ellipsoid: heights.get(`${item.lat},${item.lon}`),
|
||||
}));
|
||||
},
|
||||
},
|
||||
});
|
||||
await terrain.resolveEllipsoidalGround(
|
||||
points.map(({ lat, lon }) => ({ lat, lon })),
|
||||
);
|
||||
return terrain;
|
||||
}
|
||||
|
||||
/** Deterministic pseudo-random numbers in [0, 1). */
|
||||
function random(seed) {
|
||||
let state = seed;
|
||||
return () => {
|
||||
state = (state * 1664525 + 1013904223) % 4294967296;
|
||||
return state / 4294967296;
|
||||
};
|
||||
}
|
||||
|
||||
/** Coordinates that land on, and just either side of, a coarse-cell edge. */
|
||||
function edgeCoordinates() {
|
||||
const out = [];
|
||||
for (const base of [0, 0.0005, -0.0005, 12.3455, -12.3455, 89.9995, 179.9995])
|
||||
for (const nudge of [0, 1e-13, -1e-13, 1e-9, -1e-9, 1e-7, -1e-7, 4e-4])
|
||||
out.push(base + nudge);
|
||||
// Thousandths whose doubles sit a hair either side of the half.
|
||||
for (let k = -40; k <= 40; k += 1) out.push((k + 0.5) / 1000);
|
||||
out.push(-0, 1.0005, 1.0015, 2.5, -2.5, 1e-7, -1e-7);
|
||||
return out;
|
||||
}
|
||||
|
||||
test('the floor read answers exactly as the string-keyed read did', async () => {
|
||||
const next = random(20261113);
|
||||
const cells = [];
|
||||
for (let i = 0; i < 400; i += 1) {
|
||||
cells.push({
|
||||
lat: Number((next() * 170 - 85).toFixed(3)),
|
||||
lon: Number((next() * 360 - 180).toFixed(3)),
|
||||
h: Math.round(next() * 4000),
|
||||
});
|
||||
}
|
||||
// Cells along the equator and the prime meridian, where a cell index is 0.
|
||||
for (const edge of edgeCoordinates()) {
|
||||
const lat = Number(edge.toFixed(3));
|
||||
cells.push({ lat: Math.max(-89, Math.min(89, lat)), lon: 10, h: 77 });
|
||||
cells.push({ lat: 10, lon: lat, h: 88 });
|
||||
}
|
||||
const reference = referenceFloor();
|
||||
for (const c of cells) reference.storeDem(c.lat, c.lon, c.h);
|
||||
const terrain = await warmTerrain(cells);
|
||||
const groundFloor = createGroundFloor({ terrain });
|
||||
|
||||
// A rendered-surface reading in every seventh cell, taken off its centre.
|
||||
cells.forEach((c, i) => {
|
||||
if (i % 7) return;
|
||||
const lat = c.lat + 0.0002;
|
||||
const lon = c.lon - 0.0003;
|
||||
reference.reportMesh(lat, lon, c.h + 3);
|
||||
groundFloor.reportMeshFloorCell(lat, lon, c.h + 3);
|
||||
});
|
||||
|
||||
const probes = [];
|
||||
for (const c of cells) {
|
||||
probes.push([c.lat, c.lon]);
|
||||
probes.push([c.lat + (next() - 0.5) * 0.002, c.lon + (next() - 0.5) * 0.002]);
|
||||
}
|
||||
for (const a of edgeCoordinates())
|
||||
for (const b of [10, 10.0004, -0, 0.0005, -0.0005]) {
|
||||
probes.push([a, b]);
|
||||
probes.push([b, a]);
|
||||
}
|
||||
probes.push([NaN, 1], [1, Infinity], [95, 400], [10, 720.1234]);
|
||||
|
||||
for (const meshPreferred of [true, false]) {
|
||||
groundFloor.setMeshFloorPreferred(meshPreferred);
|
||||
let hits = 0;
|
||||
for (const [lat, lon] of probes) {
|
||||
const expected = reference.read(lat, lon, meshPreferred);
|
||||
assert.equal(
|
||||
groundFloor.cachedGroundFloor(lat, lon),
|
||||
expected,
|
||||
`floor at ${lat}, ${lon} (mesh ${meshPreferred})`,
|
||||
);
|
||||
if (expected !== null) hits += 1;
|
||||
}
|
||||
assert.ok(hits > 400, `${hits} probes found a floor`);
|
||||
}
|
||||
});
|
||||
|
||||
test('reading a floor formats no numbers, hit or miss', async () => {
|
||||
const cell = { lat: 51.47, lon: -0.454, h: 25 };
|
||||
const terrain = await warmTerrain([cell]);
|
||||
const groundFloor = createGroundFloor({ terrain });
|
||||
groundFloor.reportMeshFloorCell(48.3537, 11.7861, 453);
|
||||
|
||||
const calls = countToFixed(() => {
|
||||
// The DEM cell, a rendered-surface cell, and open ocean with neither.
|
||||
assert.equal(groundFloor.cachedGroundFloor(51.4702, -0.4541), 25);
|
||||
assert.equal(groundFloor.cachedGroundFloor(48.3538, 11.7862), 453);
|
||||
for (let i = 0; i < 1000; i += 1)
|
||||
assert.equal(groundFloor.cachedGroundFloor(30 + i * 0.011, -40.2), null);
|
||||
});
|
||||
// Before, each read made six toFixed calls (the cell twice, the DEM key).
|
||||
assert.equal(calls, 0);
|
||||
});
|
||||
|
||||
test('a reading reported at a point is found from anywhere in its cell', async () => {
|
||||
const terrain = await warmTerrain([{ lat: 41.5, lon: -97.66, h: 120 }]);
|
||||
const groundFloor = createGroundFloor({ terrain });
|
||||
assert.equal(groundFloor.reportValidatedMeshFloorCell(41.5003, -97.6602, 129), true);
|
||||
assert.equal(groundFloor.cachedMeshFloor(41.4996, -97.6598), 129);
|
||||
assert.equal(groundFloor.cachedGroundFloor(41.4996, -97.6598), 129);
|
||||
// The next cell over has no reading and no DEM entry.
|
||||
assert.equal(groundFloor.cachedMeshFloor(41.5006, -97.6602), null);
|
||||
assert.equal(groundFloor.cachedGroundFloor(41.5006, -97.6602), null);
|
||||
});
|
||||
@@ -86,6 +86,89 @@ export function createTerrainHeights({
|
||||
*/
|
||||
const cache = new Map();
|
||||
|
||||
/**
|
||||
* The same entries under a numeric key, for the synchronous reads. Those run
|
||||
* for every aircraft on every fleet pass (about 94,000 a second with 7,500
|
||||
* aircraft in view), nearly all of them misses, and building the string key
|
||||
* (two `toFixed` calls and a concatenation) was most of what a read cost.
|
||||
* Kept in step with `cache` by `cacheStore`, eviction and the abort handler.
|
||||
* `at` is the value of `moves` when the entry last became the newest.
|
||||
* @type {Map<number, {key: string, entry: object, at: number}>}
|
||||
*/
|
||||
const cells = new Map();
|
||||
/** Counts every move to the newest end of `cache`: stores and promotions. */
|
||||
let moves = 0;
|
||||
/**
|
||||
* A read promotes its entry only once this many moves have passed since it
|
||||
* was last the newest. An entry cannot reach the cold end in fewer than
|
||||
* CACHE_MAX_ENTRIES - 1 moves, so one read at least this often is never
|
||||
* evicted, without rewriting the Map on every read.
|
||||
*/
|
||||
const PROMOTE_AFTER_MOVES = Math.floor(CACHE_MAX_ENTRIES / 2);
|
||||
|
||||
/**
|
||||
* `value.toFixed(5)` as an integer count of 1e-5 steps, or null when the
|
||||
* fast arithmetic cannot be trusted to match it. `toFixed` rounds the exact
|
||||
* decimal value; `value * 1e5` is within 2e-8 of it for |value| < 1000, so
|
||||
* the two can only round differently this close to a half. A negative value
|
||||
* that rounds to zero is left to the string too: it formats as "-0.00000",
|
||||
* a different key from "0.00000".
|
||||
* @param {number} value
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function fixed5Index(value) {
|
||||
if (!(Math.abs(value) < 1000)) return null;
|
||||
const scaled = value * 1e5;
|
||||
if (Math.abs(scaled - Math.floor(scaled) - 0.5) < 1e-6) return null;
|
||||
const index = Math.round(scaled);
|
||||
return Object.is(index, -0) ? null : index;
|
||||
}
|
||||
|
||||
/** Indices at or past this (500 degrees) do not pack into one integer. */
|
||||
const CELL_INDEX_LIMIT = 5e7;
|
||||
|
||||
/**
|
||||
* The numeric key for a coordinate: its two 5-decimal indices packed into
|
||||
* one integer (below 2^53), or null when the coordinate has to go through
|
||||
* its string key instead.
|
||||
* @param {number} lat
|
||||
* @param {number} lon
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function cellKey(lat, lon) {
|
||||
const latIndex = fixed5Index(lat);
|
||||
const lonIndex = fixed5Index(lon);
|
||||
if (
|
||||
latIndex === null ||
|
||||
lonIndex === null ||
|
||||
Math.abs(latIndex) >= CELL_INDEX_LIMIT ||
|
||||
Math.abs(lonIndex) >= CELL_INDEX_LIMIT
|
||||
)
|
||||
return null;
|
||||
return latIndex * 1e8 + lonIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* The numeric key of a stored string key. The text is the rounded
|
||||
* coordinate itself, so this is exact. Null for a key `cellKey` never
|
||||
* produces; such a key is only ever read through its string.
|
||||
* @param {string} key
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function cellKeyOf(key) {
|
||||
const comma = key.indexOf(',');
|
||||
const latIndex = Math.round(Number(key.slice(0, comma)) * 1e5);
|
||||
const lonIndex = Math.round(Number(key.slice(comma + 1)) * 1e5);
|
||||
if (
|
||||
!(Math.abs(latIndex) < CELL_INDEX_LIMIT) ||
|
||||
!(Math.abs(lonIndex) < CELL_INDEX_LIMIT) ||
|
||||
Object.is(latIndex, -0) ||
|
||||
Object.is(lonIndex, -0)
|
||||
)
|
||||
return null;
|
||||
return latIndex * 1e8 + lonIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an entry and drop the coldest ones once past the cap. `Map` iterates
|
||||
* in insertion order, so deleting the first key evicts the least recently
|
||||
@@ -101,10 +184,15 @@ export function createTerrainHeights({
|
||||
function cacheStore(key, entry) {
|
||||
cache.delete(key);
|
||||
cache.set(key, entry);
|
||||
moves += 1;
|
||||
const cell = cellKeyOf(key);
|
||||
if (cell !== null) cells.set(cell, { key, entry, at: moves });
|
||||
while (cache.size > CACHE_MAX_ENTRIES) {
|
||||
const coldest = cache.keys().next().value;
|
||||
if (coldest === undefined) break;
|
||||
cache.delete(coldest);
|
||||
const coldestCell = cellKeyOf(coldest);
|
||||
if (coldestCell !== null) cells.delete(coldestCell);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,9 +207,36 @@ export function createTerrainHeights({
|
||||
if (entry === undefined) return undefined;
|
||||
cache.delete(key);
|
||||
cache.set(key, entry);
|
||||
moves += 1;
|
||||
const cell = cellKeyOf(key);
|
||||
const held = cell === null ? undefined : cells.get(cell);
|
||||
if (held) held.at = moves;
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* The synchronous read: the entry for a coordinate, or undefined on a miss.
|
||||
* Same answer as `cacheTouch(cacheKey(lat, lon))`, without building the
|
||||
* string, and promoting only an entry that has drifted towards the cold end
|
||||
* (see PROMOTE_AFTER_MOVES).
|
||||
* @param {number} lat
|
||||
* @param {number} lon
|
||||
* @returns {{ellipsoid: number, source: 'reearth'|'geoid-fallback', retryAt?: number}|undefined}
|
||||
*/
|
||||
function cacheRead(lat, lon) {
|
||||
const cell = cellKey(lat, lon);
|
||||
if (cell === null) return cacheTouch(cacheKey(lat, lon));
|
||||
const held = cells.get(cell);
|
||||
if (held === undefined) return undefined;
|
||||
if (moves - held.at > PROMOTE_AFTER_MOVES) {
|
||||
cache.delete(held.key);
|
||||
cache.set(held.key, held.entry);
|
||||
moves += 1;
|
||||
held.at = moves;
|
||||
}
|
||||
return held.entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the rounded cache key shared between the in-memory cache and the
|
||||
* `points` string sent to the proxy (see file header, point 2).
|
||||
@@ -142,7 +257,7 @@ export function createTerrainHeights({
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function cachedEllipsoidalGround(lat, lon) {
|
||||
const entry = cacheTouch(cacheKey(lat, lon));
|
||||
const entry = cacheRead(lat, lon);
|
||||
return entry ? entry.ellipsoid : null;
|
||||
}
|
||||
|
||||
@@ -159,7 +274,7 @@ export function createTerrainHeights({
|
||||
* @returns {number|null}
|
||||
*/
|
||||
function cachedRealEllipsoidalGround(lat, lon) {
|
||||
const entry = cacheTouch(cacheKey(lat, lon));
|
||||
const entry = cacheRead(lat, lon);
|
||||
return entry && entry.source === 'reearth' ? entry.ellipsoid : null;
|
||||
}
|
||||
|
||||
@@ -370,7 +485,14 @@ export function createTerrainHeights({
|
||||
});
|
||||
}
|
||||
|
||||
signal?.addEventListener('abort', () => cache.clear(), { once: true });
|
||||
signal?.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
cache.clear();
|
||||
cells.clear();
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
return {
|
||||
cachedEllipsoidalGround,
|
||||
cachedRealEllipsoidalGround,
|
||||
|
||||
@@ -169,3 +169,151 @@ test('an out-of-range cache bound falls back to the default instead of disabling
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
/** Counts Number.prototype.toFixed calls made while `run` executes. */
|
||||
function countToFixed(run) {
|
||||
const original = Number.prototype.toFixed;
|
||||
let calls = 0;
|
||||
Number.prototype.toFixed = function (...args) {
|
||||
calls += 1;
|
||||
return original.apply(this, args);
|
||||
};
|
||||
try {
|
||||
run();
|
||||
} finally {
|
||||
Number.prototype.toFixed = original;
|
||||
}
|
||||
return calls;
|
||||
}
|
||||
|
||||
test('a synchronous read finds exactly the entry its 5-decimal key names', async () => {
|
||||
// Stored coordinates, and for each the reads that must and must not reach it.
|
||||
// The expectation is the string key the cache has always used.
|
||||
const stored = [
|
||||
[0, 0.001],
|
||||
[51.47, -0.454],
|
||||
[-33.94611, 151.17722],
|
||||
[0.00001, 0.00002],
|
||||
[-0.00001, 0.00003],
|
||||
[89.99999, 179.99999],
|
||||
[-89.99999, -179.99999],
|
||||
[12.345675, 98.765435],
|
||||
[1e-7, 5],
|
||||
[-1e-7, 6],
|
||||
[7, -1e-7],
|
||||
[200.5, 361.25],
|
||||
[10, 500],
|
||||
[10, 499.999999],
|
||||
];
|
||||
const key = (lat, lon) => `${lat.toFixed(5)},${lon.toFixed(5)}`;
|
||||
const heights = new Map(
|
||||
stored.map(([lat, lon], i) => [key(lat, lon), 100 + i]),
|
||||
);
|
||||
const terrain = createTerrainHeights({
|
||||
source: {
|
||||
async getHeights(chunk) {
|
||||
return chunk.map((item) => ({ ellipsoid: heights.get(item.key) }));
|
||||
},
|
||||
},
|
||||
});
|
||||
await terrain.resolveEllipsoidalGround(
|
||||
stored.map(([lat, lon]) => ({ lat, lon })),
|
||||
);
|
||||
|
||||
const probes = [];
|
||||
const nudges = [
|
||||
0, 1e-13, -1e-13, 1e-9, -1e-9, 4e-6, -4e-6, 4.9999e-6, 5e-6, -5e-6,
|
||||
5.0001e-6, 1e-5, -1e-5,
|
||||
];
|
||||
for (const [lat, lon] of stored)
|
||||
for (const a of nudges)
|
||||
for (const b of [0, 4e-6, -5e-6])
|
||||
probes.push([lat + a, lon + b], [lat + b, lon + a]);
|
||||
probes.push([0, 0], [-0, 0.001], [0, -0], [NaN, 1], [1, Infinity], [1e21, 2]);
|
||||
|
||||
let hits = 0;
|
||||
for (const [lat, lon] of probes) {
|
||||
const expected = heights.get(key(lat, lon)) ?? null;
|
||||
assert.equal(
|
||||
terrain.cachedEllipsoidalGround(lat, lon),
|
||||
expected,
|
||||
`plain read at ${lat}, ${lon}`,
|
||||
);
|
||||
assert.equal(
|
||||
terrain.cachedRealEllipsoidalGround(lat, lon),
|
||||
expected,
|
||||
`real read at ${lat}, ${lon}`,
|
||||
);
|
||||
if (expected !== null) hits += 1;
|
||||
}
|
||||
assert.ok(
|
||||
hits > 100 && hits < probes.length,
|
||||
`${hits} of ${probes.length} reads hit`,
|
||||
);
|
||||
});
|
||||
|
||||
test('a synchronous read of an ordinary coordinate formats no numbers', async () => {
|
||||
const terrain = createTerrainHeights({ source: stubSource() });
|
||||
await terrain.resolveEllipsoidalGround([pointAt(1), pointAt(2)]);
|
||||
const calls = countToFixed(() => {
|
||||
assert.equal(
|
||||
terrain.cachedRealEllipsoidalGround(0, lonAt(1)),
|
||||
heightAt(lonAt(1)),
|
||||
);
|
||||
assert.equal(
|
||||
terrain.cachedEllipsoidalGround(0, lonAt(2)),
|
||||
heightAt(lonAt(2)),
|
||||
);
|
||||
// Misses are the common case: an aircraft over a cell nobody resolved.
|
||||
for (let i = 10; i < 1010; i += 1)
|
||||
assert.equal(terrain.cachedRealEllipsoidalGround(45.123, lonAt(i)), null);
|
||||
});
|
||||
// Before, every read built its key with two toFixed calls.
|
||||
assert.equal(calls, 0);
|
||||
});
|
||||
|
||||
test('an entry read again and again is not rewritten on each read, and still outlives the rest', async () => {
|
||||
const terrain = createTerrainHeights({
|
||||
source: stubSource(),
|
||||
maxCacheEntries: 8,
|
||||
});
|
||||
for (let i = 0; i < 8; i += 1)
|
||||
await terrain.resolveEllipsoidalGround([pointAt(i)]);
|
||||
|
||||
// Coordinate 0 is the coldest. Read it between stores, as a parked contact
|
||||
// would; the seven newer ones are never read.
|
||||
for (let i = 8; i < 15; i += 1) {
|
||||
for (let read = 0; read < 50; read += 1)
|
||||
assert.equal(
|
||||
terrain.cachedEllipsoidalGround(0, lonAt(0)),
|
||||
heightAt(lonAt(0)),
|
||||
);
|
||||
await terrain.resolveEllipsoidalGround([pointAt(i)]);
|
||||
}
|
||||
assert.equal(
|
||||
terrain.cachedEllipsoidalGround(0, lonAt(0)),
|
||||
heightAt(lonAt(0)),
|
||||
);
|
||||
for (let i = 1; i < 8; i += 1)
|
||||
assert.equal(
|
||||
terrain.cachedEllipsoidalGround(0, lonAt(i)),
|
||||
null,
|
||||
`unread coordinate ${i} was evicted`,
|
||||
);
|
||||
});
|
||||
|
||||
test('an evicted coordinate is gone from the synchronous read too', async () => {
|
||||
const terrain = createTerrainHeights({
|
||||
source: stubSource(),
|
||||
maxCacheEntries: 2,
|
||||
});
|
||||
for (let i = 0; i < 5; i += 1)
|
||||
await terrain.resolveEllipsoidalGround([pointAt(i)]);
|
||||
for (let i = 0; i < 3; i += 1)
|
||||
assert.equal(terrain.cachedRealEllipsoidalGround(0, lonAt(i)), null);
|
||||
for (let i = 3; i < 5; i += 1)
|
||||
assert.equal(
|
||||
terrain.cachedRealEllipsoidalGround(0, lonAt(i)),
|
||||
heightAt(lonAt(i)),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user