Skip to content

Commit e893c61

Browse files
committed
fix: JS formatBytes unit index off-by-one for realtime updates
The JS formatBytes used floor(log1000(b)) directly as the units index, but this returns a value 1 too high. For 642MB (642000000 bytes): log1000 gives ~2.94 → floor 2 → units[2]='GB' (wrong, should be MB). Root cause: Go formatBytes counts how many times b/1000 is still ≥ 1000 (loop condition n >= unit), which produces exp where divisor=1000^(exp+1). But JS formula floor(log1000(b)) counts how many times b ≥ 1000^n without the extra division check, making it off by 1. Fix: separate exp (power of 1000) from i (units index), where i = exp-1 and divisor = 1000^exp. This mirrors the Go logic exactly.
1 parent 95fa0ae commit e893c61

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎internal/http/templates/live_charts.html‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
b = Number(b);
66
if (b < 1000) return Math.round(b) + ' B';
77
var units = ['kB','MB','GB','TB','PB'];
8-
var i = Math.floor(Math.log(b) / Math.log(1000));
8+
var exp = Math.floor(Math.log(b) / Math.log(1000));
9+
var i = exp - 1;
910
if (i >= units.length) i = units.length - 1;
10-
return (b / Math.pow(1000, i)).toFixed(1) + ' ' + units[i];
11+
return (b / Math.pow(1000, exp)).toFixed(1) + ' ' + units[i];
1112
}
1213

1314
// initLiveCharts wires up a live (1s) WebSocket chart + gauge feed for a server

0 commit comments

Comments
 (0)