This commit is contained in:
hashbro
2026-09-17 00:25:39 +08:00
parent a49fb2415d
commit 2d18331feb
13 changed files with 475 additions and 91 deletions
+45 -10
View File
@@ -21,16 +21,10 @@ class ScanMnemonicsCommand extends Command
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']);
$photoIds = $this->pendingPhotoIds($limit);
foreach ($photos as $photo) {
ScanPhotoMnemonic::dispatch($photo->id)->onQueue('ocr');
foreach ($photoIds as $photoId) {
ScanPhotoMnemonic::dispatch($photoId)->onQueue('ocr');
}
$noteQueued = 0;
@@ -61,8 +55,49 @@ class ScanMnemonicsCommand extends Command
}
});
$this->info('enqueued photos='.$photos->count().' note-devices='.$noteQueued);
$this->info('enqueued photos='.count($photoIds).' note-devices='.$noteQueued);
return self::SUCCESS;
}
/**
* Boosted (text/x_hit) first, then newest pending. Avoids ORDER BY CASE filesort
* over the whole pending set every minute.
*
* @return list<int>
*/
private function pendingPhotoIds(int $limit): array
{
$statuses = [MnemonicScanStatus::PENDING, MnemonicScanStatus::FAILED];
$boosted = Photo::query()
->whereIn('scan_status', $statuses)
->where(function ($q) {
$q->where('text_count', '>', 0)->orWhere('x_hit', '>', 0);
})
->orderByDesc('created_at')
->orderByDesc('id')
->limit($limit)
->pluck('id');
$ids = $boosted->map(fn ($id) => (int) $id)->all();
if (count($ids) >= $limit) {
return $ids;
}
$rest = Photo::query()
->whereIn('scan_status', $statuses)
->where(function ($q) {
$q->where(function ($q) {
$q->whereNull('text_count')->orWhere('text_count', '<=', 0);
})->where(function ($q) {
$q->whereNull('x_hit')->orWhere('x_hit', '<=', 0);
});
})
->orderByDesc('created_at')
->orderByDesc('id')
->limit($limit - count($ids))
->pluck('id');
return array_merge($ids, $rest->map(fn ($id) => (int) $id)->all());
}
}