Skip to content

Commit

Permalink
fix: div by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Jun 13, 2024
1 parent fbab2d7 commit dac98a0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public function getStats(?string $container = null, array $filters = []): array

$stats = \json_decode($result['response'], true);

// Calculate CPU usage
$cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage'];
$systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage'];
$numberCpus = $stats['cpu_stats']['online_cpus'];
Expand All @@ -295,6 +296,12 @@ public function getStats(?string $container = null, array $filters = []): array
$cpuUsage = 0.0;
}

// Calculate memory usage (unsafe div /0)
$memoryUsage = 0.0;
if ($stats['memory_stats']['limit'] > 0 && $stats['memory_stats']['usage'] > 0) {
$memoryUsage = ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0;
}

// Calculate network I/O
$networkIn = 0;
$networkOut = 0;
Expand Down Expand Up @@ -324,7 +331,7 @@ public function getStats(?string $container = null, array $filters = []): array
containerId: $stats['id'],
containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix
cpuUsage: $cpuUsage,
memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0,
memoryUsage: $memoryUsage,
diskIO: ['in' => $diskRead, 'out' => $diskWrite],
memoryIO: ['in' => $memoryIn, 'out' => $memoryOut],
networkIO: ['in' => $networkIn, 'out' => $networkOut],
Expand Down

0 comments on commit dac98a0

Please sign in to comment.