fix(docker): stats not working (#4311)

This commit is contained in:
Meier Lukas
2025-10-18 20:29:11 +02:00
committed by GitHub
parent f78eaa533e
commit 9aa2d5ccd8

View File

@@ -61,7 +61,17 @@ async function getContainersWithStatsAsync() {
})?.url ?? null,
cpuUsage: calculateCpuUsage(stats),
// memory usage by default includes cache, which should not be shown as it is also not shown with docker stats command
memoryUsage: stats.memory_stats.usage - stats.memory_stats.stats.cache,
// The below type is probably wrong, sometimes stats can be undefined
// See https://docs.docker.com/reference/cli/docker/container/stats/ how it is / was calculated
memoryUsage:
stats.memory_stats.usage -
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
(stats.memory_stats.stats?.cache ??
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
stats.memory_stats.stats?.total_inactive_file ??
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
stats.memory_stats.stats?.inactive_file ??
0),
image: container.Image,
ports: container.Ports,
};
@@ -71,11 +81,10 @@ async function getContainersWithStatsAsync() {
}
function calculateCpuUsage(stats: ContainerStats): number {
const cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage;
const systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage;
const numberOfCpus = stats.cpu_stats.online_cpus || 1;
if (systemDelta === 0) return 0;
const usage = stats.cpu_stats.system_cpu_usage;
if (usage === 0) return 0;
return (cpuDelta / systemDelta) * numberOfCpus * 100;
return (stats.cpu_stats.cpu_usage.total_usage / usage) * numberOfCpus * 100;
}