mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-11-22 12:47:52 +00:00
## Problem
The node analytics activity timeline only shows the hours that hold
packets. `GetNodeAnalytics` sends sparse hourly buckets
(`cmd/server/store.go:10328`) and `buildActivityChart` used them as
category labels, so a silent stretch takes no room on the axis. An 11 h
outage of one repeater collapsed into two neighbouring bars, and two
repeaters that went down 12 h apart looked as if they dropped at the
same moment.
## Change
- `public/node-analytics.js`: `fillTimeline(tl, timeRange)` places every
hour from the start of the covered window (`timeRange.dataFrom` when
partial, else `timeRange.from`) to `timeRange.to` on the axis. Silent
hours get `{count: 0, certain: 0}`, so the certain/uncertain split from
#2260 stacks them as empty. Buckets the server sent are kept as they
are, also one outside the window. A missing or malformed `timeRange`
leaves the timeline unchanged.
- Above 8 days of covered window the hours are summed per UTC day (count
and certain), and the bar is labelled with its date plus "UTC". 337
hourly bars (14 days in memory) are about 1.5 px wide on a 700 px
canvas, so a quiet node would look empty. 7d stays hourly (169 bars).
- Buckets from older ingestors (`"2006-01-02 15:04:05"`, space instead
of T) are normalised so they land on their own hour.
- Exported as `NodeAnalyticsTimeline.fill` next to `series`.
Server stats are not touched: availability counts the hours with certain
packets (`store.go:10540`) and the longest silence is the gap between
them (`store.go:10549`), both correct on the sparse list. Sending zero
buckets from the server would break both.
## Scale
One bar per hour up to 8 days covered (25 for 24h, 169 for 7d), one per
day above it (31 for 30d, 366 for a full year). The fill is one pass
over the hours of the window: 8761 iterations for 365 days.
## Tests
- New `tests/unit/test-node-analytics-timeline-gaps.js` (13 cases, real
`node-analytics.js` in a vm), listed in `test-all.sh`: gaps filled with
zeros, window edges rounded to the hour, partial window starts at
`dataFrom`, empty node gives a full row of zeros, filled buckets go
through `series` as 0/0, bad `timeRange` returns the input,
out-of-window bucket kept, end on the hour included, 7d stays hourly, >8
days sums per day, 365 days gives 366 buckets, legacy space-format
bucket lands on its hour.
- Every suite in `test-all.sh` run one by one: all pass except
`test-manage-workflow.js` and `test-preflight-xss-gate.js`, which fail
the same way on this checkout without the change (Windows line endings /
console encoding).
## Browser check
Local server on the e2e fixture with one transmission moved 6 h back:
the 24h chart shows 10 hourly bars, five of them empty between the lone
early packet and the recent ones. Before the change the same data drew 4
adjacent bars. With the same transmission moved 10 days back, 30d shows
11 day bars labelled `2026-11-09 UTC` to `2026-11-19 UTC`, the lone
early packet visible at the left.
Not done: no E2E test added, no check on a mobile viewport.
127 lines
5.8 KiB
JavaScript
127 lines
5.8 KiB
JavaScript
/**
|
|
* Node analytics activity timeline: the server sends only the hours that
|
|
* hold packets, so the chart must place every hour of the covered window on
|
|
* the axis, silent hours included. Without that an 11 h outage collapsed into
|
|
* two neighbouring bars. Loads the real public/node-analytics.js in a vm
|
|
* sandbox.
|
|
*/
|
|
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
const assert = require('assert');
|
|
|
|
const REPO_ROOT = path.resolve(__dirname, '..', '..');
|
|
let passed = 0, failed = 0;
|
|
function t(name, fn) {
|
|
try { fn(); passed++; console.log(' ✓ ' + name); }
|
|
catch (e) { failed++; console.error(' ✗ ' + name + ': ' + e.message); }
|
|
}
|
|
|
|
const ctx = { window: {}, registerPage: () => {}, Date, Math, Number, String, isNaN, console };
|
|
vm.createContext(ctx);
|
|
vm.runInContext(fs.readFileSync(path.join(REPO_ROOT, 'public', 'node-analytics.js'), 'utf8'), ctx);
|
|
const fill = ctx.window.NodeAnalyticsTimeline && ctx.window.NodeAnalyticsTimeline.fill;
|
|
|
|
console.log('node-analytics.js activity timeline gaps');
|
|
|
|
t('exposes NodeAnalyticsTimeline.fill', () => {
|
|
assert.strictEqual(typeof fill, 'function');
|
|
});
|
|
|
|
t('silent hours between two active hours become zero buckets', () => {
|
|
const out = fill(
|
|
[{ bucket: '2026-11-17T18:00:00Z', count: 6, certain: 5 }, { bucket: '2026-11-17T21:00:00Z', count: 2, certain: 0 }],
|
|
{ from: '2026-11-17T18:00:00Z', to: '2026-11-17T21:30:00Z' });
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(out)), [
|
|
{ bucket: '2026-11-17T18:00:00Z', count: 6, certain: 5 },
|
|
{ bucket: '2026-11-17T19:00:00Z', count: 0, certain: 0 },
|
|
{ bucket: '2026-11-17T20:00:00Z', count: 0, certain: 0 },
|
|
{ bucket: '2026-11-17T21:00:00Z', count: 2, certain: 0 },
|
|
]);
|
|
});
|
|
|
|
t('runs from the hour of the window start to the hour of its end', () => {
|
|
const out = fill([{ bucket: '2026-11-18T02:00:00Z', count: 3 }],
|
|
{ from: '2026-11-18T00:40:00Z', to: '2026-11-18T03:05:00Z' });
|
|
assert.deepStrictEqual(Array.from(out, b => b.bucket), [
|
|
'2026-11-18T00:00:00Z', '2026-11-18T01:00:00Z', '2026-11-18T02:00:00Z', '2026-11-18T03:00:00Z']);
|
|
assert.deepStrictEqual(Array.from(out, b => b.count), [0, 0, 3, 0]);
|
|
});
|
|
|
|
t('a partial window starts at dataFrom, not at the requested range start', () => {
|
|
const out = fill([{ bucket: '2026-11-18T05:00:00Z', count: 1 }],
|
|
{ from: '2026-11-11T06:00:00Z', dataFrom: '2026-11-18T04:10:00Z', to: '2026-11-18T05:20:00Z', partial: true });
|
|
assert.deepStrictEqual(Array.from(out, b => b.bucket), ['2026-11-18T04:00:00Z', '2026-11-18T05:00:00Z']);
|
|
});
|
|
|
|
t('a node with no packets gets a full row of zeros', () => {
|
|
const out = fill([], { from: '2026-11-18T00:00:00Z', to: '2026-11-18T23:59:00Z' });
|
|
assert.strictEqual(out.length, 24);
|
|
assert.ok(out.every(b => b.count === 0));
|
|
});
|
|
|
|
t('filled buckets feed timelineSeries as zero certain and zero uncertain', () => {
|
|
const tl = fill([{ bucket: '2026-11-17T18:00:00Z', count: 3, certain: 1 }],
|
|
{ from: '2026-11-17T17:00:00Z', to: '2026-11-17T18:10:00Z' });
|
|
const s = ctx.window.NodeAnalyticsTimeline.series(tl);
|
|
assert.deepStrictEqual(Array.from(s.certain), [0, 1]);
|
|
assert.deepStrictEqual(Array.from(s.uncertain), [0, 2]);
|
|
});
|
|
|
|
t('missing or malformed time range: buckets unchanged', () => {
|
|
const tl = [{ bucket: '2026-11-17T18:00:00Z', count: 6 }];
|
|
assert.strictEqual(fill(tl, null), tl);
|
|
assert.strictEqual(fill(tl, { from: 'garbage', to: '2026-11-17T20:00:00Z' }), tl);
|
|
assert.strictEqual(fill(tl, { from: '2026-11-17T20:00:00Z', to: '2026-11-17T18:00:00Z' }), tl);
|
|
});
|
|
|
|
t('an end exactly on the hour includes that hour', () => {
|
|
const out = fill([], { from: '2026-11-17T17:00:00Z', to: '2026-11-17T19:00:00Z' });
|
|
assert.deepStrictEqual(Array.from(out, b => b.bucket), ['2026-11-17T17:00:00Z', '2026-11-17T18:00:00Z', '2026-11-17T19:00:00Z']);
|
|
});
|
|
|
|
t('7 days stay hourly', () => {
|
|
const out = fill([], { from: '2026-11-12T10:00:00Z', to: '2026-11-19T10:00:00Z' });
|
|
assert.strictEqual(out.length, 169);
|
|
assert.ok(out.every(b => !b.day));
|
|
});
|
|
|
|
t('more than 8 days covered: one bucket per UTC day, counts and certain summed', () => {
|
|
const out = fill([
|
|
{ bucket: '2026-11-18T01:00:00Z', count: 3, certain: 1 },
|
|
{ bucket: '2026-11-18T22:00:00Z', count: 2, certain: 2 },
|
|
{ bucket: '2026-11-19T09:00:00Z', count: 1 },
|
|
], { from: '2026-11-05T10:00:00Z', to: '2026-11-19T10:00:00Z' });
|
|
assert.strictEqual(out.length, 15);
|
|
assert.strictEqual(out[0].bucket, '2026-11-05T00:00:00Z');
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(out.slice(-2))), [
|
|
{ bucket: '2026-11-18T00:00:00Z', count: 5, certain: 3, day: true },
|
|
{ bucket: '2026-11-19T00:00:00Z', count: 1, certain: 1, day: true },
|
|
]);
|
|
assert.ok(out.every(b => b.day === true));
|
|
assert.ok(out.slice(0, -2).every(b => b.count === 0));
|
|
});
|
|
|
|
t('a 365-day window gives 366 day buckets', () => {
|
|
const out = fill([], { from: '2025-11-19T10:00:00Z', to: '2026-11-19T10:00:00Z' });
|
|
assert.strictEqual(out.length, 366);
|
|
});
|
|
|
|
t('a legacy space-format bucket lands on its hour, not beside it', () => {
|
|
const out = fill([{ bucket: '2026-11-17 18:00:00Z', count: 2 }],
|
|
{ from: '2026-11-17T17:00:00Z', to: '2026-11-17T19:00:00Z' });
|
|
assert.deepStrictEqual(Array.from(out, b => b.bucket), ['2026-11-17T17:00:00Z', '2026-11-17T18:00:00Z', '2026-11-17T19:00:00Z']);
|
|
assert.deepStrictEqual(Array.from(out, b => b.count), [0, 2, 0]);
|
|
});
|
|
|
|
t('a bucket outside the window is kept, not dropped', () => {
|
|
const out = fill([{ bucket: '2026-11-17T10:00:00Z', count: 4 }, { bucket: '2026-11-17T18:00:00Z', count: 1 }],
|
|
{ from: '2026-11-17T17:00:00Z', to: '2026-11-17T18:10:00Z' });
|
|
assert.deepStrictEqual(Array.from(out, b => b.bucket), ['2026-11-17T10:00:00Z', '2026-11-17T17:00:00Z', '2026-11-17T18:00:00Z']);
|
|
assert.deepStrictEqual(Array.from(out, b => b.count), [4, 0, 1]);
|
|
});
|
|
|
|
console.log(`\n${passed} passed, ${failed} failed`);
|
|
process.exit(failed ? 1 : 0);
|