mirror of
https://github.com/ajnart/homarr.git
synced 2026-01-29 18:59:20 +01:00
fix(docker): stats not working (#4311)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user