Skip to content

Commit

Permalink
fix: empty getstats
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Nov 19, 2024
1 parent a16e27a commit b575f5f
Showing 1 changed file with 66 additions and 61 deletions.
127 changes: 66 additions & 61 deletions src/Orchestration/Adapter/DockerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utopia\Orchestration\Adapter;

use stdClass;
use Utopia\CLI\Console;
use Utopia\Orchestration\Adapter;
use Utopia\Orchestration\Container;
use Utopia\Orchestration\Container\Stats;
Expand Down Expand Up @@ -307,74 +308,78 @@ public function getStats(?string $container = null, array $filters = []): array
} else {
$containerIds[] = $container;
}

$list = [];

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

if ($result['code'] !== 200 || empty($result['response'])) {
throw new Orchestration($result['response']);
}

$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);
}

// 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 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;
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'];
$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 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'];
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],
);
} catch (\Throwable $e) {
continue;
}

// 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],
);
}

return $list;
}

Expand Down

0 comments on commit b575f5f

Please sign in to comment.