fix: aggregate presence samples every five minutes with seven-day retention
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import type { AnalyticsStore } from '../src/server/analytics-store.js';
|
||||
import { Analytics } from '../src/server/analytics.js';
|
||||
import { ManualClock } from './helpers.js';
|
||||
|
||||
test('ten-second observations preserve a brief peak in one five-minute write and flush the final partial interval', async () => {
|
||||
const clock = new ManualClock();
|
||||
const writes: Parameters<AnalyticsStore['sample']>[] = [];
|
||||
const store = {
|
||||
sample: async (...args: Parameters<AnalyticsStore['sample']>) => { writes.push(args); },
|
||||
cleanup: async () => {},
|
||||
} as unknown as AnalyticsStore;
|
||||
const analytics = new Analytics(store, clock);
|
||||
try {
|
||||
analytics.states.set('visitor', { visible: true, lastAt: clock.now(), metadata: { device: 'pc', browser: 'Other', source: 'direct_or_unknown', invitation: false } });
|
||||
await clock.advance(290000);
|
||||
assert.equal(writes.length, 0);
|
||||
assert.equal(analytics.live().active, 0);
|
||||
await clock.advance(10000);
|
||||
assert.equal(writes.length, 1);
|
||||
assert.equal(writes[0][1].active, 1);
|
||||
assert.equal(writes[0][4], 30);
|
||||
await clock.advance(300000);
|
||||
assert.equal(writes.length, 2);
|
||||
assert.equal(writes[1][1].active, 0);
|
||||
assert.equal(writes[1][4], 30);
|
||||
await clock.advance(20000);
|
||||
assert.equal(writes.length, 2);
|
||||
await analytics.close();
|
||||
assert.equal(writes.length, 3);
|
||||
assert.equal(writes[2][4], 2);
|
||||
await analytics.close();
|
||||
assert.equal(writes.length, 3);
|
||||
} finally { await analytics.close(); }
|
||||
});
|
||||
Reference in New Issue
Block a user