feat(statistics): keep large totals precise in gigabytes

This commit is contained in:
Sucukdeluxe
2026-08-21 10:34:13 +02:00
parent dc3d9dfc10
commit 5aa0359a93
2 changed files with 19 additions and 4 deletions
@@ -32,7 +32,7 @@ const rangeItems: Array<{ id: StatisticsRange; label: string }> = [
const numberFormatter = new Intl.NumberFormat("de-DE", { maximumFractionDigits: 1 });
function formatBytes(bytes: number): string {
function formatBytes(bytes: number, maximumUnitIndex = Number.POSITIVE_INFINITY): string {
const safe = Math.max(0, Number.isFinite(bytes) ? bytes : 0);
if (safe < 1024) {
return `${Math.round(safe)} B`;
@@ -40,7 +40,7 @@ function formatBytes(bytes: number): string {
const units = ["KB", "MB", "GB", "TB", "PB"];
let value = safe / 1024;
let unitIndex = 0;
while (value >= 1024 && unitIndex < units.length - 1) {
while (value >= 1024 && unitIndex < units.length - 1 && unitIndex < maximumUnitIndex) {
value /= 1024;
unitIndex += 1;
}
@@ -51,8 +51,8 @@ function formatMetric(metric: StatisticsMetric, kind: "bytes" | "count" | "perce
if (!metric.available || metric.value === null) {
return "";
}
if (kind === "bytes") {
return formatBytes(metric.value);
if (kind === "bytes") {
return formatBytes(metric.value, 2);
}
if (kind === "speed") {
return `${formatBytes(metric.value)}/s`;