feat : add DatabaseInfo API resource and provider
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
29
src/ApiResource/DatabaseInfo.php
Normal file
29
src/ApiResource/DatabaseInfo.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\ApiResource;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiResource;
|
||||||
|
use ApiPlatform\Metadata\Get;
|
||||||
|
use App\State\DatabaseInfoProvider;
|
||||||
|
|
||||||
|
#[ApiResource(
|
||||||
|
operations: [
|
||||||
|
new Get(
|
||||||
|
uriTemplate: '/environments/{id}/database',
|
||||||
|
security: "is_granted('ROLE_ADMIN')",
|
||||||
|
provider: DatabaseInfoProvider::class,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)]
|
||||||
|
final class DatabaseInfo
|
||||||
|
{
|
||||||
|
public bool $connected = false;
|
||||||
|
public string $name = '';
|
||||||
|
public string $size = '';
|
||||||
|
public int $tableCount = 0;
|
||||||
|
public int $activeConnections = 0;
|
||||||
|
public float $cacheHitRatio = 0.0;
|
||||||
|
public string $largestTable = '';
|
||||||
|
}
|
||||||
49
src/State/DatabaseInfoProvider.php
Normal file
49
src/State/DatabaseInfoProvider.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\State;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\ProviderInterface;
|
||||||
|
use App\ApiResource\DatabaseInfo;
|
||||||
|
use App\Repository\EnvironmentRepository;
|
||||||
|
use App\Service\DatabaseService;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
|
final readonly class DatabaseInfoProvider implements ProviderInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EnvironmentRepository $environmentRepository,
|
||||||
|
private DatabaseService $databaseService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): DatabaseInfo
|
||||||
|
{
|
||||||
|
$id = $uriVariables['id'] ?? null;
|
||||||
|
$environment = $id ? $this->environmentRepository->find($id) : null;
|
||||||
|
|
||||||
|
if (null === $environment) {
|
||||||
|
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
||||||
|
}
|
||||||
|
|
||||||
|
$databaseName = $environment->getDatabaseName();
|
||||||
|
|
||||||
|
if (null === $databaseName || '' === $databaseName) {
|
||||||
|
throw new NotFoundHttpException('No database configured for this environment.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$info = $this->databaseService->getDatabaseInfo($databaseName);
|
||||||
|
|
||||||
|
$dto = new DatabaseInfo();
|
||||||
|
$dto->connected = $info['connected'];
|
||||||
|
$dto->name = $info['name'];
|
||||||
|
$dto->size = $info['size'];
|
||||||
|
$dto->tableCount = $info['tableCount'];
|
||||||
|
$dto->activeConnections = $info['activeConnections'];
|
||||||
|
$dto->cacheHitRatio = $info['cacheHitRatio'];
|
||||||
|
$dto->largestTable = $info['largestTable'];
|
||||||
|
|
||||||
|
return $dto;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user