// HOW LONG WILL THIS TAKE RENDER? (UI notes 2026-10-30, #18) -- from the // project's own past renders, never a constant. On the Ember cut every ETA was // worked out by hand. // // A past render is a take (takes//take.json) whose render time is known: // // - `renderSeconds`, which the take maker records (takeMaker.mjs: from the // build's first event to its end, so a wait at the heavy slot's door is // not counted); // - else the take's own files: from its video.manifest.json (written just // before the render starts and renamed into place unchanged) to its // take.json `builtAt`, less any wait at the slot its log says it had (a // `.log` beside the project or in the take, the heavy gate's // `admitted after …` / `the render let go after …` lines). // // The ETA is the median seconds of render per second of video, over the past // renders of the same kind -- made `from` another take (its segments, frames // and chunks reused) or fresh, the whole cut or a window -- times the take's // length; with none of its kind it says it used the others. Plain ESM: the CLI // and the route read it under bare node. import { readdir, readFile, stat } from "node:fs/promises"; import path from "node:path"; const TAKE_ID = /^[a-z0-9][a-z0-9-]{0,63}$/; const num = (v) => typeof v === "number" && Number.isFinite(v); /** `35s`, `2m10s`, `1h02m` (queue-lock's humanDuration) -> seconds, else null. */ export function parseDuration(s) { const m = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/.exec(String(s ?? "").trim()); if (!m || (!m[1] && !m[2] && !m[3])) return null; return Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60 + Number(m[3] ?? 0); } /** The seconds a render's log says it waited at the heavy slot (its door, its line, memory). */ export function slotWaitOf(text) { let s = 0; for (const m of String(text ?? "").matchAll(/(?:heavy: .* after |queue-lock: the render let go after )(\d+h)?(\d+m)?(\d+s)?(?: — admitted| — going on)/g)) { s += parseDuration(`${m[1] ?? ""}${m[2] ?? ""}${m[3] ?? ""}`) ?? 0; } return s; } /** * Every past render of the project with its render time and length. * @param {string} projectDir * @returns {Promise>} */ export async function pastRenders(projectDir) { const takesDir = path.join(projectDir, "takes"); const names = await readdir(takesDir).catch(() => []); const out = []; for (const id of names) { if (!TAKE_ID.test(id)) continue; const dir = path.join(takesDir, id); let t; try { t = JSON.parse(await readFile(path.join(dir, "take.json"), "utf8")); } catch { continue; } if (t?.draft === true || !num(t?.seconds) || t.seconds <= 0) continue; let renderSeconds = num(t.renderSeconds) && t.renderSeconds > 0 ? t.renderSeconds : null; let how = /** @type {"recorded" | "files"} */ ("recorded"); if (renderSeconds === null && typeof t.builtAt === "string") { const st = await stat(path.join(dir, "video.manifest.json")).catch(() => null); const end = Date.parse(t.builtAt); if (st && Number.isFinite(end)) { let wait = 0; for (const log of [path.join(projectDir, `${id}.log`), path.join(dir, `${id}.log`), path.join(dir, "take.log")]) { const text = await readFile(log, "utf8").catch(() => null); if (text) { wait = slotWaitOf(text); break; } } const s = (end - st.mtimeMs) / 1000 - wait; // A manifest older than a day before its build is a take rebuilt in // place long after: its files say nothing about the render. if (s > 0 && s < 86_400) { renderSeconds = Math.round(s * 10) / 10; how = "files"; } } } if (renderSeconds === null) continue; out.push({ id, seconds: t.seconds, renderSeconds, how, window: t.window && t.window !== "full" ? "window" : "full", from: typeof t.from === "string" && t.from ? t.from : null, quick: t.quick === true, builtAt: typeof t.builtAt === "string" ? t.builtAt : null, }); } return out.sort((a, b) => String(a.builtAt).localeCompare(String(b.builtAt))); } const median = (xs) => { const s = [...xs].sort((a, b) => a - b); const n = s.length; return n % 2 ? s[(n - 1) / 2] : (s[n / 2 - 1] + s[n / 2]) / 2; }; /** * The ETA of a take of `seconds` of video. Pure over `past`. * @param {Awaited>} past * @param {{ seconds: number, from?: boolean, window?: "full" | "window", quick?: boolean, last?: number }} want * `from`: made from another take (reuse); `last`: at most this many of the newest renders count (default 6) * @returns {null | { etaSeconds: number, rate: number, basis: Array<{ id: string, seconds: number, renderSeconds: number, how: string }>, kind: string, note: string | null }} */ export function takeEta(past, { seconds, from = false, window = "full", quick = false, last = 6 }) { if (!num(seconds) || seconds <= 0 || !past.length) return null; const kindOf = (p) => `${p.from ? "reuse" : "fresh"}/${p.window}${p.quick ? "/quick" : ""}`; const want = `${from ? "reuse" : "fresh"}/${window}${quick ? "/quick" : ""}`; let pool = past.filter((p) => kindOf(p) === want); let note = null; if (!pool.length) { pool = past.filter((p) => p.window === window); if (!pool.length) pool = past; note = `no past ${from ? "take made from another" : "fresh take"}${window === "window" ? " of a window" : " of the whole cut"} — from the project's other renders`; } pool = pool.slice(-last); const rate = median(pool.map((p) => p.renderSeconds / p.seconds)); return { etaSeconds: Math.round(rate * seconds), rate: Math.round(rate * 100) / 100, basis: pool.map((p) => ({ id: p.id, seconds: p.seconds, renderSeconds: p.renderSeconds, how: p.how })), kind: want, note, }; } /** `1 h 05 min` / `23 min` / `40 s`. */ export function etaText(s) { if (!num(s)) return "—"; if (s < 90) return `${Math.round(s)} s`; const m = Math.round(s / 60); return m >= 60 ? `${Math.floor(m / 60)} h ${String(m % 60).padStart(2, "0")} min` : `${m} min`; } /** * The project's ETAs for the take panel: a whole-cut take fresh and made from * the newest take, at the length of the newest whole-cut take. * @param {string} projectDir */ export async function projectEtas(projectDir) { const past = await pastRenders(projectDir); const fulls = past.filter((p) => p.window === "full"); const newest = fulls.at(-1) ?? null; const seconds = newest?.seconds ?? null; return { past, seconds, newest: newest?.id ?? null, fresh: seconds ? takeEta(past, { seconds, from: false }) : null, reuse: seconds ? takeEta(past, { seconds, from: true }) : null, }; }