File size: 6,300 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// history store: file layout, cut-off record, buckets left open by the last run, trimming. node t_history.js <project root> <scratch dir>
const fs = require('fs'), path = require('path'), { check, done } = require('./lib');
const [root, scratch] = process.argv.slice(2);
let fake = Math.floor(new Date(2026, 5, 10, 10, 20, 0).getTime() / 1000); // local 10:20
const realNow = Date.now; Date.now = () => fake * 1000;
const { HistoryStore, I, stats } = require(path.join(root, 'core/history.js'));
const dir = n => { const d = path.join(scratch, n); fs.rmSync(d, { recursive: true, force: true }); return d; };
const vals = x => Array.from({ length: 15 }, (_, i) => i === 4 ? NaN : x + i);
const read = f => { // independent reader of the documented format
    const b = fs.readFileSync(f), out = [];
    if (b.subarray(0, 4).toString() !== 'SMH1' || b[4] !== 15 || b[5] !== 0 || b.readUInt16LE(6) !== 136) return null;
    for (let o = 8; o + 136 <= b.length; o += 136) out.push({ ts: Number(b.readBigInt64LE(o)), n: b.readUInt32LE(o + 8), nUp: b.readUInt32LE(o + 12), avg0: b.readFloatLE(o + 16), avg4: b.readFloatLE(o + 32), max0: b.readFloatLE(o + 76) });
    return Object.assign(out, { rest: (b.length - 8) % 136 });
};

// 1. layout + accumulation
let d = dir('h1'), st = new HistoryStore(d), t0 = fake;
for (let i = 0; i < 120; ++i) fake = t0 + i * 10, st.add(fake, vals(i % 6 * 10), i !== 7); // 20 min, one sample down
st.add(fake, vals(999), true); // same second again: not a second raw point
let raw = read(path.join(d, 'raw.bin')), min = read(path.join(d, 'minute.bin'));
check('raw.bin: SMH1 header, 136-byte records, one per sample', raw && raw.length === 120 && raw.rest === 0 && raw[0].ts === t0 && raw[0].n === 1 && raw[0].nUp === 1 && isNaN(raw[0].avg4), raw && `${raw.length} records`);
check('a downtime sample is n=1 nUp=0 with NaN values', raw[7].n === 1 && raw[7].nUp === 0 && isNaN(raw[7].avg0));
check('minute.bin: closed minutes only, avg and max per bucket', min.length === 19 && min[0].ts === t0 && min[0].n === 6 && min[0].avg0 === 25 && min[0].max0 === 50 && min[1].n === 6 && min[1].nUp === 5, JSON.stringify(min[0]));
const open = st.range(t0, fake, 1).at(-1);
check('range() includes the open bucket', st.range(t0, fake, 1).length === 20 && open.ts === t0 + 19 * 60 && open.n === 7, `n=${open.n}`);
check('no hour / day file content before a bucket closes', !fs.existsSync(path.join(d, 'hour.bin')) && st.range(t0 - 3600, fake, 2).length === 1 && st.range(t0 - 86400, fake, 3)[0].n === 121);

// 2. restart later: the buckets the last run left open must not be lost (they are stored with the first new sample).
// The raw tier keeps one sample per second, so the rebuilt minute has 6 of the 7 samples.
fake = t0 + 2 * 3600 + 300, st = new HistoryStore(d);
check('restart 2 h later: the open buckets of the last run are still in range()', st.range(t0 - 3600, fake, 1).length === 20 && st.range(t0 - 3600, fake, 2)[0]?.n === 120 && st.range(t0 - 86400, fake, 3)[0]?.n === 120);
st.add(fake, vals(1), true);
check('restart 2 h later: last open minute is stored', st.t[1].length === 20 && st.t[1].at(-1).n === 6 && read(path.join(d, 'minute.bin')).length === 20, `${st.t[1].length} minutes`);
check('restart 2 h later: the open hour of the last run is stored', st.t[2].length === 1 && st.t[2][0].n === 120 && st.t[2][0].nUp === 119 && st.t[2][0].ts === t0 - 20 * 60 && read(path.join(d, 'hour.bin'))?.length === 1, `${st.t[2].length} hours`);
fake = Math.floor(new Date(2026, 5, 11, 9, 5, 0).getTime() / 1000), st = new HistoryStore(d), st.add(fake, vals(1), true);
check('restart next day: yesterday is stored in the day tier', st.t[3].length === 1 && st.t[3][0].n === 120 && st.t[3][0].ts === Math.floor(new Date(2026, 5, 10).getTime() / 1000), `${st.t[3].length} days, n=${st.t[3][0]?.n}`);
st = new HistoryStore(d);
check('another restart does not duplicate buckets', st.t[1].length === 20 && st.t[2].length === 1 && st.t[3].length === 1 && st.range(fake - 86400 * 2, fake, 3).length === 2 && read(path.join(d, 'day.bin')).length === 1, `${st.t[1].length} ${st.t[2].length} ${st.t[3].length}`);

// 3. a record cut off by a crash must not misalign what is appended later
d = dir('h2'), fake = t0, st = new HistoryStore(d);
for (let i = 0; i < 10; ++i) st.add(fake = t0 + i, vals(i), true);
fs.truncateSync(path.join(d, 'raw.bin'), 8 + 136 * 9 + 50);
st = new HistoryStore(d);
for (let i = 10; i < 13; ++i) st.add(fake = t0 + i, vals(i), true);
st = new HistoryStore(d), raw = read(path.join(d, 'raw.bin'));
check('cut-off record: file realigned, later samples readable', raw.rest === 0 && st.t[0].length === 12 && st.t[0].at(-1).ts === t0 + 12 && st.t[0].at(-1).avg[0] === 12, `${st.t[0].length} points, last ts ${st.t[0].at(-1)?.ts - t0}, file remainder ${raw.rest}`);

// 4. foreign file is set aside, expired points are trimmed
fs.writeFileSync(path.join(d, 'hour.bin'), 'not a history file'), fs.writeFileSync(path.join(d, 'hour.bin.old'), 'older');
st = new HistoryStore(d);
check('file with a wrong header is renamed to .old (replacing an older one)', fs.readFileSync(path.join(d, 'hour.bin.old'), 'utf8') === 'not a history file' && !fs.existsSync(path.join(d, 'hour.bin')));
d = dir('h3'), fake = t0, st = new HistoryStore(d);
for (let i = 0; i < 1100; ++i) st.add(fake = t0 + i * 60, vals(1), true); // 18 h of raw samples, 6 h are kept
raw = read(path.join(d, 'raw.bin'));
check('raw tier trimmed to 6 h every 500 appends (memory and file)', st.t[0].length < 500 && st.t[0][0].ts >= t0 + 999 * 60 - 6 * 3600 && raw.length === st.t[0].length && raw.rest === 0, `${st.t[0].length} in memory, ${raw.length} in file`);
check('tierFor: 1 h raw, 24 h minute, 30 d hour, 2 y day', [3600, 86400, 30 * 86400, 730 * 86400].map(r => st.tierFor(fake - r, fake)).join() === '0,1,2,3');
st = new HistoryStore(d);
check('reload drops expired raw points', st.t[0].every(p => p.ts >= fake - 6 * 3600) && st.t[0].length === 361, st.t[0].length);
const s = stats([{ nUp: 1, avg: [1], max: [5] }, { nUp: 3, avg: [3], max: [4] }, { nUp: 0, avg: [9], max: [9] }, { nUp: 1, avg: [NaN], max: [NaN] }], 0);
check('stats: weighted average, min, p95, peak', s.avg === 2.5 && s.min === 1 && s.p95 === 3 && s.max === 5 && s.count === 2, JSON.stringify(s));
Date.now = realNow, done();