perf(firms): serve a cached body and gzip instead of serialising per request

/api/firms ran JSON.stringify over the whole fire set for every request and
sent it uncompressed: about 20 MB per 100,000 detections.

The handler now keeps the last body as bytes and reuses it for the same
cache entry and stale flag while the clock stays inside the span over which
the trailing-24 h filter keeps the same detections, so the JSON a request
gets is byte-identical to a fresh serialisation. A client that accepts gzip
gets the body gzipped, compressed once per kept body.

The span ends when the oldest kept acquisition minute turns 24 h old, so
requests minutes apart are still serialised afresh.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Mazeyar Moeini FeizabadiandClaude Fable 5.2 committed 2026-11-13 14:43:14 +01:00
1 parent 188f0ccd9b
commit e79d972389
5 files changed
+216 -13

No files matched your search

+27 -2
View File
@@ -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. */