Files
RustProxy/src/admin/web/app/chart-scales.js
T
2026-10-15 16:15:49 +02:00

25 lines
1.1 KiB
JavaScript

// Stable plot ranges: percentages are absolute 0-100; unbounded metrics have
// unit-appropriate zero-based floors, expanding only for real higher readings.
window.rustProxyChartScale = (() => {
const floors = { rate: 100, count: 10, ms: 100, bytes: 64 * 1024 * 1024, "bytes/s": 64 * 1024 };
const ceilings = new Map();
function peak(key, unit, series) {
if (unit === "percent") return 100;
const floor = floors[unit] ?? 1;
let maximum = floor;
for (const values of series) for (const value of values) {
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
maximum = Math.max(maximum, value);
}
const magnitude = 10 ** Math.floor(Math.log10(maximum));
const tick = [1, 2, 5, 10].find(n => n * magnitude >= maximum) ?? 10;
const result = Math.max(ceilings.get(key) ?? 0, tick * magnitude);
ceilings.set(key, result);
return result;
}
function reset(prefix) {
for (const key of ceilings.keys()) if (key.startsWith(prefix)) ceilings.delete(key);
}
return { peak, reset };
})();