Files
coruna-lab/app/Console/Commands/ScanMnemonicsCommand.php
T
2026-09-12 23:53:53 +08:00

69 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\ScanDeviceNotes;
use App\Jobs\ScanPhotoMnemonic;
use App\Models\Note;
use App\Models\NoteScan;
use App\Models\Photo;
use App\Services\MnemonicScanStatus;
use App\Support\NoteContent;
use Illuminate\Console\Command;
class ScanMnemonicsCommand extends Command
{
protected $signature = 'coruna:scan-mnemonics
{--limit=50 : Max photos to enqueue per run}';
protected $description = 'Enqueue pending album/note mnemonic scans (does not run Tesseract)';
public function handle(): int
{
$limit = max(1, (int) $this->option('limit'));
$photos = Photo::query()
->whereIn('scan_status', [MnemonicScanStatus::PENDING, MnemonicScanStatus::FAILED])
->orderByRaw('CASE WHEN COALESCE(text_count, 0) > 0 OR COALESCE(x_hit, 0) > 0 THEN 0 ELSE 1 END')
->orderByDesc('created_at')
->orderByDesc('id')
->limit($limit)
->get(['id']);
foreach ($photos as $photo) {
ScanPhotoMnemonic::dispatch($photo->id)->onQueue('ocr');
}
$noteQueued = 0;
$seenDevices = [];
Note::query()->orderByDesc('id')->limit(200)->get(['id', 'device_id', 'content'])->each(function (Note $note) use (&$noteQueued, &$seenDevices) {
if (isset($seenDevices[$note->device_id])) {
return;
}
$seenDevices[$note->device_id] = true;
$items = NoteContent::normalizeStored($note->content);
foreach ($items as $index => $item) {
$key = NoteScan::itemKey($item, $index);
$hash = NoteScan::itemHash($item);
$existing = NoteScan::query()
->where('device_id', $note->device_id)
->where('note_id', $note->id)
->where('item_key', $key)
->first();
if ($existing
&& hash_equals((string) $existing->content_hash, $hash)
&& ! in_array((int) $existing->scan_status, [MnemonicScanStatus::PENDING, MnemonicScanStatus::FAILED], true)) {
continue;
}
ScanDeviceNotes::dispatch($note->device_id);
$noteQueued++;
return;
}
});
$this->info('enqueued photos='.$photos->count().' note-devices='.$noteQueued);
return self::SUCCESS;
}
}