mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-10-22 13:54:10 -04:00
first pass analytics redo
This commit is contained in:
1 parent
b656fd04c0
commit
45ffa39f2a
69 files changed
+6400
-1138
No files matched your search
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user