diff --git a/client/src/Pages/Infrastructure/Details/Components/Gauges.tsx b/client/src/Pages/Infrastructure/Details/Components/Gauges.tsx index 7e508a545..dded8ecf5 100644 --- a/client/src/Pages/Infrastructure/Details/Components/Gauges.tsx +++ b/client/src/Pages/Infrastructure/Details/Components/Gauges.tsx @@ -1,8 +1,9 @@ import Stack from "@mui/material/Stack"; import { DetailGauge } from "@/Components/v2/design-elements"; +import prettyBytes from "pretty-bytes"; import { useTranslation } from "react-i18next"; -import { getGbs, getFrequency } from "@/Utils/InfraUtils"; +import { getFrequency } from "@/Utils/InfraUtils"; import { useTheme } from "@mui/material"; import useMediaQuery from "@mui/material/useMediaQuery"; import type { CheckSnapshot } from "@/Types/Check"; @@ -30,9 +31,9 @@ export const InfraDetailsGauges = ({ title={t("pages.infrastructure.gauges.memory.title")} progress={(snapshot?.memory?.usage_percent || 0) * 100} upperLabel={t("pages.infrastructure.gauges.memory.upperLabel")} - upperValue={getGbs(snapshot?.memory?.used_bytes || 0)} + upperValue={prettyBytes(snapshot?.memory?.used_bytes || 0)} lowerLabel={t("pages.infrastructure.gauges.memory.lowerLabel")} - lowerValue={getGbs(snapshot?.memory?.total_bytes || 0)} + lowerValue={prettyBytes(snapshot?.memory?.total_bytes || 0)} /> ); })} diff --git a/client/src/Pages/Infrastructure/Details/Components/StatusBoxes.tsx b/client/src/Pages/Infrastructure/Details/Components/StatusBoxes.tsx index 7b1d80c40..dffa6a175 100644 --- a/client/src/Pages/Infrastructure/Details/Components/StatusBoxes.tsx +++ b/client/src/Pages/Infrastructure/Details/Components/StatusBoxes.tsx @@ -1,17 +1,11 @@ import Stack from "@mui/material/Stack"; import { StatBox } from "@/Components/v2/design-elements"; +import prettyBytes from "pretty-bytes"; import { useTheme } from "@mui/material"; import { useTranslation } from "react-i18next"; import type { Monitor } from "@/Types/Monitor"; -import { - getAvgTemp, - getCores, - getFrequency, - getGbs, - getDiskTotalGbs, - getOsAndPlatform, -} from "@/Utils/InfraUtils"; +import { getAvgTemp, getCores, getFrequency, getOsAndPlatform } from "@/Utils/InfraUtils"; export const StatusBoxes = ({ monitor }: { monitor: Monitor }) => { const { t } = useTranslation(); @@ -24,8 +18,9 @@ export const StatusBoxes = ({ monitor }: { monitor: Monitor }) => { const cpuFrequency = getFrequency(latestCheck?.cpu?.frequency); const cpuTemps = latestCheck?.cpu?.temperature ?? []; const cpuTemperature = getAvgTemp(cpuTemps); - const memoryTotalBytes = getGbs(latestCheck?.memory?.total_bytes); - const diskTotalBytes = getDiskTotalGbs(latestCheck?.disk); + const memoryTotalBytes = latestCheck?.memory?.total_bytes ?? 0; + const diskTotalBytes = + latestCheck?.disk?.reduce((acc, disk) => acc + (disk.total_bytes || 0), 0) || 0; const os = getOsAndPlatform(latestCheck?.host); const platform = latestCheck?.host?.platform ?? undefined; @@ -58,11 +53,11 @@ export const StatusBoxes = ({ monitor }: { monitor: Monitor }) => { /> { - if (!value || !total) return 0; - return (value / total) * 100; -}; - -const formatPercentage = new Intl.NumberFormat("en-US", { - style: "percent", - minimumFractionDigits: 1, - maximumFractionDigits: 1, -}); - export const StatGauges = ({ diagnostics }: StatGaugesProps) => { const theme = useTheme(); const { t } = useTranslation(); @@ -51,7 +41,7 @@ export const StatGauges = ({ diagnostics }: StatGaugesProps) => { @@ -59,7 +49,7 @@ export const StatGauges = ({ diagnostics }: StatGaugesProps) => { title={t("pages.logs.diagnostics.gauges.heapUsage")} progress={heapUsedSize} upperLabel={t("pages.logs.diagnostics.gauges.availableMemoryPercentage")} - upperValue={formatPercentage.format(heapUsedSize / 100)} + upperValue={formatPercentageFromWhole(heapUsedSize)} lowerLabel={t("pages.logs.diagnostics.gauges.used")} lowerValue={prettyBytes(diagnostics.v8HeapStats?.usedHeapSizeBytes ?? 0)} /> @@ -67,7 +57,7 @@ export const StatGauges = ({ diagnostics }: StatGaugesProps) => { title={t("pages.logs.diagnostics.gauges.heapUtilization")} progress={actualHeapUsed} upperLabel={t("pages.logs.diagnostics.gauges.allocatedPercentage")} - upperValue={formatPercentage.format(actualHeapUsed / 100)} + upperValue={formatPercentageFromWhole(actualHeapUsed)} lowerLabel={t("pages.logs.diagnostics.gauges.total")} lowerValue={prettyBytes(diagnostics.v8HeapStats?.usedHeapSizeBytes ?? 0)} /> @@ -75,9 +65,7 @@ export const StatGauges = ({ diagnostics }: StatGaugesProps) => { title={t("pages.logs.diagnostics.gauges.instantCpuUsage")} progress={diagnostics.cpuUsage?.usagePercentage ?? 0} upperLabel={t("pages.logs.diagnostics.gauges.usedSPercentage")} - upperValue={formatPercentage.format( - (diagnostics.cpuUsage?.usagePercentage ?? 0) / 100 - )} + upperValue={formatPercentageFromWhole(diagnostics.cpuUsage?.usagePercentage ?? 0)} /> ); diff --git a/client/src/Utils/FormatUtils.ts b/client/src/Utils/FormatUtils.ts new file mode 100644 index 000000000..35182b797 --- /dev/null +++ b/client/src/Utils/FormatUtils.ts @@ -0,0 +1,43 @@ +/** + * Intl.NumberFormat instance for percentage formatting. + * Reused across all formatting calls for performance. + */ +const percentageFormatter = new Intl.NumberFormat("en-US", { + style: "percent", + minimumFractionDigits: 1, + maximumFractionDigits: 1, +}); + +/** + * Formats a decimal value as a percentage string. + * @param value - Decimal value (e.g., 0.75 for 75%) + * @returns Formatted percentage string (e.g., "75.0%") + * @example + * formatPercentage(0.75) // "75.0%" + * formatPercentage(1) // "100.0%" + * formatPercentage(0.5432) // "54.3%" + */ +export const formatPercentage = (value: number): string => { + if (typeof value !== "number" || Number.isNaN(value)) { + return "0.0%"; + } + return percentageFormatter.format(value); +}; + +/** + * Formats a whole number percentage value as a percentage string. + * @param value - Whole number percentage (e.g., 75 for 75%) + * @returns Formatted percentage string (e.g., "75.0%") + * @example + * formatPercentageFromWhole(75) // "75.0%" + * formatPercentageFromWhole(100) // "100.0%" + * formatPercentageFromWhole(54.32) // "54.3%" + */ +export const formatPercentageFromWhole = (value: number): string => { + return formatPercentage(value / 100); +}; + +export const getPercentage = (value: number, total: number) => { + if (!value || !total) return 0; + return (value / total) * 100; +}; diff --git a/client/src/Utils/InfraUtils.ts b/client/src/Utils/InfraUtils.ts index 07212737e..5fcdd8dea 100644 --- a/client/src/Utils/InfraUtils.ts +++ b/client/src/Utils/InfraUtils.ts @@ -1,4 +1,4 @@ -import type { CheckDiskInfo, CheckHostInfo } from "@/Types/Check"; +import type { CheckHostInfo } from "@/Types/Check"; export const getFrequency = (frequency: number | undefined): string => { if (!frequency) return "N/A"; @@ -18,32 +18,6 @@ export const getAvgTemp = (temps: number[]): string => { return `${avgTemp?.toFixed(2)} °C`; }; -export const getGbs = (bytes: number | undefined): string => { - if (!bytes) { - return "N/A"; - } - if (bytes === 0) { - return "0 GB"; - } - - const GB = bytes / (1024 * 1024 * 1024); - const MB = bytes / (1024 * 1024); - - if (GB >= 1) { - return GB.toFixed(2) + " GB"; - } else { - return MB.toFixed(2) + " MB"; - } -}; - -export const getDiskTotalGbs = (disk?: Partial[]): string => { - if (!disk) { - return getGbs(0); - } - const totalBytes = disk?.reduce((acc, disk) => acc + (disk.total_bytes || 0), 0) || 0; - return getGbs(totalBytes); -}; - export const getOsAndPlatform = (hostInfo: CheckHostInfo | undefined): string => { if (!hostInfo) { return "N/A";