// Formatting and catalogues used by both the server (core/*.js) and the page (app.js). const SM = (() => { const fix = (v, prec = 0) => (+v).toFixed(prec); const bytes = (b, prec = 1) => { let i = 0; for (; Math.abs(b) >= 1024 && i < 5; ++i) b /= 1024; return fix(b, i ? prec : 0) + ' ' + ['B', 'KB', 'MB', 'GB', 'TB', 'PB'][i]; }; const Fmt = { fix, bytes, rate: b => bytes(b) + '/s', pct: (p, prec = 1) => fix(p, prec) + '%', temp: c => fix(c) + ' °C', num: v => String(+(+v).toPrecision(6)), iso: ms => new Date(ms - new Date(ms).getTimezoneOffset() * 60000).toISOString().slice(0, 19), // local time count(v) { const a = Math.abs(v); return a >= 1e9 ? fix(v / 1e9, 1) + 'G' : a >= 1e6 ? fix(v / 1e6, 1) + 'M' : a >= 1e4 ? fix(v / 1e3, 1) + 'K' : fix(v); }, duration(sec) { const s = Math.trunc(sec), d = Math.trunc(s / 86400), h = Math.trunc(s % 86400 / 3600), m = Math.trunc(s % 3600 / 60); return d ? `${d}d ${h}h ${m}m` : h ? `${h}h ${m}m` : m ? `${m}m ${s % 60}s` : `${s}s`; }, }; // history metrics, in the order of the binary record const H = ['cpu', 'iowait', 'load', 'mem', 'swap', 'disk', 'diskio', 'rx', 'tx', 'rd', 'wr', 'temp', 'procs', 'gpu', 'failed']; const hNames = ['CPU', 'I/O wait', 'Load (1m)', 'Memory', 'Swap', 'Disk usage', 'Disk busy', 'Net download', 'Net upload', 'Disk read', 'Disk write', 'Temperature', 'Processes', 'GPU', 'Failed services']; const fmtMetric = (m, v) => v == null || isNaN(v) ? '—' : m >= 7 && m <= 10 ? Fmt.rate(v) : m === 2 ? fix(v, 2) : m === 11 ? Fmt.temp(v) : m === 12 || m === 14 ? fix(v) : Fmt.pct(v); // rule catalogue rows: key, label, unit (metrics) or argument hint (actions, defined per platform) const ruleMetrics = [['cpu', 'CPU usage', '%'], ['iowait', 'CPU I/O wait', '%'], ['load', 'Load (1 min) per core', '%'], ['mem', 'Memory usage', '%'], ['swap', 'Swap usage', '%'], ['disk', 'Fullest disk usage', '%'], ['diskio', 'Busiest disk utilisation', '%'], ['temp', 'Hottest sensor', '°C'], ['rx', 'Network download', 'MB/s'], ['tx', 'Network upload', 'MB/s'], ['failed', 'Failed services', ''], ['procs', 'Process count', ''], ['gpu', 'GPU usage (max)', '%'], ['gputemp', 'GPU temperature (max)', '°C']]; const def = (list, key) => list.find(d => d[0] === key) || [key, key, '']; const conditionText = r => { const [, label, unit] = def(ruleMetrics, r.metric); return `${label} ${r.op} ${Fmt.num(r.value)}${unit && ' ' + unit}` + (r.duration ? ` for ${r.duration} s` : ''); }; const actionText = (actions, r) => def(actions, r.action)[1] + (r.arg ? ': ' + r.arg : ''); return { Fmt, H, hNames, fmtMetric, ruleMetrics, def, conditionText, actionText }; })(); if (typeof module === 'object') module.exports = SM;