All checks were successful
Auto Tag Develop / tag (push) Successful in 6s
Reviewed-on: #1 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?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;
|
|
}
|
|
}
|