// JS port of ref/analysis_test.cpp: synthetic 3-day history -> store reload + analyzer. const path = require('path'), fs = require('fs'); const root = process.argv[2], dir = process.argv[3]; const { HistoryStore, analyze, stats, TIERS, I, nowSec } = require(path.join(root, 'core/history.js')), { H } = require(path.join(root, 'public/shared.js')); const { blank } = require(path.join(root, 'core/metrics.js')); const model = require(path.join(root, 'core/model.js')); fs.rmSync(dir, { recursive: true, force: true }); const now = nowSec(), start = now - 3 * 86400; let st = new HistoryStore(dir); for (let t = start; t <= now; t += 30) { const v = new Array(H.length).fill(0), days = (t - start) / 86400, hr = Math.floor(t / 3600) % 24; v[I.cpu] = 20 + 10 * (hr > 8 && hr < 18); v[I.disk] = 70 + 5 * days; v[I.mem] = now - t < 6 * 3600 ? 50 + 2.0 * (6 - (now - t) / 3600.0) : 50; v[I.load] = 1, v[I.rx] = 1e6, v[I.tx] = 5e5; st.add(t, v, !(t > now - 86400 && t < now - 86400 + 1800)); } st = new HistoryStore(dir); console.log(`reloaded raw=${st.t[0].length} minute=${st.t[1].length} hour=${st.t[2].length} day=${st.t[3].length} bytes=${st.diskBytes()}`); const used = st.tierFor(now - 7 * 86400, now), r = st.range(now - 7 * 86400, now); console.log(`range 7d -> ${TIERS[used]}, ${r.length} pts`); const m = Object.assign(blank(0), { valid: true, cpuCount: 4, memPct: 62, maxDiskPct: 85, fullestMount: '/' }); const ap = { ...model.make({}, '').autopilot, diskCleanup: true, diskForecastDays: 5 }; const trend = o => ({ pid: 0, minutesTracked: 0, name: '', user: '', cpuNow: 0, highCpuMinutes: 0, rssNow: 0, rssGrowth: 0, rssGrowthPct: 0, rssMonotonic: 0, kernel: false, ...o }); const p = trend({ pid: 4242, name: 'leaky', minutesTracked: 45, rssNow: 900e6, rssGrowth: 400e6, rssGrowthPct: 80, rssMonotonic: 0.95 }); const q = trend({ pid: 777, name: 'spinner', cpuNow: 99, highCpuMinutes: 12 }); const svc = (unit, failedNow, preexisting, failsLastHour, autoRestartsLastHour) => ({ unit, failedNow, preexisting, failsLastHour, autoRestartsLastHour }); const a = analyze(st, m, ap, [p, q], [svc('web.service', true, false, 1, 0), svc('old.service', true, true, 0, 0)], true); const f = (v, n = 2) => v.toFixed(n); console.log(`health=${a.health} grade=${a.grade} diskFullDays=${f(a.diskFullDays)} slope/day=${f(a.diskSlopePerDay)} memSlope/h=${f(a.memSlopePerHour)} r2=${f(a.memTrendR2)} up24=${f(a.up[0])} up7=${f(a.up[1])}`); for (const x of a.findings) console.log(` [${x.severity}] ${x.title} | auto=${+x.autoEligible} action=${x.action} ${x.actionArg}`); const s = stats(r, I.disk); console.log(`disk stats min=${f(s.min, 1)} avg=${f(s.avg, 1)} p95=${f(s.p95, 1)} max=${f(s.max, 1)} n=${s.count}; capacity: ${a.capacity.join(' | ')}`);