53 lines
2.4 KiB
TypeScript
53 lines
2.4 KiB
TypeScript
/**
|
|
* One-off 存量 backfill: mirror ALL existing evidence media (screenshots +
|
|
* demo videos) for terminal-state bug/feature/security items to TOS. Mirrors
|
|
* the logic of `POST /api/internal/evidence/backfill-tos` (routes.ts:2378) so it
|
|
* can run out-of-process — the running control-server predates that route
|
|
* (stale --hot reload) and 404s it.
|
|
*
|
|
* Idempotent (files already mirrored at matching size are skipped), best-effort
|
|
* (unreachable nodes contribute zero). Run from repo root so bun auto-loads .env
|
|
* (needs TOS_ACCESS_KEY): bun scripts/backfill_tos_oneoff.ts
|
|
*/
|
|
import { listTasks, listFeatures, listSecurity, type EvidenceOwnerKind } from "../control-server/src/db";
|
|
import { mirrorEvidenceToTos } from "../control-server/src/evidence_tos";
|
|
import { isTosConfigured } from "../control-server/src/tos";
|
|
|
|
const TERMINAL = new Set(["manual_review", "fix_succeeded", "fix_failed", "unreproducible", "completed"]);
|
|
|
|
const targets: { kind: EvidenceOwnerKind; id: string }[] = [
|
|
...listTasks().filter((t) => TERMINAL.has(t.status)).map((t) => ({ kind: "bug" as const, id: t.id })),
|
|
...listFeatures().filter((f) => TERMINAL.has(f.status)).map((f) => ({ kind: "feature" as const, id: f.id })),
|
|
...listSecurity().filter((s) => TERMINAL.has(s.status)).map((s) => ({ kind: "security" as const, id: s.id })),
|
|
];
|
|
|
|
if (!isTosConfigured()) {
|
|
console.error("TOS not configured (TOS_ACCESS_KEY empty). Run from repo root so .env loads. Aborting.");
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`[backfill] ${targets.length} terminal candidates; POOL=3`);
|
|
const t0 = Date.now();
|
|
let uploaded = 0, skipped = 0, failed = 0, items = 0, done = 0;
|
|
const POOL = 3;
|
|
for (let i = 0; i < targets.length; i += POOL) {
|
|
const batch = targets.slice(i, i + POOL);
|
|
const results = await Promise.all(
|
|
batch.map(async (t) => {
|
|
const r = await mirrorEvidenceToTos(t.kind, t.id);
|
|
done++;
|
|
if (r && (r.uploaded || r.failed)) {
|
|
console.log(` [${done}/${targets.length}] ${t.kind} ${t.id.slice(0, 8)} up=${r.uploaded} skip=${r.skipped} fail=${r.failed}`);
|
|
}
|
|
return r;
|
|
}),
|
|
);
|
|
for (const r of results) {
|
|
if (!r) continue;
|
|
items++; uploaded += r.uploaded; skipped += r.skipped; failed += r.failed;
|
|
}
|
|
}
|
|
const secs = ((Date.now() - t0) / 1000).toFixed(1);
|
|
console.log(`\n[backfill] DONE in ${secs}s`);
|
|
console.log(JSON.stringify({ candidates: targets.length, itemsMirrored: items, uploaded, skipped, failed }, null, 2));
|