add health check
This commit is contained in:
35
src/Command/VectorHealthCheckCommand.php
Normal file
35
src/Command/VectorHealthCheckCommand.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Vector\VectorIndexHealthService;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mto:agent:vector:health',
|
||||
description: 'Health-Check für NDJSON/FAISS Konsistenz'
|
||||
)]
|
||||
final class VectorHealthCheckCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private VectorIndexHealthService $health
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$result = $this->health->check();
|
||||
|
||||
$output->writeln(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||||
|
||||
return str_starts_with($result['status'], 'OK')
|
||||
? Command::SUCCESS
|
||||
: Command::FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Controller\Admin;
|
||||
|
||||
use App\Index\IndexMetaManager;
|
||||
use App\Ingest\IngestFlow;
|
||||
use App\Vector\VectorIndexHealthService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -12,13 +13,15 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
final class DashboardController extends AbstractController
|
||||
{
|
||||
#[Route('/admin', name: 'admin_dashboard')]
|
||||
public function index(): Response
|
||||
public function index(VectorIndexHealthService $health): Response
|
||||
{
|
||||
return $this->render('admin/dashboard/index.html.twig');
|
||||
return $this->render('admin/dashboard/index.html.twig', [
|
||||
'vectorHealth' => $health->check()
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin/dashboard', name: 'admin_dashboard')]
|
||||
public function dashboard(IndexMetaManager $metaManager): Response
|
||||
public function dashboard(IndexMetaManager $metaManager,VectorIndexHealthService $health): Response
|
||||
{
|
||||
$chunkCount = $metaManager->getRuntimeChunkCount();
|
||||
$limit = IngestFlow::CHUNK_LIMIT_HARD;
|
||||
@@ -26,6 +29,7 @@ final class DashboardController extends AbstractController
|
||||
return $this->render('admin/dashboard/index.html.twig', [
|
||||
'chunkCount' => $chunkCount,
|
||||
'chunkLimit' => $limit,
|
||||
'vectorHealth' => $health->check(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ final class IndexNdjsonInspector
|
||||
{
|
||||
if (!is_file($this->metaPath)) {
|
||||
return [
|
||||
'error' => 'index_meta.json nicht gefunden',
|
||||
'error' => 'index_meta.json nicht gefunden. Bitte laden Sie min. ein Dokument in das System.',
|
||||
'path' => $this->metaPath,
|
||||
];
|
||||
}
|
||||
@@ -67,7 +67,7 @@ final class IndexNdjsonInspector
|
||||
|
||||
if (!is_file($this->ndjsonPath)) {
|
||||
return [
|
||||
'error' => 'index.ndjson nicht gefunden',
|
||||
'error' => 'index.ndjson nicht gefunden. Bitte laden Sie min. ein Dokument in das System.',
|
||||
'path' => $this->ndjsonPath,
|
||||
'items' => [],
|
||||
];
|
||||
|
||||
93
src/Vector/VectorIndexHealthService.php
Normal file
93
src/Vector/VectorIndexHealthService.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Vector;
|
||||
|
||||
final class VectorIndexHealthService
|
||||
{
|
||||
public function __construct(
|
||||
private string $indexNdjsonPath,
|
||||
private string $vectorIndexPath,
|
||||
private string $vectorMetaPath
|
||||
) {}
|
||||
|
||||
public function check(): array
|
||||
{
|
||||
$ndjsonExists = is_file($this->indexNdjsonPath);
|
||||
$vectorExists = is_file($this->vectorIndexPath);
|
||||
$metaExists = is_file($this->vectorMetaPath);
|
||||
|
||||
$ndjsonChunkCount = 0;
|
||||
|
||||
if ($ndjsonExists) {
|
||||
$h = @fopen($this->indexNdjsonPath, 'r');
|
||||
if ($h !== false) {
|
||||
while (($line = fgets($h)) !== false) {
|
||||
$line = trim($line);
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
$data = json_decode($line, true);
|
||||
if (is_array($data) && !empty($data['chunk_id']) && !empty($data['text'])) {
|
||||
$ndjsonChunkCount++;
|
||||
}
|
||||
}
|
||||
fclose($h);
|
||||
}
|
||||
}
|
||||
|
||||
$vectorChunkCount = 0;
|
||||
if ($metaExists) {
|
||||
$meta = json_decode((string) file_get_contents($this->vectorMetaPath), true);
|
||||
if (is_array($meta)) {
|
||||
$vectorChunkCount = count($meta);
|
||||
}
|
||||
}
|
||||
|
||||
$status = $this->determineStatus(
|
||||
$ndjsonChunkCount,
|
||||
$vectorExists,
|
||||
$metaExists,
|
||||
$vectorChunkCount
|
||||
);
|
||||
|
||||
return [
|
||||
'ndjson_exists' => $ndjsonExists,
|
||||
'ndjson_chunk_count' => $ndjsonChunkCount,
|
||||
'vector_exists' => $vectorExists,
|
||||
'meta_exists' => $metaExists,
|
||||
'vector_chunk_count' => $vectorChunkCount,
|
||||
'status' => $status,
|
||||
];
|
||||
}
|
||||
|
||||
private function determineStatus(
|
||||
int $ndjsonChunkCount,
|
||||
bool $vectorExists,
|
||||
bool $metaExists,
|
||||
int $vectorChunkCount
|
||||
): string {
|
||||
if ($ndjsonChunkCount === 0 && !$vectorExists && !$metaExists) {
|
||||
return 'OK_EMPTY';
|
||||
}
|
||||
|
||||
if ($ndjsonChunkCount > 0 && $vectorExists && $metaExists && $vectorChunkCount === $ndjsonChunkCount) {
|
||||
return 'OK';
|
||||
}
|
||||
|
||||
if ($ndjsonChunkCount === 0 && ($vectorExists || $metaExists)) {
|
||||
return 'INCONSISTENT_STALE_VECTOR';
|
||||
}
|
||||
|
||||
if ($ndjsonChunkCount > 0 && (!$vectorExists || !$metaExists)) {
|
||||
return 'INCONSISTENT_MISSING_VECTOR';
|
||||
}
|
||||
|
||||
if ($ndjsonChunkCount !== $vectorChunkCount) {
|
||||
return 'INCONSISTENT_COUNT_MISMATCH';
|
||||
}
|
||||
|
||||
return 'UNKNOWN';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user