Expose preview batches, rate-limit status, and live option updates
This commit is contained in:
@@ -384,6 +384,59 @@ export class ScrapeStore {
|
||||
.run(preview.hash, preview.bytes, preview.ext ?? "webp", id);
|
||||
}
|
||||
|
||||
/** Preview state for the whole scrape and per kind, for the backfill UI. */
|
||||
previewCounts(): {
|
||||
total: number;
|
||||
pending: number;
|
||||
cached: number;
|
||||
failed: number;
|
||||
byKind: Record<MediaKind, { total: number; pending: number; cached: number; failed: number }>;
|
||||
} {
|
||||
const empty = () => ({ total: 0, pending: 0, cached: 0, failed: 0 });
|
||||
const byKind: Record<MediaKind, { total: number; pending: number; cached: number; failed: number }> = {
|
||||
image: empty(),
|
||||
gif: empty(),
|
||||
video: empty(),
|
||||
audio: empty()
|
||||
};
|
||||
|
||||
const rows = this.db
|
||||
.prepare(
|
||||
`SELECT kind,
|
||||
COUNT(*) AS total,
|
||||
SUM(preview_hash IS NOT NULL) AS cached,
|
||||
SUM(preview_error IS NOT NULL) AS failed,
|
||||
SUM(preview_hash IS NULL AND preview_error IS NULL) AS pending
|
||||
FROM media
|
||||
GROUP BY kind`
|
||||
)
|
||||
.all() as unknown as Array<{
|
||||
kind: MediaKind;
|
||||
total: number;
|
||||
cached: number;
|
||||
failed: number;
|
||||
pending: number;
|
||||
}>;
|
||||
|
||||
const totals = empty();
|
||||
for (const row of rows) {
|
||||
const bucket = byKind[row.kind] ?? empty();
|
||||
bucket.total += row.total;
|
||||
bucket.pending += row.pending ?? 0;
|
||||
bucket.cached += row.cached ?? 0;
|
||||
bucket.failed += row.failed ?? 0;
|
||||
byKind[row.kind] = bucket;
|
||||
|
||||
totals.total += row.total;
|
||||
totals.pending += row.pending ?? 0;
|
||||
totals.cached += row.cached ?? 0;
|
||||
totals.failed += row.failed ?? 0;
|
||||
}
|
||||
|
||||
return { ...totals, byKind };
|
||||
}
|
||||
|
||||
/** Count one transient preview failure and return the new total. */
|
||||
/** Count one transient preview failure and return the new total. */
|
||||
bumpPreviewAttempt(id: number): number {
|
||||
this.db.prepare("UPDATE media SET preview_attempts = preview_attempts + 1 WHERE id = ?").run(id);
|
||||
|
||||
Reference in New Issue
Block a user