38 lines
2.3 KiB
TypeScript
38 lines
2.3 KiB
TypeScript
// PEP-198: the evening digest of finished stories nobody waited for (core/ready-digest.ts). Once a
|
|
// working day per project, after the end of the working hours of the watchdog (PEP-173:
|
|
// `workTo`, `workDays`, platform time zone): one message of the analyst in the project's
|
|
// discussion — it reaches the project's people in Telegram and the browser like any message of
|
|
// the analyst. An empty digest is not sent.
|
|
import { ensureProjectChat, heldReadyStories, markReadyDigest, pausedProjectIds, readyDigestDay, readyDigestText, listProjects } from "@peppa/core";
|
|
import type { Db } from "@peppa/db";
|
|
import { sayInDiscussion } from "./analyst-chat.ts";
|
|
import { getWatchdogSettings } from "./recovery.ts";
|
|
|
|
/** The day (YYYY-MM-DD), hour and weekday (1 = Mon) in the platform's time zone. */
|
|
function localNow(now: Date): { day: string; hour: number; weekday: number } {
|
|
const parts = new Intl.DateTimeFormat("en-GB", { timeZone: process.env.PEPPA_TZ || undefined, year: "numeric", month: "2-digit", day: "2-digit", weekday: "short", hour: "numeric", hourCycle: "h23" }).formatToParts(now);
|
|
const get = (t: string) => parts.find((x) => x.type === t)?.value ?? "";
|
|
return { day: `${get("year")}-${get("month")}-${get("day")}`, hour: Number(get("hour")), weekday: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].indexOf(get("weekday")) + 1 };
|
|
}
|
|
|
|
export async function readyDigestTick(db: Db, o: { now?: Date; log?: (m: string) => void } = {}): Promise<number> {
|
|
if (process.env.PEPPA_READY_DIGEST === "off") return 0;
|
|
const now = o.now ?? new Date();
|
|
const cfg = await getWatchdogSettings(db);
|
|
const t = localNow(now);
|
|
if (!cfg.workDays.includes(t.weekday) || t.hour < cfg.workTo) return 0;
|
|
const paused = await pausedProjectIds(db);
|
|
let sent = 0;
|
|
for (const project of await listProjects(db)) {
|
|
if (paused.has(project.id) || (await readyDigestDay(db, project.id)) === t.day) continue;
|
|
const items = await heldReadyStories(db, project);
|
|
await markReadyDigest(db, project.id, t.day);
|
|
if (!items.length) continue;
|
|
const chat = await ensureProjectChat(db, project.id);
|
|
await sayInDiscussion(db, project, chat.id, readyDigestText(items, `${process.env.PEPPA_WEB_URL ?? ""}/`));
|
|
o.log?.(`ready digest ${project.key}: ${items.length}`);
|
|
sent++;
|
|
}
|
|
return sent;
|
|
}
|