// IMPORTING CHOSEN SCAN ROWS (release 37 S2): the Captures tab's Import selected // and Import all in range, `pnpm ops wayback-import`, and the download lane's // `wayback.auto` pick all end here — one `import-page` job per row (S1's job, // controller/importPage.ts), queued on archive.org's queue, so they run one at // a time at its pace. A row already imported (a webpage record holds its // content, imported.ts) is skipped; at most IMPORT_BATCH_MAX are queued per // call. Each job takes the capture the row was first listed at — exactly that // content. import type { Paths } from "../lib/paths"; import { startImportPageJob } from "./importPage"; import { readImportedIndex, readWaybackScan } from "../lib/waybackPages/scanFile-server"; import { captureRows, filterCaptureRows, type CaptureFilter, type CaptureRowView, } from "../lib/waybackPages/scanView"; export type ImportPageRequest = { slug: string; url: string; at?: string; requestedBy?: string; paths?: Paths }; export type ImportPageStarter = (req: ImportPageRequest) => Promise<{ jobId: string }>; // At most this many jobs per call: a 50,000-row range is queued a batch at a // time, never as 50,000 records at once. export const IMPORT_BATCH_MAX = 500; export const WAYBACK_IMPORT_BODY_KEYS = ["slug", "keys", "year", "prefix", "q", "show", "dryRun"] as const; // THE DOOR to `import-page`: S1's job (controller/importPage.ts), one per row. // Its refusal (ImportPageRefusal) carries the plan's sentence. async function defaultStarter(req: ImportPageRequest): Promise<{ jobId: string }> { return startImportPageJob(req); } let starter: ImportPageStarter = defaultStarter; // Tests (and nothing else) swap the starter. export function setImportPageStarterForTest(fn: ImportPageStarter | null): void { starter = fn ?? defaultStarter; } export type CaptureImportResult = { ok: boolean; error?: string; slug: string; dryRun: boolean; // Rows the selection or the range named. chosen: number; queued: { key: string; url: string; jobId?: string }[]; skipped: { key: string; why: string }[]; // Rows left for another call (over IMPORT_BATCH_MAX). left: number; }; export type QueueCaptureImportsOpts = { paths: Paths; slug: string; // The rows by key (` `), or every row the filter keeps. keys?: ReadonlyArray; filter?: CaptureFilter; dryRun?: boolean; requestedBy?: string; }; export async function queueCaptureImports(opts: QueueCaptureImportsOpts): Promise { const base = { slug: opts.slug, dryRun: opts.dryRun === true, chosen: 0, queued: [], skipped: [], left: 0 }; const file = await readWaybackScan(opts.paths.channelsDir, opts.slug); if (!file) return { ...base, ok: false, error: `${opts.slug} has no Wayback scan yet: scan it first.` }; const rows = captureRows(file, await readImportedIndex(opts.paths.channelsDir, opts.slug)); const out: CaptureImportResult = { ...base, ok: true, queued: [], skipped: [] }; let chosen: CaptureRowView[]; if (opts.keys) { const byKey = new Map(rows.map((r) => [r.key, r])); chosen = []; for (const key of new Set(opts.keys)) { const r = byKey.get(key); if (r) chosen.push(r); else out.skipped.push({ key, why: "not in the scan" }); } } else { chosen = filterCaptureRows(rows, opts.filter ?? {}); } out.chosen = chosen.length; const todo: CaptureRowView[] = []; for (const r of chosen) { if (r.imported) out.skipped.push({ key: r.key, why: `imported as ${r.imported.id}` }); else todo.push(r); } out.left = Math.max(0, todo.length - IMPORT_BATCH_MAX); for (const r of todo.slice(0, IMPORT_BATCH_MAX)) { if (opts.dryRun) { out.queued.push({ key: r.key, url: r.replayUrl }); continue; } try { const { jobId } = await starter({ slug: opts.slug, url: r.replayUrl, paths: opts.paths, ...(opts.requestedBy ? { requestedBy: opts.requestedBy } : {}) }); out.queued.push({ key: r.key, url: r.replayUrl, jobId }); } catch (e) { // A refusal of the first is a refusal of all (the same channel, the same job). return { ...out, ok: false, error: (e as Error).message }; } } return out; } // ONE ROW, for the download lane's `wayback.auto` unit. export async function startCaptureImport(slug: string, row: Pick, requestedBy: string, paths?: Paths): Promise<{ jobId: string }> { return starter({ slug, url: row.replayUrl, requestedBy, ...(paths ? { paths } : {}) }); } // What `wayback.auto` imports: rows first listed after the channel's first // pass (a capture the Wayback Machine took, or indexed, since the channel's // scope was first listed) that no record holds yet, oldest first. The first // pass's rows are the operator's to choose. export async function pendingAutoImports(paths: Paths, slug: string): Promise { const file = await readWaybackScan(paths.channelsDir, slug); if (!file) return []; const pass1 = new Set(file.rows.filter((r) => r.pass <= 1).map((r) => `${r.digest} ${r.originalUrl}`)); return captureRows(file, await readImportedIndex(paths.channelsDir, slug)) .filter((r) => !r.imported && !pass1.has(r.key)) .sort((a, b) => a.firstTs.localeCompare(b.firstTs) || a.key.localeCompare(b.key)); }