mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Contacts move back as smoothly as they move on
A snapshot timeline may declare spanMs: each record of a snapshot at a time then carries its own fixes from spanMs before the time to spanMs after it. Flights, military flights and vessels following the clock are placed on those fixes, so a seek or a move back within the fixes loaded is no jump: nothing is cleared and play does not wait. The step there is asked for once and stands in for every contact; those it has keep their place. Fixes are merged across snapshots at least 20 s apart, at most 240 a contact, and a contact is not drawn more than a minute before its first fix.
This commit is contained in:
1 parent
b8a320edce
commit
2b5fb51684
27 files changed
+1377
-182
No files matched your search
+117
-10
@@ -1,4 +1,5 @@
|
||||
import { readSnapshotTimeline } from '../sources/live/contract.js';
|
||||
import { TIMELINE_HELD_MS } from './timelineFixes.js';
|
||||
|
||||
/** The longest play waits on a snapshot before it goes on without it. */
|
||||
export const SNAPSHOT_WAIT_MS = 8_000;
|
||||
@@ -23,6 +24,16 @@ export const SNAPSHOT_RETRY_MS = 5_000;
|
||||
* step as the clock nears the newest it has, and play does not pass a
|
||||
* snapshot still loading (for at most SNAPSHOT_WAIT_MS). Those snapshots
|
||||
* are applied as polls are (`replace: false`), so contacts move on smoothly.
|
||||
* - When the source's timeline declares `spanMs`, its records carry their
|
||||
* fixes from `spanMs` before a snapshot's time to `spanMs` after it, and
|
||||
* the layer places each contact on them by the clock. A seek or a move
|
||||
* back that stays within the fixes the loaded snapshots carry is then no
|
||||
* jump: nothing is cleared, play does not wait, and contacts move to the
|
||||
* time smoothly. A move to a step before the newest loaded asks for that
|
||||
* step once (`replace: true`): it brings back the contacts that left
|
||||
* after it and drops those not there yet, while those it has keep their
|
||||
* place, as their fixes put them. Paused, a time nearing the end of the
|
||||
* fixes after the newest asks for the snapshot there (`replace: false`).
|
||||
* - Outside the source's coverage the layer is `clear()`ed and nothing loads;
|
||||
* while the coverage is not known yet nothing loads and nothing is cleared.
|
||||
* - Back at live, the layer is `clear()`ed and `resumeLive()` polls at once.
|
||||
@@ -61,8 +72,11 @@ export function createSnapshotTimeline({
|
||||
// Each request's number; an answer to an older one is dropped.
|
||||
let sequence = 0;
|
||||
let inflight = null;
|
||||
// The time of the newest snapshot applied since the last jump.
|
||||
// The times of the newest and oldest snapshots applied since the last jump.
|
||||
let lead = null;
|
||||
let base = null;
|
||||
// The step a move back last asked for, until a newer snapshot applies.
|
||||
let scrubbed = null;
|
||||
// Cleared for a time outside coverage, so it is not cleared again.
|
||||
let empty = false;
|
||||
let waiting = null;
|
||||
@@ -135,23 +149,27 @@ export function createSnapshotTimeline({
|
||||
clear();
|
||||
};
|
||||
|
||||
function request(atMs, replace) {
|
||||
// `quiet`: a move within the loaded fixes, which shows the contacts
|
||||
// already: play does not wait for it, and its failure clears nothing.
|
||||
function request(atMs, replace, quiet = false) {
|
||||
// One load at a time: an older one is dropped before a newer starts.
|
||||
inflight?.controller.abort();
|
||||
const mine = ++sequence;
|
||||
const controller = new AbortController();
|
||||
inflight = { atMs, controller };
|
||||
inflight = { atMs, controller, quiet };
|
||||
const failed = () => {
|
||||
inflight = null;
|
||||
// A time that could not be shown shows nothing, not another time.
|
||||
if (replace) clearOnce();
|
||||
if (replace && !quiet) clearOnce();
|
||||
// Asked again once the retry is due.
|
||||
if (quiet) scrubbed = null;
|
||||
capped = false;
|
||||
stopWaiting();
|
||||
failedUntil = now() + SNAPSHOT_RETRY_MS;
|
||||
stopRetry();
|
||||
retryTimer = setTimeout(() => {
|
||||
retryTimer = null;
|
||||
if (started) follow(clock.getState());
|
||||
resume();
|
||||
}, SNAPSHOT_RETRY_MS);
|
||||
};
|
||||
// The source dropped this request for a newer one of its own: nothing
|
||||
@@ -159,10 +177,11 @@ export function createSnapshotTimeline({
|
||||
// as the clock next moves, or shortly.
|
||||
const superseded = () => {
|
||||
inflight = null;
|
||||
if (quiet) scrubbed = null;
|
||||
stopRetry();
|
||||
retryTimer = setTimeout(() => {
|
||||
retryTimer = null;
|
||||
if (started) follow(clock.getState());
|
||||
resume();
|
||||
}, SNAPSHOT_RETRY_MS);
|
||||
};
|
||||
Promise.resolve()
|
||||
@@ -175,7 +194,7 @@ export function createSnapshotTimeline({
|
||||
return;
|
||||
}
|
||||
inflight = null;
|
||||
lead = atMs;
|
||||
held(atMs, replace);
|
||||
empty = false;
|
||||
capped = false;
|
||||
stopWaiting();
|
||||
@@ -193,10 +212,87 @@ export function createSnapshotTimeline({
|
||||
);
|
||||
}
|
||||
|
||||
// The snapshot at `atMs` is applied: the stretch of snapshot times loaded
|
||||
// (`base` to `lead`) takes it in. One the stretch does not reach with its
|
||||
// fixes (play went on without waiting, far past the newest) starts it
|
||||
// afresh: the contacts have no fixes for the time between. The stretch is
|
||||
// kept to TIMELINE_HELD_MS, from the end just loaded, which every
|
||||
// contact's fixes still cover.
|
||||
function held(atMs, replace) {
|
||||
const reach = 2 * (timeline.spanMs || 0);
|
||||
scrubbed = null;
|
||||
if (
|
||||
replace ||
|
||||
lead === null ||
|
||||
atMs > lead + reach ||
|
||||
atMs < base - reach
|
||||
) {
|
||||
lead = atMs;
|
||||
base = atMs;
|
||||
return;
|
||||
}
|
||||
lead = Math.max(lead, atMs);
|
||||
base = Math.min(base, atMs);
|
||||
if (lead - base <= TIMELINE_HELD_MS) return;
|
||||
if (atMs === lead) base = lead - TIMELINE_HELD_MS;
|
||||
else lead = base + TIMELINE_HELD_MS;
|
||||
}
|
||||
|
||||
// Ask again for what the time needs, after a failure or a dropped load:
|
||||
// a time within the loaded fixes may still need a snapshot near their end.
|
||||
function resume() {
|
||||
if (!started) return;
|
||||
const state = clock.getState();
|
||||
if (!state.live && wasActive && heldAt(state.timeMs))
|
||||
scrubTo(state.timeMs, state);
|
||||
else follow(state);
|
||||
}
|
||||
|
||||
// Whether the fixes of the snapshots loaded since the last jump span the
|
||||
// time, so contacts can be placed there without a jump.
|
||||
const heldAt = (timeMs) =>
|
||||
timeline.spanMs > 0 &&
|
||||
lead !== null &&
|
||||
base !== null &&
|
||||
timeMs >= base - timeline.spanMs &&
|
||||
timeMs <= lead + timeline.spanMs;
|
||||
|
||||
// The clock moved to a time the loaded fixes span: contacts move there as
|
||||
// they are. A step before the newest loaded is asked for once, in place
|
||||
// of every contact; paused, a time in the outer half of the fixes after
|
||||
// the newest asks for the snapshot at its step, which brings those beyond.
|
||||
function scrubTo(timeMs, state) {
|
||||
const covered = span();
|
||||
if (!covered) return;
|
||||
if (!within(timeMs, covered)) {
|
||||
cancel();
|
||||
lead = null;
|
||||
base = null;
|
||||
setGate(null);
|
||||
clearOnce();
|
||||
return;
|
||||
}
|
||||
const half = timeline.spanMs / 2;
|
||||
const step = Math.max(covered.start, stepAtOrBefore(timeMs));
|
||||
if (step < lead) {
|
||||
if (step !== scrubbed && now() >= failedUntil) {
|
||||
scrubbed = step;
|
||||
if (inflight?.atMs !== step) request(step, true, true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!playsOn(state) && timeMs > lead + half && step > lead) {
|
||||
if (inflight?.atMs !== step && now() >= failedUntil) request(step, false);
|
||||
return;
|
||||
}
|
||||
keepUp(timeMs, state);
|
||||
}
|
||||
|
||||
// Show the time afresh: the snapshot at its step replaces every contact.
|
||||
function jumpTo(timeMs) {
|
||||
cancel();
|
||||
lead = null;
|
||||
base = null;
|
||||
setGate(null);
|
||||
const covered = span();
|
||||
if (!covered) return;
|
||||
@@ -215,12 +311,14 @@ export function createSnapshotTimeline({
|
||||
if (!within(timeMs, covered)) {
|
||||
cancel();
|
||||
lead = null;
|
||||
base = null;
|
||||
setGate(null);
|
||||
clearOnce();
|
||||
return;
|
||||
}
|
||||
if (inflight) {
|
||||
if (playsOn(state) && timeMs >= inflight.atMs) startWaiting();
|
||||
if (playsOn(state) && timeMs >= inflight.atMs && !inflight.quiet)
|
||||
startWaiting();
|
||||
return;
|
||||
}
|
||||
if (now() < failedUntil) return;
|
||||
@@ -246,7 +344,10 @@ export function createSnapshotTimeline({
|
||||
return;
|
||||
}
|
||||
setGate(capped ? null : next);
|
||||
request(next, false);
|
||||
// Past what the loaded fixes reach (play went on without waiting), the
|
||||
// contacts have no fixes for the time between: the step stands in for
|
||||
// them all.
|
||||
request(next, timeline.spanMs > 0 && next > lead + 2 * timeline.spanMs);
|
||||
}
|
||||
|
||||
function follow(state) {
|
||||
@@ -259,6 +360,7 @@ export function createSnapshotTimeline({
|
||||
cancel();
|
||||
setGate(null);
|
||||
lead = null;
|
||||
base = null;
|
||||
empty = false;
|
||||
failedUntil = -Infinity;
|
||||
clear();
|
||||
@@ -266,6 +368,8 @@ export function createSnapshotTimeline({
|
||||
return;
|
||||
}
|
||||
const timeMs = state.timeMs;
|
||||
// Away from live already: a move may stay within the loaded fixes.
|
||||
const wasFollowing = wasActive;
|
||||
const jumped =
|
||||
!wasActive ||
|
||||
state.jumps !== lastJumps ||
|
||||
@@ -273,7 +377,8 @@ export function createSnapshotTimeline({
|
||||
wasActive = true;
|
||||
lastJumps = state.jumps;
|
||||
lastTimeMs = timeMs;
|
||||
if (jumped) {
|
||||
if (jumped && wasFollowing && heldAt(timeMs)) scrubTo(timeMs, state);
|
||||
else if (jumped) {
|
||||
failedUntil = -Infinity;
|
||||
jumpTo(timeMs);
|
||||
} else keepUp(timeMs, state);
|
||||
@@ -303,6 +408,7 @@ export function createSnapshotTimeline({
|
||||
lastJumps = null;
|
||||
lastTimeMs = null;
|
||||
lead = null;
|
||||
base = null;
|
||||
empty = false;
|
||||
failedUntil = -Infinity;
|
||||
gated = undefined;
|
||||
@@ -330,6 +436,7 @@ export function createSnapshotTimeline({
|
||||
setGate(null);
|
||||
wasActive = false;
|
||||
lead = null;
|
||||
base = null;
|
||||
for (const release of releases.splice(0).reverse()) release();
|
||||
timeline = null;
|
||||
},
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
const STEP = 10_000;
|
||||
const WALL = Date.UTC(2026, 10, 1, 12);
|
||||
|
||||
function harness({ timeline = true, coverage } = {}) {
|
||||
function harness({ timeline = true, coverage, spanMs } = {}) {
|
||||
const t = {
|
||||
wall: WALL,
|
||||
mono: 1_000,
|
||||
@@ -34,6 +34,7 @@ function harness({ timeline = true, coverage } = {}) {
|
||||
timeline: {
|
||||
coverage: () => span,
|
||||
stepMs: STEP,
|
||||
...(spanMs && { spanMs }),
|
||||
subscribe(listener) {
|
||||
coverageListeners.add(listener);
|
||||
return () => coverageListeners.delete(listener);
|
||||
@@ -335,3 +336,215 @@ test('a source that tells of a better answer has the paused time loaded again, n
|
||||
assert.equal(loads(h.calls).length, live, 'live, nothing is loaded');
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('with fixes either side, a move back within them asks for its step once, clearing nothing and holding nothing', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({ spanMs: SPAN });
|
||||
h.follower.start();
|
||||
const at = WALL - 600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
assert.equal(loads(h.calls).length, 1);
|
||||
// Within the step loaded: the contacts move along their fixes.
|
||||
h.clock.seek(at + STEP / 2);
|
||||
h.clock.seek(at + 1_000);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).length, 1);
|
||||
// Back two steps: the contacts move along their fixes, and the step there
|
||||
// stands in for every contact once it loads, bringing back any that left
|
||||
// after it. Nothing is cleared and play does not wait.
|
||||
const before = h.calls.length;
|
||||
h.clock.seek(at - 2 * STEP);
|
||||
await flush();
|
||||
assert.deepEqual(h.calls.slice(before), [
|
||||
{ type: 'load', atMs: at - 2 * STEP, replace: true },
|
||||
]);
|
||||
assert.equal(h.clock.getState().buffering, false);
|
||||
// Moving within that step while it loads asks for nothing more.
|
||||
h.clock.seek(at - 2 * STEP + 1_000);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).length, 2);
|
||||
await h.answer();
|
||||
h.clock.seek(at - 2 * STEP + 2_000);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).length, 2);
|
||||
// Playing on from there asks for the next step, as play does.
|
||||
h.clock.play();
|
||||
h.advance(STEP - 2_000);
|
||||
h.clock.coverageChanged();
|
||||
await flush();
|
||||
assert.deepEqual(loads(h.calls).at(-1), {
|
||||
type: 'load',
|
||||
atMs: at - STEP,
|
||||
replace: false,
|
||||
});
|
||||
await h.answer();
|
||||
h.clock.pause();
|
||||
// Paused past the outer half after the newest: the step there.
|
||||
const newest = loads(h.calls).at(-1).atMs;
|
||||
h.clock.seek(newest + 3 * STEP + 1_000);
|
||||
await flush();
|
||||
assert.deepEqual(loads(h.calls).at(-1), {
|
||||
type: 'load',
|
||||
atMs: newest + 3 * STEP,
|
||||
replace: false,
|
||||
});
|
||||
await h.answer();
|
||||
// Beyond the fixes is a jump: the time is shown afresh, play held.
|
||||
h.clock.seek(at - 60 * STEP);
|
||||
await flush();
|
||||
assert.deepEqual(loads(h.calls).at(-1), {
|
||||
type: 'load',
|
||||
atMs: at - 60 * STEP,
|
||||
replace: true,
|
||||
});
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('playing, a seek back within the fixes does not hold play on its step', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({ spanMs: SPAN });
|
||||
h.follower.start();
|
||||
const at = WALL - 600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.play();
|
||||
h.clock.seek(at - 2 * STEP + 1_000);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).at(-1).atMs, at - 2 * STEP);
|
||||
h.advance(STEP / 4);
|
||||
h.clock.coverageChanged();
|
||||
await flush();
|
||||
assert.equal(h.clock.getState().buffering, false);
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('without a span every move back is a jump', async () => {
|
||||
const h = harness();
|
||||
h.follower.start();
|
||||
const at = WALL - 600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.seek(at - STEP);
|
||||
await flush();
|
||||
assert.deepEqual(loads(h.calls).at(-1), {
|
||||
type: 'load',
|
||||
atMs: at - STEP,
|
||||
replace: true,
|
||||
});
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('a snapshot the loaded fixes do not reach starts the held stretch afresh', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({ spanMs: SPAN });
|
||||
h.follower.start();
|
||||
const at = WALL - 1_200_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.play();
|
||||
h.advance(100);
|
||||
h.clock.coverageChanged();
|
||||
await flush();
|
||||
// The next step is slow: play goes on without it, far past the fixes.
|
||||
h.advance(2 * STEP);
|
||||
h.clock.coverageChanged();
|
||||
h.advance(SNAPSHOT_WAIT_MS);
|
||||
h.advance(30 * STEP);
|
||||
h.clock.coverageChanged();
|
||||
await h.answer();
|
||||
const far = loads(h.calls).at(-1).atMs;
|
||||
assert.ok(far > at + STEP + 2 * SPAN, 'asked for a step past the fixes');
|
||||
assert.equal(
|
||||
loads(h.calls).at(-1).replace,
|
||||
true,
|
||||
'in place of every contact',
|
||||
);
|
||||
await h.answer();
|
||||
h.clock.pause();
|
||||
// Back between the two: no snapshot's fixes cover it, so it is a jump.
|
||||
h.clock.seek(at + SPAN + 2 * STEP);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).at(-1).replace, true);
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('the held stretch is kept to the newest stretch every contact covers', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({
|
||||
spanMs: SPAN,
|
||||
coverage: { start: WALL - 6 * 3_600_000, end: WALL },
|
||||
});
|
||||
h.follower.start();
|
||||
const at = WALL - 3 * 3_600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.play();
|
||||
// Playing on for well over TIMELINE_HELD_MS, a step at a time.
|
||||
for (let k = 0; k < 160; k++) {
|
||||
h.advance(STEP);
|
||||
h.clock.coverageChanged();
|
||||
await flush();
|
||||
while (h.pending.length) await h.answer(h.pending.length - 1);
|
||||
}
|
||||
h.clock.pause();
|
||||
// Back to the first snapshot's time: past what is held, so a jump.
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).at(-1).replace, true);
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('a step a move back asked for that the source drops is asked again', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({ spanMs: SPAN });
|
||||
h.follower.start();
|
||||
const at = WALL - 600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.seek(at - 2 * STEP);
|
||||
await flush();
|
||||
const asked = loads(h.calls).length;
|
||||
h.pending
|
||||
.shift()
|
||||
.reject(Object.assign(new Error('dropped'), { name: 'AbortError' }));
|
||||
await flush();
|
||||
h.advance(SNAPSHOT_RETRY_MS);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).length, asked + 1);
|
||||
assert.equal(loads(h.calls).at(-1).atMs, at - 2 * STEP);
|
||||
h.follower.stop();
|
||||
});
|
||||
|
||||
test('a step a move back asked for that fails clears nothing and is asked again while paused', async () => {
|
||||
const SPAN = 5 * STEP;
|
||||
const h = harness({ spanMs: SPAN });
|
||||
h.follower.start();
|
||||
const at = WALL - 600_000;
|
||||
h.clock.seek(at);
|
||||
await flush();
|
||||
await h.answer();
|
||||
h.clock.seek(at - 3 * STEP - 1_000);
|
||||
await flush();
|
||||
const asked = loads(h.calls).length;
|
||||
await h.answer(0, false);
|
||||
assert.equal(
|
||||
h.calls.some((call) => call.type === 'clear'),
|
||||
false,
|
||||
);
|
||||
h.advance(SNAPSHOT_RETRY_MS);
|
||||
await flush();
|
||||
assert.equal(loads(h.calls).length, asked + 1);
|
||||
assert.deepEqual(loads(h.calls).at(-1), {
|
||||
type: 'load',
|
||||
atMs: at - 4 * STEP,
|
||||
replace: true,
|
||||
});
|
||||
h.follower.stop();
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* The fixes a snapshot at a time may carry for each contact (a record's
|
||||
* `fixes`, see readSnapshotTimeline in src/sources/live/contract.js): where
|
||||
* it was in the minutes around the time, so a layer following the world
|
||||
* clock places it at nearby times, earlier ones too, without asking again.
|
||||
*/
|
||||
|
||||
/** Most fixes a contact keeps across the snapshots that brought them. */
|
||||
export const TIMELINE_FIXES_LIMIT = 240;
|
||||
/** Kept fixes are at least this far apart, so the limit spans well over an
|
||||
* hour around the newest snapshot's time however dense the fixes come. */
|
||||
export const TIMELINE_FIX_SPACING_MS = 20_000;
|
||||
/**
|
||||
* The longest stretch of snapshot times a layer treats as loaded at once
|
||||
* (see src/time/snapshotTimeline.js). With each snapshot's fixes reaching a
|
||||
* span either side, it stays within what every contact keeps around the
|
||||
* newest snapshot's time.
|
||||
*/
|
||||
export const TIMELINE_HELD_MS = 20 * 60_000;
|
||||
/**
|
||||
* A contact following the clock is not drawn at a time more than this
|
||||
* before its first fix: it was not there yet. Within it, it is drawn
|
||||
* reaching its first fix, as a contact first seen is.
|
||||
*/
|
||||
export const TIMELINE_UNSEEN_MS = 60_000;
|
||||
|
||||
/**
|
||||
* A record's fixes in time order, those with a time and a position only,
|
||||
* or null when it carries fewer than two.
|
||||
* @param {unknown} value
|
||||
* @returns {Array<{atMs: number, lat: number, lon: number,
|
||||
* altitudeM: number|null}>|null}
|
||||
*/
|
||||
export function readTimelineFixes(value) {
|
||||
if (!Array.isArray(value)) return null;
|
||||
const fixes = [];
|
||||
for (const fix of value) {
|
||||
const atMs = Number(fix?.atMs);
|
||||
const lat = Number(fix?.lat);
|
||||
const lon = Number(fix?.lon);
|
||||
if (
|
||||
!Number.isFinite(atMs) ||
|
||||
!Number.isFinite(lat) ||
|
||||
!Number.isFinite(lon) ||
|
||||
Math.abs(lat) > 90
|
||||
)
|
||||
continue;
|
||||
const altitudeM = Number(fix?.altitudeM);
|
||||
fixes.push({
|
||||
atMs,
|
||||
lat,
|
||||
lon,
|
||||
altitudeM:
|
||||
fix?.altitudeM != null && Number.isFinite(altitudeM) ? altitudeM : null,
|
||||
});
|
||||
}
|
||||
if (fixes.length < 2) return null;
|
||||
fixes.sort((a, b) => a.atMs - b.atMs);
|
||||
return fixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Two runs of entries in time order (`timeOf` reads an entry's epoch ms) as
|
||||
* one: `newer` stands in for the entries of `older` within the time it
|
||||
* spans, the rest of `older` is kept, and an entry closer than
|
||||
* TIMELINE_FIX_SPACING_MS to the one kept before it is left out, but for
|
||||
* the newest of `newer`, which is always kept. Only the `limit` entries nearest
|
||||
* `centreMs` are kept, so the fixes kept span at least `limit` times that
|
||||
* spacing.
|
||||
* @template T
|
||||
* @param {T[]} older
|
||||
* @param {T[]} newer
|
||||
* @param {(entry: T) => number} timeOf
|
||||
* @param {number} centreMs
|
||||
* @param {number} [limit]
|
||||
* @returns {T[]}
|
||||
*/
|
||||
export function mergeTimelineRuns(
|
||||
older,
|
||||
newer,
|
||||
timeOf,
|
||||
centreMs,
|
||||
limit = TIMELINE_FIXES_LIMIT,
|
||||
) {
|
||||
const first = newer.length ? timeOf(newer[0]) : Infinity;
|
||||
const last = newer.length ? timeOf(newer[newer.length - 1]) : -Infinity;
|
||||
const newest = newer[newer.length - 1];
|
||||
// Both runs in order, the older entries within the newer run's time left
|
||||
// out, thinned to the spacing.
|
||||
const merged = [];
|
||||
const take = (entry) => {
|
||||
const k = merged.length - 1;
|
||||
if (k < 0 || timeOf(entry) - timeOf(merged[k]) >= TIMELINE_FIX_SPACING_MS) {
|
||||
merged.push(entry);
|
||||
} else if (entry === newest) {
|
||||
merged[k] = entry;
|
||||
}
|
||||
};
|
||||
let i = 0;
|
||||
let j = 0;
|
||||
while (i < older.length || j < newer.length) {
|
||||
if (i < older.length) {
|
||||
const t = timeOf(older[i]);
|
||||
if (t >= first && t <= last) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (j >= newer.length || t < timeOf(newer[j])) {
|
||||
take(older[i++]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
take(newer[j++]);
|
||||
}
|
||||
if (merged.length <= limit) return merged;
|
||||
// The window of `limit` entries nearest the centre.
|
||||
let start = 0;
|
||||
while (
|
||||
start + limit < merged.length &&
|
||||
Math.abs(timeOf(merged[start + limit]) - centreMs) <
|
||||
Math.abs(timeOf(merged[start]) - centreMs)
|
||||
)
|
||||
start++;
|
||||
return merged.slice(start, start + limit);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mergeTimelineRuns, readTimelineFixes } from './timelineFixes.js';
|
||||
|
||||
test('fixes are read in time order, the malformed left out, and fewer than two are none', () => {
|
||||
assert.equal(readTimelineFixes(undefined), null);
|
||||
assert.equal(readTimelineFixes([{ atMs: 1, lat: 1, lon: 1 }]), null);
|
||||
assert.deepEqual(
|
||||
readTimelineFixes([
|
||||
{ atMs: 3_000, lat: 3, lon: 3, altitudeM: 30 },
|
||||
{ atMs: 'x', lat: 0, lon: 0 },
|
||||
{ atMs: 2_000, lat: 95, lon: 2 },
|
||||
{ atMs: 1_000, lat: 1, lon: 1, altitudeM: null },
|
||||
]),
|
||||
[
|
||||
{ atMs: 1_000, lat: 1, lon: 1, altitudeM: null },
|
||||
{ atMs: 3_000, lat: 3, lon: 3, altitudeM: 30 },
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('two runs merge in order, the newer standing in for the older within its time, thinned to the spacing', () => {
|
||||
const at = (atMs, from) => ({ atMs, from });
|
||||
const merged = mergeTimelineRuns(
|
||||
[at(0, 'old'), at(30_000, 'old'), at(105_000, 'old'), at(160_000, 'old')],
|
||||
[
|
||||
at(40_000, 'new'),
|
||||
at(80_000, 'new'),
|
||||
at(90_000, 'new'),
|
||||
at(150_000, 'new'),
|
||||
],
|
||||
(fix) => fix.atMs,
|
||||
60_000,
|
||||
);
|
||||
// 40 s is too near 30 s; 90 s too near 80 s; 105 s is within the newer
|
||||
// run; 160 s too near 150 s, the newest of the newer run, kept.
|
||||
assert.deepEqual(merged, [
|
||||
at(0, 'old'),
|
||||
at(30_000, 'old'),
|
||||
at(80_000, 'new'),
|
||||
at(150_000, 'new'),
|
||||
]);
|
||||
const ends = mergeTimelineRuns(
|
||||
[at(0, 'old')],
|
||||
[at(5_000, 'new'), at(10_000, 'new')],
|
||||
(fix) => fix.atMs,
|
||||
0,
|
||||
);
|
||||
assert.deepEqual(ends, [at(10_000, 'new')]);
|
||||
});
|
||||
|
||||
test("runs a step apart, however dense, keep a contact's fixes back over every step", () => {
|
||||
const spanMs = 5 * 60_000;
|
||||
for (const [stepMs, spacingMs] of [
|
||||
[10_000, 5_000],
|
||||
[60_000, 5_000],
|
||||
[60_000, 30_000],
|
||||
]) {
|
||||
let fixes = [];
|
||||
for (let t = 0; t <= 20 * 60_000; t += stepMs) {
|
||||
const run = [];
|
||||
for (
|
||||
let f = Math.ceil((t - spanMs) / spacingMs) * spacingMs;
|
||||
f <= t + spanMs;
|
||||
f += spacingMs
|
||||
)
|
||||
run.push({ atMs: f });
|
||||
fixes = mergeTimelineRuns(fixes, run, (fix) => fix.atMs, t);
|
||||
}
|
||||
assert.equal(fixes[0].atMs, -spanMs, `${stepMs}/${spacingMs}`);
|
||||
assert.equal(fixes.at(-1).atMs, 25 * 60_000);
|
||||
for (let k = 1; k < fixes.length - 1; k++)
|
||||
assert.ok(fixes[k].atMs - fixes[k - 1].atMs >= 20_000);
|
||||
}
|
||||
});
|
||||
|
||||
test('past the limit only the fixes nearest the centre are kept', () => {
|
||||
const run = (from, to) =>
|
||||
Array.from({ length: to - from }, (_, k) => ({
|
||||
atMs: (from + k) * 60_000,
|
||||
}));
|
||||
const kept = mergeTimelineRuns(
|
||||
run(0, 10),
|
||||
run(10, 20),
|
||||
(f) => f.atMs,
|
||||
15 * 60_000,
|
||||
6,
|
||||
);
|
||||
assert.deepEqual(
|
||||
kept.map((f) => f.atMs / 60_000),
|
||||
[12, 13, 14, 15, 16, 17],
|
||||
);
|
||||
const early = mergeTimelineRuns(run(0, 10), run(10, 20), (f) => f.atMs, 0, 4);
|
||||
assert.deepEqual(
|
||||
early.map((f) => f.atMs / 60_000),
|
||||
[0, 1, 2, 3],
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user