optimize dashboard
This commit is contained in:
68
src/Tag/TagVectorIndexHealthService.php
Normal file
68
src/Tag/TagVectorIndexHealthService.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tag;
|
||||
|
||||
final readonly class TagVectorIndexHealthService
|
||||
{
|
||||
public function __construct(
|
||||
private string $tagsNdjsonPath,
|
||||
private string $vectorTagsIndexPath,
|
||||
private string $vectorTagsMetaPath
|
||||
) {}
|
||||
|
||||
public function check(): array
|
||||
{
|
||||
$ndjsonExists = is_file($this->tagsNdjsonPath);
|
||||
$vectorExists = is_file($this->vectorTagsIndexPath);
|
||||
$metaExists = is_file($this->vectorTagsMetaPath);
|
||||
|
||||
$ndjsonTagCount = 0;
|
||||
|
||||
if ($ndjsonExists) {
|
||||
$h = @fopen($this->tagsNdjsonPath, '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['tag_id']) && !empty($data['text'])) {
|
||||
$ndjsonTagCount++;
|
||||
}
|
||||
}
|
||||
fclose($h);
|
||||
}
|
||||
}
|
||||
|
||||
$vectorTagCount = 0;
|
||||
if ($metaExists) {
|
||||
$meta = json_decode((string) file_get_contents($this->vectorTagsMetaPath), true);
|
||||
if (is_array($meta)) {
|
||||
$vectorTagCount = count($meta);
|
||||
}
|
||||
}
|
||||
|
||||
$status = $this->determineStatus($ndjsonTagCount, $vectorExists, $metaExists, $vectorTagCount);
|
||||
|
||||
return [
|
||||
'tags_ndjson_exists' => $ndjsonExists,
|
||||
'tags_ndjson_count' => $ndjsonTagCount,
|
||||
'vector_exists' => $vectorExists,
|
||||
'meta_exists' => $metaExists,
|
||||
'vector_tag_count' => $vectorTagCount,
|
||||
'status' => $status,
|
||||
];
|
||||
}
|
||||
|
||||
private function determineStatus(int $ndjsonTagCount, bool $vectorExists, bool $metaExists, int $vectorTagCount): string
|
||||
{
|
||||
if ($ndjsonTagCount === 0 && !$vectorExists && !$metaExists) return 'OK_EMPTY';
|
||||
if ($ndjsonTagCount > 0 && $vectorExists && $metaExists && $vectorTagCount === $ndjsonTagCount) return 'OK';
|
||||
if ($ndjsonTagCount === 0 && ($vectorExists || $metaExists)) return 'INCONSISTENT_STALE_VECTOR';
|
||||
if ($ndjsonTagCount > 0 && (!$vectorExists || !$metaExists)) return 'INCONSISTENT_MISSING_VECTOR';
|
||||
if ($ndjsonTagCount !== $vectorTagCount) return 'INCONSISTENT_COUNT_MISMATCH';
|
||||
return 'UNKNOWN';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user