diff --git a/plogical/DockerSites.py b/plogical/DockerSites.py index 9f7c007de..0d2e70f70 100644 --- a/plogical/DockerSites.py +++ b/plogical/DockerSites.py @@ -740,30 +740,27 @@ services: # Calculate CPU percentage properly with safeguards try: - # Get the CPU stats - cpu_stats = stats["cpu_stats"] - precpu_stats = stats["precpu_stats"] + cpu_stats = stats.get("cpu_stats", {}) + precpu_stats = stats.get("precpu_stats", {}) - # Calculate CPU delta - cpu_delta = float(cpu_stats["cpu_usage"]["total_usage"]) - float(precpu_stats["cpu_usage"]["total_usage"]) + # Get CPU usage values + cpu_total = float(cpu_stats.get("cpu_usage", {}).get("total_usage", 0)) + precpu_total = float(precpu_stats.get("cpu_usage", {}).get("total_usage", 0)) - # Calculate system CPU delta - system_delta = float(cpu_stats["system_cpu_usage"]) - float(precpu_stats["system_cpu_usage"]) + # Get system CPU values + sys_total = float(cpu_stats.get("system_cpu_usage", 0)) + presys_total = float(precpu_stats.get("system_cpu_usage", 0)) - # Get number of CPUs - online_cpus = cpu_stats.get("online_cpus", len(cpu_stats["cpu_usage"]["percpu_usage"])) + cpu_delta = cpu_total - precpu_total + system_delta = sys_total - presys_total cpu_usage = 0.0 - if system_delta > 0 and cpu_delta >= 0: - # Calculate percentage of total CPU used - cpu_usage = (cpu_delta / system_delta) * online_cpus * 100.0 + if system_delta > 0: + # Calculate percentage of single CPU + cpu_usage = (cpu_delta / system_delta) * 100.0 - # Cap at 100% per core - cpu_usage = min(cpu_usage, online_cpus * 100.0) - - # Ensure we return a valid number - if cpu_usage < 0 or math.isnan(cpu_usage): - cpu_usage = 0.0 + # Ensure it's a reasonable value + cpu_usage = max(0.0, min(100.0, cpu_usage)) except Exception as e: logging.writeToFile(f"CPU calculation error: {str(e)}") diff --git a/websiteFunctions/static/websiteFunctions/DockerContainers.js b/websiteFunctions/static/websiteFunctions/DockerContainers.js index d5d014acc..9f47e8e11 100644 --- a/websiteFunctions/static/websiteFunctions/DockerContainers.js +++ b/websiteFunctions/static/websiteFunctions/DockerContainers.js @@ -99,14 +99,11 @@ app.controller('ListDockersitecontainer', function ($scope, $http) { $scope.ContainerList[i].memoryUsage = formatBytes(memoryBytes); $scope.ContainerList[i].memoryUsagePercent = (memoryBytes / (1024 * 1024 * 1024)) * 100; - // Ensure CPU usage is a valid number and cap it at 800% (8 cores at 100% each) + // CPU Usage - ensure it's a valid number and keep original value var cpuUsage = parseFloat(containerInfo.cpu_usage); - if (isNaN(cpuUsage) || cpuUsage < 0) { - cpuUsage = 0; - } else if (cpuUsage > 800) { - cpuUsage = 800; - } - $scope.ContainerList[i].cpuUsagePercent = cpuUsage; + $scope.ContainerList[i].cpuUsagePercent = isNaN(cpuUsage) ? 0 : cpuUsage; + + console.log('Container CPU Usage:', containerInfo.name, cpuUsage); // Network & Ports $scope.ContainerList[i].ports = containerInfo.ports;