feat : add EnvironmentHealthProvider for detailed env metrics
This commit is contained in:
45
src/State/EnvironmentHealthProvider.php
Normal file
45
src/State/EnvironmentHealthProvider.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\ApiResource\EnvironmentHealth;
|
||||||
|
use App\Repository\EnvironmentRepository;
|
||||||
|
use App\Service\DockerService;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
final readonly class EnvironmentHealthProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EnvironmentRepository $environmentRepository,
|
||||||
|
private DockerService $dockerService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EnvironmentHealth
|
||||||
|
{
|
||||||
|
$id = $uriVariables['id'] ?? null;
|
||||||
|
$environment = $id ? $this->environmentRepository->find($id) : null;
|
||||||
|
|
||||||
|
if (null === $environment) {
|
||||||
|
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
||||||
|
}
|
||||||
|
|
||||||
|
$containerName = $environment->getContainerName();
|
||||||
|
$status = $this->dockerService->getContainerStatus($containerName);
|
||||||
|
$stats = $this->dockerService->getContainerStats($containerName);
|
||||||
|
|
||||||
|
$dto = new EnvironmentHealth();
|
||||||
|
$dto->status = $status['status'];
|
||||||
|
$dto->version = $status['version'];
|
||||||
|
$dto->startedAt = $status['startedAt'];
|
||||||
|
$dto->cpuPercent = $stats['cpuPercent'];
|
||||||
|
$dto->memoryUsage = $stats['memoryUsage'];
|
||||||
|
$dto->memoryLimit = $stats['memoryLimit'];
|
||||||
|
$dto->memoryPercent = $stats['memoryPercent'];
|
||||||
|
|
||||||
|
return $dto;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user