From 7a72c8a28197d764a18c7e1ecb3d22c1bc217a2d Mon Sep 17 00:00:00 2001 From: Joseph Woolley Date: Sat, 5 Dec 2026 07:54:12 -0500 Subject: [PATCH] Use one size formatter for gauges and Unraid. Machine gauges and the Unraid import list kept their own copies, so a size just under a unit still read 1024 KB or 1000 KB. They now use the shared helper, which steps up after rounding. Fixes #141 Co-Authored-By: Grok 4.7 --- test/swarm-gauges.test.mjs | 6 ++++++ test/unraid-ui.test.mjs | 1 + web/src/swarm/gauges.ts | 12 +----------- web/src/unraidView.ts | 14 ++++---------- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/test/swarm-gauges.test.mjs b/test/swarm-gauges.test.mjs index a122c9c2..c1bb52b9 100644 --- a/test/swarm-gauges.test.mjs +++ b/test/swarm-gauges.test.mjs @@ -56,6 +56,12 @@ test('swarmGaugeItems: swap sits after memory only when the machine has swap spa assert.match(swap.sub, /of /) }) +test('swarmGaugeItems: a size that rounds onto the next unit is not left as 1024 KB', () => { + const items = swarmGaugeItems(metrics({mem: {total: 2 * 1024 ** 3, used: 1048575}, disks: [{mount: '/', total: 2_000_000, used: 999999}]})) + assert.equal(items.find((g) => g.k === 'Memory').sub, '1.0 MB of 2 GB') + assert.equal(items.find((g) => g.k === 'Storage').sub, '1.0 MB of 2 MB') +}) + test('swarmGaugeItems: one GPU is a GPU ring; two are GPU 0 and GPU 1', () => { const one = swarmGaugeItems(metrics({gpus: [{id: 'gpu-0', index: 0, name: 'NVIDIA GeForce RTX 3060', used: 32}]})) assert.equal(one.at(-1).k, 'GPU') diff --git a/test/unraid-ui.test.mjs b/test/unraid-ui.test.mjs index 11f1d639..75a58d1f 100644 --- a/test/unraid-ui.test.mjs +++ b/test/unraid-ui.test.mjs @@ -159,6 +159,7 @@ test('unknown container sizes format as size unknown and mark totals as a minimu assert.equal(formatBytes(null), 'size unknown') assert.equal(formatBytes(0), '0 B') assert.equal(formatBytes(1024), '1.0 KB') + assert.equal(formatBytes(1048575), '1.0 MB') assert.equal(formatTotalSize([null]), 'size unknown') assert.equal(formatTotalSize([0, null]), 'size unknown') assert.equal(formatTotalSize([2 * 1024 ** 3, null]), 'at least 2.0 GB') diff --git a/web/src/swarm/gauges.ts b/web/src/swarm/gauges.ts index 0d693983..514e60cd 100644 --- a/web/src/swarm/gauges.ts +++ b/web/src/swarm/gauges.ts @@ -1,4 +1,5 @@ // Glance rings for a swarm computer: same set as Home, from connector metrics. +import {bytes} from '../formatBytes.ts' export type SwarmGpu = { id: string @@ -36,17 +37,6 @@ export function batteryColor(p: number) { return p <= 15 ? 'var(--red)' : p <= 25 ? 'var(--yellow)' : 'var(--green)' } -function bytes(n: number | undefined, digits = 1, base: 1000 | 1024 = 1000) { - if (n === undefined || !Number.isFinite(n)) return '–' - const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] - let i = 0 - while (n >= base && i < units.length - 1) { - n /= base - i++ - } - return `${n.toFixed(n >= 100 || i === 0 ? 0 : digits)} ${units[i]}` -} - const pct = (used: number, total: number) => (total > 0 ? Math.min(100, Math.max(0, (100 * used) / total)) : 0) export function rootDisk(disks: SwarmDisk[] = []) { diff --git a/web/src/unraidView.ts b/web/src/unraidView.ts index c86ff562..0d87c783 100644 --- a/web/src/unraidView.ts +++ b/web/src/unraidView.ts @@ -1,3 +1,5 @@ +import {bytes} from './formatBytes.ts' + export type UnraidItemStatus = 'waiting' | 'running' | 'done' | 'failed' | 'skipped' export type UnraidItem = { @@ -72,16 +74,8 @@ export function defaultSelectedVms(vms: {name: string; state: string; problems: } export function formatBytes(n: number | null | undefined): string { - if (n == null) return 'size unknown' - if (n < 1024) return `${n} B` - const u = ['KB', 'MB', 'GB', 'TB'] - let i = -1 - let v = n - do { - v /= 1024 - i++ - } while (v >= 1024 && i < u.length - 1) - return `${v.toFixed(v < 10 ? 1 : 0)} ${u[i]}` + if (n == null || !Number.isFinite(n)) return 'size unknown' + return bytes(n, 1, 1024) } export function formatTotalSize(sizes: (number | null | undefined)[]): string {