added MaU section with projection

This commit is contained in:
George Powell
2026-03-19 00:39:54 -04:00
parent bdc08bc58e
commit b6b41b6ba9
2 changed files with 235 additions and 10 deletions

View File

@@ -56,6 +56,12 @@ export const load: PageServerLoad = async () => {
.from(dailyCompletions)
.where(gte(dailyCompletions.date, sevenDaysAgo));
const thirtyDaysAgo = estDateStr(30);
const [{ monthlyPlayers }] = await db
.select({ monthlyPlayers: countDistinct(dailyCompletions.anonymousId) })
.from(dailyCompletions)
.where(gte(dailyCompletions.date, thirtyDaysAgo));
const todayPlayers = await db
.selectDistinct({ id: dailyCompletions.anonymousId })
.from(dailyCompletions)
@@ -347,6 +353,96 @@ export const load: PageServerLoad = async () => {
const avgWau = Math.round(wauWeeks.reduce((sum, w) => sum + w.wau, 0) / wauWeeks.length);
// ── Monthly Active Users history (6 months) ───────────────────────────────
const sixMonthsAgo = estDateStr(185);
const mauCompletions = await db
.select({ anonymousId: dailyCompletions.anonymousId, date: dailyCompletions.date })
.from(dailyCompletions)
.where(gte(dailyCompletions.date, sixMonthsAgo));
// Rolling 30-day windows
const mauMonths: { monthStart: string; monthEnd: string; mau: number; changePct: number | null }[] = [];
for (let m = 0; m < 6; m++) {
const monthEnd = estDateStr(m * 30);
const monthStart = estDateStr(m * 30 + 29);
const users = new Set<string>();
for (const row of mauCompletions) {
if (row.date >= monthStart && row.date <= monthEnd) {
users.add(row.anonymousId);
}
}
mauMonths.push({ monthStart, monthEnd, mau: users.size, changePct: null });
}
for (let i = 0; i < mauMonths.length - 1; i++) {
const prev = mauMonths[i + 1].mau;
if (prev > 0) {
mauMonths[i].changePct = Math.round(((mauMonths[i].mau - prev) / prev) * 1000) / 10;
}
}
// Calendar month windows
const MONTH_NAMES = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const [todayYear, todayMonth, todayDay] = todayEst.split('-').map(Number);
const calendarMauMonths: {
label: string;
monthStart: string;
monthEnd: string;
mau: number;
daysElapsed: number;
daysInMonth: number;
projectedMau: number | null;
changePct: number | null;
isCurrentMonth: boolean;
}[] = [];
for (let i = 0; i < 6; i++) {
let mo = todayMonth - i;
let yr = todayYear;
if (mo <= 0) { mo += 12; yr--; }
// new Date(yr, mo, 0) gives last day of month mo (1-indexed) in local time
const daysInMonth = new Date(yr, mo, 0).getDate();
const monthStart = `${yr}-${String(mo).padStart(2, '0')}-01`;
const monthEnd = `${yr}-${String(mo).padStart(2, '0')}-${String(daysInMonth).padStart(2, '0')}`;
const isCurrentMonth = i === 0;
const daysElapsed = isCurrentMonth ? todayDay : daysInMonth;
const queryEnd = isCurrentMonth ? todayEst : monthEnd;
const users = new Set<string>();
for (const row of mauCompletions) {
if (row.date >= monthStart && row.date <= queryEnd) {
users.add(row.anonymousId);
}
}
const projectedMau = isCurrentMonth && daysElapsed > 0
? Math.round(users.size * (daysInMonth / daysElapsed))
: null;
calendarMauMonths.push({
label: `${MONTH_NAMES[mo - 1]} ${yr}`,
monthStart,
monthEnd,
mau: users.size,
daysElapsed,
daysInMonth,
projectedMau,
changePct: null,
isCurrentMonth
});
}
for (let i = 0; i < calendarMauMonths.length - 1; i++) {
const curr = calendarMauMonths[i];
const prev = calendarMauMonths[i + 1];
if (prev.mau > 0) {
const compareVal = curr.projectedMau ?? curr.mau;
curr.changePct = Math.round(((compareVal - prev.mau) / prev.mau) * 1000) / 10;
}
}
return {
todayEst,
sessionDepthCards,
@@ -358,7 +454,7 @@ export const load: PageServerLoad = async () => {
activeStreaks,
avgGuessesToday,
registeredUsers,
avgCompletionsPerPlayer
monthlyPlayers
},
growth: {
completionsVelocity,
@@ -381,6 +477,8 @@ export const load: PageServerLoad = async () => {
change: returnRateChange
},
wauWeeks,
avgWau
avgWau,
mauMonths,
calendarMauMonths
};
};