import { listScheduleReads, readLivingSchedule, readScheduleById } from "yt-dlp-transcript-common/lib/report/schedule/store"; import { callOf, scheduleChip, spentOf, type Schedule } from "yt-dlp-transcript-common/lib/report/schedule/types"; import { cadenceWords, tickWords } from "yt-dlp-transcript-common/lib/report/schedule/cadence"; import { ARTICLES_KEY } from "./articles/key.mjs"; import { sitesPaths } from "./articles/sites"; import { askEditor } from "./articles/generation"; import { articleHref, seriesHref } from "./hrefs"; import { articlePieceId, seriesPieceId } from "./pieces/ids.mjs"; import type { ScheduleChipData, ScheduleView } from "./schedule-types"; // --------------------------------------------------------------------------- // RECURRING REPORTS, umtool's side (plans/umtool-redesign.md, R5). The editor // keeps the schedules -- its report-schedule lane ticks them -- and they are // stored in the article store (common lib/report/schedule/store.ts: a living // report's beside its article, a series in reports/.series//). umtool // READS them from disk (this file) and WRITES only through the editor's ops // action, `report-schedule` (scheduleAsk): it never runs a timer itself. // // SERVER ONLY. // --------------------------------------------------------------------------- export function viewOfSchedule(s: Schedule, now: number = Date.now()): ScheduleView { const article = s.shape === "living" ? (s.article ?? s.id) : null; const next = s.nextTick ? Date.parse(s.nextTick) : NaN; return { ...s, chip: scheduleChip(s, now), cadenceWords: cadenceWords(s.cadence), nextWords: !s.paused && Number.isFinite(next) ? tickWords(next, s.tz, now) : null, call: callOf(s), spentUsd: spentOf(s), href: article ? articleHref({ site: ARTICLES_KEY, id: article }) : seriesHref(s.id), piece: article ? articlePieceId(ARTICLES_KEY, article) : seriesPieceId(s.id), }; } export const chipOf = (v: ScheduleView): ScheduleChipData => ({ id: v.id, shape: v.shape, chip: v.chip, paused: v.paused, href: v.href, running: !!v.running, }); /** Every schedule that reads, and the files that do not. */ export async function readSchedules(): Promise<{ schedules: ScheduleView[]; problems: { id: string; problem: string }[] }> { const reads = await listScheduleReads(sitesPaths()); const now = Date.now(); return { schedules: reads.flatMap((r) => (r.ok ? [viewOfSchedule(r.schedule, now)] : [])), problems: reads.flatMap((r) => (r.ok ? [] : [{ id: r.id, problem: r.problem }])), }; } /** One schedule by id (a series', or a living report's article): the view, its problem, or null. */ export async function readScheduleView(id: string): Promise<{ view: ScheduleView } | { problem: string } | null> { const r = await readScheduleById(id, sitesPaths()); if (!r) return null; return r.ok ? { view: viewOfSchedule(r.schedule) } : { problem: r.problem }; } /** A store article's living schedule, or null. */ export async function livingScheduleOf(article: string): Promise { const s = await readLivingSchedule(article, sitesPaths()).catch(() => null); return s ? viewOfSchedule(s) : null; } /** Ask the editor's `report-schedule` action (list, create, update, pause, resume, delete, tick-now). */ export function scheduleAsk>(body: Record): Promise { return askEditor("/api/ops/report-schedule", { method: "POST", body }); }