mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-10-22 05:54:10 -04:00
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import type { SnapshotPayload } from '$lib/types/stats';
|
|
import { sortSignals } from './signals';
|
|
|
|
/** Compose the ~6-line plain-language daily digest. Pure / testable. */
|
|
export function composeDigest(payload: SnapshotPayload): string {
|
|
const { activity, kpis } = payload;
|
|
const top = sortSignals(payload.signals)[0];
|
|
const lines = [
|
|
`**Bibdle stats — ${payload.asOfDate}**`,
|
|
`Players: ${activity.dau} today · ${activity.wau} this week · ${activity.mau} this month`,
|
|
`New ${activity.newPlayers} · returning ${activity.returningShare ?? '—'}% · net ${activity.netGrowth >= 0 ? '+' : ''}${activity.netGrowth}`,
|
|
`D1 return ${kpis.d1Return ?? '—'}% · 7-day ${kpis.return7 ?? '—'}%`,
|
|
top ? `Top signal: ${top.title} — ${top.detail}` : 'No signals.',
|
|
`https://bibdle.com/admin/stats?date=${payload.asOfDate}`
|
|
];
|
|
return lines.join('\n');
|
|
}
|
|
|
|
/**
|
|
* POST the digest to the admin Discord webhook. No-op (with a log) when the
|
|
* webhook is unset; never throws so a digest failure can't fail the build.
|
|
*/
|
|
export async function sendDigest(
|
|
payload: SnapshotPayload,
|
|
webhookUrl: string | undefined = Bun.env.DISCORD_ADMIN_WEBHOOK
|
|
): Promise<boolean> {
|
|
const message = composeDigest(payload);
|
|
if (!webhookUrl) {
|
|
console.log('[stats] DISCORD_ADMIN_WEBHOOK not set; digest skipped:\n' + message);
|
|
return false;
|
|
}
|
|
try {
|
|
const res = await fetch(webhookUrl, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ content: message })
|
|
});
|
|
if (!res.ok) console.error('[stats] digest webhook responded', res.status);
|
|
return res.ok;
|
|
} catch (err) {
|
|
console.error('[stats] digest failed', err);
|
|
return false;
|
|
}
|
|
} |