// THE TRACK CENSUS, persisted: transcripts vs captions per channel, as far as // a search has read each channel whole (lib/research/corpus.ts). Nothing // published counts it per scope, so it is learned by searching, and kept here // so a restart does not forget it. // // REPORTS_ROOT/.umtool/research/census/.json // { format: "umtool-research-census", version: 1, handle, identity, // updatedAt, channels: { "": { total, transcript, captions, // unknown, byYear } } } // // KEYED BY THE SOURCE AND ITS BUILD. `identity` is the source's build stamp // (sources.ts buildIdentity: corpus.json's generatedAt, and for a local dir its // mtime too). A file whose identity is not the source's current one is STALE: // its counts describe another build, so they are not used, and the scope says // so until a full search counts again and overwrites it. // // Umtool-side state, never in the corpus. Writes are lib/lifecycle.mjs's: one // writer, serialised; a whole file to a temp name and a rename. import { createHash } from "node:crypto"; import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const CENSUS_FORMAT = "umtool-research-census"; export const CENSUS_VERSION = 1; /** The file for one source handle under a reports root. */ export function censusFile(reportsRoot, handle) { const id = createHash("sha1").update(String(handle)).digest("hex").slice(0, 16); return path.join(/* turbopackIgnore: true */ reportsRoot, ".umtool", "research", "census", `${id}.json`); } const num = (v) => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : 0); function cleanCounts(c) { return { transcript: num(c?.transcript), captions: num(c?.captions), unknown: num(c?.unknown) }; } /** * The census for a source at a build. `{ channels, updatedAt }` when the file * is this build's; `{ stale: { identity, updatedAt } }` when it is another * build's; `{}` when there is none (or it cannot be read: `error` says why). * * @param {string} reportsRoot * @param {string} handle * @param {string} identity * @returns {Promise<{ channels?: Record, updatedAt?: string, stale?: { identity: string, updatedAt: string }, error?: string }>} */ export async function readCensus(reportsRoot, handle, identity) { let raw; try { raw = JSON.parse(await readFile(/* turbopackIgnore: true */ censusFile(reportsRoot, handle), "utf8")); } catch (err) { if (err?.code === "ENOENT") return {}; return { error: `the census cannot be read: ${err?.message ?? err}` }; } if (!raw || raw.format !== CENSUS_FORMAT || raw.handle !== handle || typeof raw.channels !== "object" || !raw.channels) { return { error: "not a umtool-research-census file for this source" }; } const updatedAt = typeof raw.updatedAt === "string" ? raw.updatedAt : ""; if (raw.identity !== identity) return { stale: { identity: String(raw.identity ?? ""), updatedAt } }; const channels = {}; for (const [k, c] of Object.entries(raw.channels)) { if (!c || typeof c !== "object") continue; const byYear = {}; for (const [y, v] of Object.entries(c.byYear ?? {})) byYear[y] = cleanCounts(v); channels[k] = { total: num(c.total), ...cleanCounts(c), byYear }; } return { channels, updatedAt }; } let chain = Promise.resolve(); /** * Replace the census for a source at a build (the whole map the caller holds). * * @param {string} reportsRoot * @param {string} handle * @param {string} identity * @param {Record} channels * @param {{ now?: Date }} [opts] */ export function writeCensus(reportsRoot, handle, identity, channels, { now } = {}) { const run = chain.then(async () => { const file = censusFile(reportsRoot, handle); const doc = { format: CENSUS_FORMAT, version: CENSUS_VERSION, handle, identity, updatedAt: (now ?? new Date()).toISOString(), channels: Object.fromEntries(Object.entries(channels).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))), }; await mkdir(/* turbopackIgnore: true */ path.dirname(/* turbopackIgnore: true */ file), { recursive: true }); const tmp = tmpPathFor(file); try { await writeFile(/* turbopackIgnore: true */ tmp, `${JSON.stringify(doc, null, 2)}\n`); await rename(/* turbopackIgnore: true */ tmp, file); } catch (err) { await rm(/* turbopackIgnore: true */ tmp, { force: true }).catch(() => {}); throw err; } return doc; }); chain = run.catch(() => {}); return run; }