Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
feat: Dynamic precision for byte formatting. Closes #1116
rcourtman227d791afa53
diff --git a/frontend-modern/src/utils/__tests__/format.test.ts b/frontend-modern/src/utils/__tests__/format.test.tsindex e222757776..f3ed602027 100644--- a/frontend-modern/src/utils/__tests__/format.test.ts+++ b/frontend-modern/src/utils/__tests__/format.test.ts@@ -14,47 +14,57 @@ import {} from '@/utils/format';describe('formatBytes', () => {- it('formats bytes correctly', () => {+ it('formats bytes with auto precision (small values get 2 decimals)', () => {expect(formatBytes(0)).toBe('0 B');- expect(formatBytes(512)).toBe('512.0 B');- expect(formatBytes(1024)).toBe('1.0 KB');- expect(formatBytes(1536)).toBe('1.5 KB');+ expect(formatBytes(512)).toBe('512 B'); // >= 100, 0 decimals+ expect(formatBytes(1024)).toBe('1.00 KB'); // < 10, 2 decimals+ expect(formatBytes(1536)).toBe('1.50 KB'); // < 10, 2 decimals});- it('formats kilobytes correctly', () => {- expect(formatBytes(1024 * 1024)).toBe('1.0 MB');- expect(formatBytes(1536 * 1024)).toBe('1.5 MB');+ it('formats kilobytes with auto precision', () => {+ expect(formatBytes(1024 * 1024)).toBe('1.00 MB'); // < 10, 2 decimals+ expect(formatBytes(1536 * 1024)).toBe('1.50 MB'); // < 10, 2 decimals+ expect(formatBytes(50 * 1024 * 1024)).toBe('50.0 MB'); // 10-100, 1 decimal+ expect(formatBytes(256 * 1024 * 1024)).toBe('256 MB'); // >= 100, 0 decimals});- it('formats megabytes correctly', () => {- expect(formatBytes(1024 * 1024 * 1024)).toBe('1.0 GB');- expect(formatBytes(1.5 * 1024 * 1024 * 1024)).toBe('1.5 GB');+ it('formats megabytes with auto precision', () => {+ expect(formatBytes(1024 * 1024 * 1024)).toBe('1.00 GB'); // < 10, 2 decimals+ expect(formatBytes(1.5 * 1024 * 1024 * 1024)).toBe('1.50 GB'); // < 10, 2 decimals+ expect(formatBytes(45 * 1024 * 1024 * 1024)).toBe('45.0 GB'); // 10-100, 1 decimal});- it('formats gigabytes correctly', () => {- expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe('1.0 TB');+ it('formats gigabytes with auto precision', () => {+ expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe('1.00 TB'); // < 10, 2 decimals});it('handles negative values', () => {expect(formatBytes(-1024)).toBe('0 B');});- it('handles custom decimal places', () => {+ it('handles explicit decimal places', () => {expect(formatBytes(1536 * 1024, 2)).toBe('1.50 MB');expect(formatBytes(1536 * 1024, 0)).toBe('2 MB');+ expect(formatBytes(1536 * 1024, 1)).toBe('1.5 MB');});});describe('formatSpeed', () => {- it('formats speed correctly', () => {+ it('formats speed with auto precision', () => {expect(formatSpeed(0)).toBe('0 B/s');- expect(formatSpeed(1024)).toBe('1 KB/s');- expect(formatSpeed(1024 * 1024)).toBe('1 MB/s');+ expect(formatSpeed(1024)).toBe('1.00 KB/s'); // < 10, 2 decimals+ expect(formatSpeed(1024 * 1024)).toBe('1.00 MB/s'); // < 10, 2 decimals+ expect(formatSpeed(50 * 1024 * 1024)).toBe('50.0 MB/s'); // 10-100, 1 decimal});it('handles negative values', () => {expect(formatSpeed(-1024)).toBe('0 B/s');});++ it('handles explicit decimal places', () => {+ expect(formatSpeed(1024, 0)).toBe('1 KB/s');+ expect(formatSpeed(1024 * 1024, 1)).toBe('1.0 MB/s');+ });});describe('formatPercent', () => {diff --git a/frontend-modern/src/utils/format.ts b/frontend-modern/src/utils/format.tsindex d8f9f6b679..d79edd8603 100644--- a/frontend-modern/src/utils/format.ts+++ b/frontend-modern/src/utils/format.ts@@ -1,16 +1,35 @@// Type-safe formatting utilities-export function formatBytes(bytes: number, decimals = 1): string {+/**+ * Format bytes to human-readable string with dynamic precision.+ * @param bytes - Number of bytes to format+ * @param decimals - Number of decimal places, or 'auto' for dynamic precision:+ * - Values < 10: 2 decimals (e.g., "5.94 GB")+ * - Values 10-100: 1 decimal (e.g., "45.2 GB")+ * - Values >= 100: 0 decimals (e.g., "256 GB")+ */+export function formatBytes(bytes: number, decimals: number | 'auto' = 'auto'): string {if (!bytes || bytes < 0) return '0 B';const k = 1024;const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];const i = Math.floor(Math.log(bytes) / Math.log(k));+ const value = bytes / Math.pow(k, i);++ // Determine precision+ let precision: number;+ if (decimals === 'auto') {+ if (value < 10) precision = 2;+ else if (value < 100) precision = 1;+ else precision = 0;+ } else {+ precision = decimals;+ }- return `${(bytes / Math.pow(k, i)).toFixed(decimals)} ${sizes[i]}`;+ return `${value.toFixed(precision)} ${sizes[i]}`;}-export function formatSpeed(bytesPerSecond: number, decimals = 0): string {+export function formatSpeed(bytesPerSecond: number, decimals: number | 'auto' = 'auto'): string {if (!bytesPerSecond || bytesPerSecond < 0) return '0 B/s';return `${formatBytes(bytesPerSecond, decimals)}/s`;}