First commit
This commit is contained in:
107
app/Console/Commands/ReindexDocumentCommand.php
Normal file
107
app/Console/Commands/ReindexDocumentCommand.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\ReindexDocumentJob;
|
||||
use App\Models\Document;
|
||||
use App\Models\DocumentVersion;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
/**
|
||||
* php artisan kb:reindex-document {--document_id=} {--version_id=} {--all-failed}
|
||||
*
|
||||
* Contoh:
|
||||
* php artisan kb:reindex-document --document_id=5
|
||||
* php artisan kb:reindex-document --version_id=12
|
||||
* php artisan kb:reindex-document --all-failed
|
||||
*/
|
||||
class ReindexDocumentCommand extends Command
|
||||
{
|
||||
protected $signature = 'kb:reindex-document
|
||||
{--document_id= : ID dokumen untuk reindex versi semasa}
|
||||
{--version_id= : ID versi spesifik untuk reindex}
|
||||
{--all-failed : Reindex semua versi yang gagal}';
|
||||
|
||||
protected $description = 'Reindex (re-embed) dokumen dalam Qdrant';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
if ($this->option('all-failed')) {
|
||||
return $this->reindexAllFailed();
|
||||
}
|
||||
|
||||
if ($versionId = $this->option('version_id')) {
|
||||
return $this->reindexVersion((int) $versionId);
|
||||
}
|
||||
|
||||
if ($documentId = $this->option('document_id')) {
|
||||
return $this->reindexDocument((int) $documentId);
|
||||
}
|
||||
|
||||
$this->error('Sila nyatakan --document_id, --version_id, atau --all-failed');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
private function reindexDocument(int $documentId): int
|
||||
{
|
||||
$document = Document::find($documentId);
|
||||
|
||||
if (!$document) {
|
||||
$this->error("Dokumen ID {$documentId} tidak dijumpai.");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$currentVersion = $document->currentVersion;
|
||||
|
||||
if (!$currentVersion) {
|
||||
$this->error("Dokumen '{$document->title}' tiada versi semasa.");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
ReindexDocumentJob::dispatch($currentVersion->id);
|
||||
|
||||
$this->info("✓ Reindex dijadualkan untuk dokumen: {$document->title} (v{$currentVersion->version_number})");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function reindexVersion(int $versionId): int
|
||||
{
|
||||
$version = DocumentVersion::with('document')->find($versionId);
|
||||
|
||||
if (!$version) {
|
||||
$this->error("Version ID {$versionId} tidak dijumpai.");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
ReindexDocumentJob::dispatch($version->id);
|
||||
|
||||
$this->info("✓ Reindex dijadualkan untuk: {$version->document->title} v{$version->version_number}");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function reindexAllFailed(): int
|
||||
{
|
||||
$failedVersions = DocumentVersion::whereIn('processing_status', [
|
||||
DocumentVersion::STATUS_FAILED,
|
||||
DocumentVersion::STATUS_EXTRACTION_FAILED,
|
||||
])->with('document')->get();
|
||||
|
||||
if ($failedVersions->isEmpty()) {
|
||||
$this->info('Tiada versi yang gagal ditemui.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
foreach ($failedVersions as $version) {
|
||||
ReindexDocumentJob::dispatch($version->id);
|
||||
$this->line(" → {$version->document->title} v{$version->version_number}");
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->info("✓ {$count} versi telah dijadualkan untuk reindex.");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user