fix(ops): retain byte units for sub-byte throughput

This commit is contained in:
chihao.ou
2026-09-30 02:42:28 +08:00
parent a60a29549f
commit 3a1a37b7b9
2 changed files with 14 additions and 1 deletions
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest'
import { formatBytes } from '../format'
import { formatByteRate } from '@/views/admin/ops/utils/opsFormatters'
describe('fractional byte rates', () => {
it.each([[0.5, '0.5 Bytes'], [0.01, '0.01 Bytes'], [0, '0 Bytes'], [1024, '1 KB']])('formats %s bytes', (value, expected) => {
expect(formatBytes(value as number)).toBe(expected)
})
it('keeps the byte unit when an observation averages less than one byte per second', () => {
expect(formatByteRate(30, 1)).toBe('0.5 Bytes/s')
})
})
+1 -1
View File
@@ -87,7 +87,7 @@ export function formatBytes(bytes: number, decimals: number = 2): string {
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
const i = Math.max(0, Math.floor(Math.log(bytes) / Math.log(k)))
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}