From b9fe83e7a89bdfa06e40de8e1d32493c5c24775f Mon Sep 17 00:00:00 2001 From: MacRimi Date: Sun, 5 Oct 2025 20:12:33 +0200 Subject: [PATCH] Update virtual-machines.tsx --- AppImage/components/virtual-machines.tsx | 108 +++++++++++++++-------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/AppImage/components/virtual-machines.tsx b/AppImage/components/virtual-machines.tsx index 11cb10b..0a092e7 100644 --- a/AppImage/components/virtual-machines.tsx +++ b/AppImage/components/virtual-machines.tsx @@ -83,7 +83,7 @@ const fetcher = async (url: string) => { } const data = await response.json() - return Array.isArray(data) ? data : [] + return data } const formatBytes = (bytes: number | undefined): string => { @@ -286,34 +286,34 @@ export function VirtualMachines() { return (safeVMData.reduce((sum, vm) => sum + (vm.maxmem || 0), 0) / 1024 ** 3).toFixed(1) }, [safeVMData]) - const { data: systemData } = useSWR("/api/system", fetcher, { - refreshInterval: 30000, - revalidateOnFocus: false, + const { data: systemData } = useSWR<{ memory_total: number; memory_used: number; memory_usage: number }>( + "/api/system", + fetcher, + { + refreshInterval: 30000, + revalidateOnFocus: false, + }, + ) + + const physicalMemoryGB = systemData?.memory_total ?? null + const usedMemoryGB = systemData?.memory_used ?? null + const memoryUsagePercent = systemData?.memory_usage ?? null + const allocatedMemoryGB = Number.parseFloat(totalAllocatedMemoryGB) + const isMemoryOvercommit = physicalMemoryGB !== null && allocatedMemoryGB > physicalMemoryGB + + const getMemoryUsageColor = (percent: number | null) => { + if (percent === null) return "bg-blue-500" + if (percent >= 80) return "bg-red-500" + if (percent >= 60) return "bg-yellow-500" + return "bg-green-500" + } + + console.log("[v0] Memory status:", { + physical: physicalMemoryGB, + allocated: allocatedMemoryGB, + isOvercommit: isMemoryOvercommit, }) - const physicalMemoryGB = useMemo(() => { - if (systemData && systemData.memory_total) { - console.log("[v0] Physical memory GB:", systemData.memory_total) - return systemData.memory_total.toFixed(1) - } - console.log("[v0] No physical memory data available") - return null - }, [systemData]) - - const isMemoryOvercommit = useMemo(() => { - if (physicalMemoryGB) { - const overcommit = Number.parseFloat(totalAllocatedMemoryGB) > Number.parseFloat(physicalMemoryGB) - console.log("[v0] Memory overcommit check:", { - allocated: totalAllocatedMemoryGB, - physical: physicalMemoryGB, - isOvercommit: overcommit, - }) - return overcommit - } - console.log("[v0] Cannot check overcommit - no physical memory data") - return false - }, [totalAllocatedMemoryGB, physicalMemoryGB]) - if (isLoading) { return (
@@ -373,19 +373,49 @@ export function VirtualMachines() {
- -
- {totalAllocatedMemoryGB} GB -
- {isMemoryOvercommit && ( - - - Overcommit - + + {/* Memory Usage (current) */} + {physicalMemoryGB !== null && usedMemoryGB !== null && memoryUsagePercent !== null ? ( +
+
{usedMemoryGB.toFixed(1)} GB
+
+ {memoryUsagePercent.toFixed(1)}% of {physicalMemoryGB.toFixed(1)} GB +
+ div]:${getMemoryUsageColor(memoryUsagePercent)}`} + /> +
+ ) : ( +
+
--
+
Loading memory usage...
+
)} -

- {isMemoryOvercommit ? `Excede memoria fĂ­sica (${physicalMemoryGB} GB)` : "Allocated RAM"} -

+ + {/* Allocated RAM (configured) */} +
+
+
+
{totalAllocatedMemoryGB} GB
+
Allocated RAM
+
+ {physicalMemoryGB !== null && ( +
+ {isMemoryOvercommit ? ( + + + Exceeds Physical + + ) : ( + + Within Limits + + )} +
+ )} +
+