Skip to content

Commit

Permalink
Merge pull request #57 from utopia-php/fix-empty-getstats
Browse files Browse the repository at this point in the history
fix: empty getstats
  • Loading branch information
christyjacob4 authored and loks0n committed Nov 21, 2024
1 parent 331b831 commit 858e3d1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 89 deletions.
68 changes: 34 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 56 additions & 52 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,68 +311,72 @@ public function getStats(?string $container = null, array $filters = []): array
$list = [];

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

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

$stats = \json_decode($result['response'], true);
$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'])) {
throw new Orchestration('Failed to get stats for container: '.$containerId);
}
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'];
$numberCpus = $stats['cpu_stats']['online_cpus'];
if ($systemCpuDelta > 0 && $cpuDelta > 0) {
$cpuUsage = ($cpuDelta / $systemCpuDelta) * $numberCpus;
} else {
$cpuUsage = 0.0;
}
// 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'];
if ($systemCpuDelta > 0 && $cpuDelta > 0) {
$cpuUsage = ($cpuDelta / $systemCpuDelta) * $numberCpus;
} 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 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 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'])) {
foreach ($stats['blkio_stats']['io_service_bytes_recursive'] as $entry) {
if ($entry['op'] === 'Read') {
$diskRead += $entry['value'];
} elseif ($entry['op'] === 'Write') {
$diskWrite += $entry['value'];
// Calculate disk I/O
$diskRead = 0;
$diskWrite = 0;
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'];
} elseif ($entry['op'] === 'Write') {
$diskWrite += $entry['value'];
}
}
}
}

// 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
cpuUsage: $cpuUsage,
memoryUsage: $memoryUsage,
diskIO: ['in' => $diskRead, 'out' => $diskWrite],
memoryIO: ['in' => $memoryIn, 'out' => $memoryOut],
networkIO: ['in' => $networkIn, 'out' => $networkOut],
);
// 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
cpuUsage: $cpuUsage,
memoryUsage: $memoryUsage,
diskIO: ['in' => $diskRead, 'out' => $diskWrite],
memoryIO: ['in' => $memoryIn, 'out' => $memoryOut],
networkIO: ['in' => $networkIn, 'out' => $networkOut],
);
} catch (\Throwable $e) {
continue;
}
}

return $list;
Expand Down
2 changes: 1 addition & 1 deletion src/Orchestration/Adapter/DockerCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function getStats(?string $container = null, array $filters = []): array
$result = Console::execute('docker stats --no-trunc --format "id={{.ID}}&name={{.Name}}&cpu={{.CPUPerc}}&memory={{.MemPerc}}&diskIO={{.BlockIO}}&memoryIO={{.MemUsage}}&networkIO={{.NetIO}}" --no-stream'.$containersString, '', $output);

if ($result !== 0) {
throw new Orchestration("Docker Error: {$output}");
return [];
}

$lines = \explode("\n", $output);
Expand Down
3 changes: 1 addition & 2 deletions tests/Orchestration/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,9 +712,8 @@ public function testUsageStats(): void
/**
* Test for Failure
*/
$this->expectException(\Exception::class);

$stats = static::getOrchestration()->getStats('IDontExist');
$this->assertCount(0, $stats);
}

public function testNetworkExists(): void
Expand Down

0 comments on commit 858e3d1

Please sign in to comment.