Skip to content

Commit

Permalink
chore: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Nov 19, 2024
1 parent 6912bea commit 2de11fb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,23 +307,23 @@ public function getStats(?string $container = null, array $filters = []): array
} else {
$containerIds[] = $container;
}

$list = [];

foreach ($containerIds as $containerId) {
try {
$result = $this->call('http://localhost/containers/'.$containerId.'/stats?stream=false', 'GET');

if ($result['code'] !== 200 || empty($result['response'])) {
continue; // Skip to the next container
}

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

if (! isset($stats['id']) || ! isset($stats['precpu_stats']) || ! isset($stats['cpu_stats']) || ! isset($stats['memory_stats']) || ! isset($stats['networks'])) {
continue; // Skip to the next container
}

// 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'];
Expand All @@ -333,25 +333,25 @@ public function getStats(?string $container = null, array $filters = []): array
} else {
$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;
foreach ($stats['networks'] as $network) {
$networkIn += $network['rx_bytes'];
$networkOut += $network['tx_bytes'];
}

// Calculate disk I/O
$diskRead = 0;
$diskWrite = 0;
if (isset($stats['blkio_stats']['io_service_bytes_recursive'])) {
if (isset($stats['blkio_stats']['io_service_bytes_recursive'])) {
foreach ($stats['blkio_stats']['io_service_bytes_recursive'] as $entry) {
if ($entry['op'] === 'Read') {
$diskRead += $entry['value'];
Expand All @@ -360,11 +360,11 @@ public function getStats(?string $container = null, array $filters = []): array
}
}
}

// Calculate memory I/O (approximated)
$memoryIn = $stats['memory_stats']['usage'] ?? 0;
$memoryOut = $stats['memory_stats']['max_usage'] ?? 0;

$list[] = new Stats(
containerId: $stats['id'],
containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix
Expand All @@ -378,7 +378,7 @@ public function getStats(?string $container = null, array $filters = []): array
continue;
}
}

return $list;
}

Expand Down

0 comments on commit 2de11fb

Please sign in to comment.