Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/swarm-gauges.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions test/unraid-ui.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
12 changes: 1 addition & 11 deletions web/src/swarm/gauges.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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[] = []) {
Expand Down
14 changes: 4 additions & 10 deletions web/src/unraidView.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {bytes} from './formatBytes.ts'

export type UnraidItemStatus = 'waiting' | 'running' | 'done' | 'failed' | 'skipped'

export type UnraidItem = {
Expand Down Expand Up @@ -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 {
Expand Down
Loading