import { readFile, writeFile, mkdir } from "node:fs/promises"; import { DATA } from "./paths.js"; // What happened to the catalog and the university, newest first, for the dashboard: the same // events the Telegram messages report, plus republished documents without new problems. // The last hundred are kept on disk, so a restart does not empty the feed. const FILE = "events.json"; const MAX = 100; let events; async function load() { if (events) return events; try { events = JSON.parse(await readFile(DATA + FILE, "utf8")); if (!Array.isArray(events)) events = []; } catch { events = []; } return events; } /** * Adds events: `{kind, title, text?, doc?}`. Kinds: new, republished, failed, year, year_late, * source_down, source_up. */ export async function recordEvents(list) { if (!list.length) return; await load(); const t = new Date().toISOString(); events.unshift(...list.map((e) => ({ t, ...e }))); events.length = Math.min(events.length, MAX); await mkdir(DATA, { recursive: true }); await writeFile(DATA + FILE, JSON.stringify(events)); } export async function listEvents(limit = 50) { return (await load()).slice(0, limit); }