2026-07-06 21:10:02 -05:00
|
|
|
"use strict";
|
|
|
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
|
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
|
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
|
|
|
}
|
|
|
|
|
Object.defineProperty(o, k2, desc);
|
|
|
|
|
}) : (function(o, m, k, k2) {
|
|
|
|
|
if (k2 === undefined) k2 = k;
|
|
|
|
|
o[k2] = m[k];
|
|
|
|
|
}));
|
|
|
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
|
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
|
|
|
}) : function(o, v) {
|
|
|
|
|
o["default"] = v;
|
|
|
|
|
});
|
|
|
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
|
|
|
var ownKeys = function(o) {
|
|
|
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
|
|
|
var ar = [];
|
|
|
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
|
|
|
return ar;
|
|
|
|
|
};
|
|
|
|
|
return ownKeys(o);
|
|
|
|
|
};
|
|
|
|
|
return function (mod) {
|
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
|
var result = {};
|
|
|
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
|
|
|
__setModuleDefault(result, mod);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
})();
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
|
exports.aggregateOutcomeStats = exports.MIN_COHORT = exports.FOLLOWUP_DAYS = exports.SCORE_KEYS = void 0;
|
|
|
|
|
exports.aggregate = aggregate;
|
|
|
|
|
exports.extractCoupleOutcome = extractCoupleOutcome;
|
|
|
|
|
const admin = __importStar(require("firebase-admin"));
|
refactor(functions): B2 migrate 8 scheduled functions to v2 + harden
Migrate all scheduled jobs off functions.pubsub.schedule().onRun() to
firebase-functions/v2/scheduler onSchedule({ schedule, timeZone, ...opts }, handler):
sendChallengeDayReminders, unlockDueMemoryCapsules, sendDailyQuestionProactiveReminder,
sendStreakReminder, sendReengagementReminder, assignDailyQuestion (scheduled export),
aggregateOutcomeStats, scheduledOutcomesReminder.
Hardening folded in:
- Fan-out isolation: Promise.all → Promise.allSettled in dailyQuestionReminder (outer+inner),
reengagement, gameRetention (both jobs), scheduledOutcomesReminder — one bad couple can no
longer abort a whole run. streakReminder / assignDailyQuestion already isolated.
- Resource options: assignDailyQuestion + aggregateOutcomeStats memory 512MiB + timeout 300s
(they iterate all couples); the four fan-out reminders get timeout 180s.
- Adopt shared sendPushToUser()/logger everywhere; remove five copied getUserTokens() and the
copied send/prune blocks (no plaintext token logging remains here).
- Consolidate duplicated date/time helpers into notifications/time.ts (chicagoDateKey, toMillis),
replacing streakReminder's + scheduledOutcomesReminder's per-file copies.
assignDailyQuestion.ts callable export stays v1 for now (migrates in B3); its tested CST helpers
are untouched. Scanner pagination for assignDailyQuestion/aggregateOutcomes is deferred to B6.
Build clean; 70 tests green (tested pure helpers preserved). dist rebuilt. Still on v5.1.1.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:50:59 -05:00
|
|
|
const scheduler_1 = require("firebase-functions/v2/scheduler");
|
|
|
|
|
const log_1 = require("../log");
|
2026-07-06 21:10:02 -05:00
|
|
|
exports.SCORE_KEYS = ['connection', 'communication', 'intimacy', 'happiness'];
|
|
|
|
|
exports.FOLLOWUP_DAYS = ['day_30', 'day_60', 'day_90'];
|
|
|
|
|
exports.MIN_COHORT = 50;
|
|
|
|
|
function pct(n, total) {
|
|
|
|
|
return total === 0 ? 0 : Math.round((n / total) * 100);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Pure aggregation — no I/O, unit-tested. A window below [minCohort] couples is suppressed.
|
|
|
|
|
*/
|
|
|
|
|
function aggregate(couples, minCohort = exports.MIN_COHORT) {
|
|
|
|
|
const windows = {};
|
|
|
|
|
for (const day of exports.FOLLOWUP_DAYS) {
|
|
|
|
|
const eligible = couples.filter((c) => c.deltas[day] != null);
|
|
|
|
|
const cohort = eligible.length;
|
|
|
|
|
if (cohort < minCohort) {
|
|
|
|
|
windows[day] = { cohort, suppressed: true };
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const feltCloser = eligible.filter((c) => { var _a, _b; return ((_b = (_a = c.deltas[day]) === null || _a === void 0 ? void 0 : _a.connection) !== null && _b !== void 0 ? _b : 0) > 0; }).length;
|
|
|
|
|
const byMetric = {};
|
|
|
|
|
for (const metric of exports.SCORE_KEYS) {
|
|
|
|
|
const improved = eligible.filter((c) => { var _a, _b; return ((_b = (_a = c.deltas[day]) === null || _a === void 0 ? void 0 : _a[metric]) !== null && _b !== void 0 ? _b : 0) > 0; }).length;
|
|
|
|
|
byMetric[metric] = pct(improved, cohort);
|
|
|
|
|
}
|
|
|
|
|
windows[day] = {
|
|
|
|
|
cohort,
|
|
|
|
|
suppressed: false,
|
|
|
|
|
feltCloserPct: pct(feltCloser, cohort),
|
|
|
|
|
improvedPctByMetric: byMetric,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return { minCohort, windows };
|
|
|
|
|
}
|
|
|
|
|
/** Extracts the follow-up deltas from a couple's outcomes subcollection docs. */
|
|
|
|
|
function extractCoupleOutcome(outcomeDocs) {
|
|
|
|
|
const deltas = {};
|
|
|
|
|
for (const doc of outcomeDocs) {
|
|
|
|
|
if (exports.FOLLOWUP_DAYS.includes(doc.id)) {
|
|
|
|
|
const delta = doc.data.delta;
|
|
|
|
|
if (delta && typeof delta === 'object')
|
|
|
|
|
deltas[doc.id] = delta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { deltas };
|
|
|
|
|
}
|
refactor(functions): B2 migrate 8 scheduled functions to v2 + harden
Migrate all scheduled jobs off functions.pubsub.schedule().onRun() to
firebase-functions/v2/scheduler onSchedule({ schedule, timeZone, ...opts }, handler):
sendChallengeDayReminders, unlockDueMemoryCapsules, sendDailyQuestionProactiveReminder,
sendStreakReminder, sendReengagementReminder, assignDailyQuestion (scheduled export),
aggregateOutcomeStats, scheduledOutcomesReminder.
Hardening folded in:
- Fan-out isolation: Promise.all → Promise.allSettled in dailyQuestionReminder (outer+inner),
reengagement, gameRetention (both jobs), scheduledOutcomesReminder — one bad couple can no
longer abort a whole run. streakReminder / assignDailyQuestion already isolated.
- Resource options: assignDailyQuestion + aggregateOutcomeStats memory 512MiB + timeout 300s
(they iterate all couples); the four fan-out reminders get timeout 180s.
- Adopt shared sendPushToUser()/logger everywhere; remove five copied getUserTokens() and the
copied send/prune blocks (no plaintext token logging remains here).
- Consolidate duplicated date/time helpers into notifications/time.ts (chicagoDateKey, toMillis),
replacing streakReminder's + scheduledOutcomesReminder's per-file copies.
assignDailyQuestion.ts callable export stays v1 for now (migrates in B3); its tested CST helpers
are untouched. Scanner pagination for assignDailyQuestion/aggregateOutcomes is deferred to B6.
Build clean; 70 tests green (tested pure helpers preserved). dist rebuilt. Still on v5.1.1.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:50:59 -05:00
|
|
|
exports.aggregateOutcomeStats = (0, scheduler_1.onSchedule)({ schedule: 'every 24 hours', memory: '512MiB', timeoutSeconds: 300 }, async () => {
|
2026-07-06 21:10:02 -05:00
|
|
|
const db = admin.firestore();
|
2026-07-08 00:05:32 -05:00
|
|
|
// Paginate the couple scan (was an unbounded couples.get()) and read each page's outcomes in
|
|
|
|
|
// parallel (was one serial outcomes.get() per couple → O(couples) round-trips). couples.length
|
|
|
|
|
// still counts every couple, so the aggregate + totalCouples are unchanged.
|
2026-07-06 21:10:02 -05:00
|
|
|
const couples = [];
|
2026-07-08 00:05:32 -05:00
|
|
|
const PAGE_SIZE = 200;
|
|
|
|
|
let lastDoc;
|
|
|
|
|
for (;;) {
|
|
|
|
|
let query = db.collection('couples').orderBy('__name__').limit(PAGE_SIZE);
|
|
|
|
|
if (lastDoc)
|
|
|
|
|
query = query.startAfter(lastDoc);
|
|
|
|
|
const page = await query.get();
|
|
|
|
|
if (page.empty)
|
|
|
|
|
break;
|
|
|
|
|
const pageOutcomes = await Promise.all(page.docs.map(async (couple) => {
|
|
|
|
|
const outcomesSnap = await couple.ref.collection('outcomes').get();
|
|
|
|
|
return extractCoupleOutcome(outcomesSnap.docs.map((d) => ({ id: d.id, data: d.data() })));
|
|
|
|
|
}));
|
|
|
|
|
couples.push(...pageOutcomes);
|
|
|
|
|
lastDoc = page.docs[page.docs.length - 1];
|
|
|
|
|
if (page.size < PAGE_SIZE)
|
|
|
|
|
break;
|
2026-07-06 21:10:02 -05:00
|
|
|
}
|
|
|
|
|
const stats = aggregate(couples);
|
|
|
|
|
await db.collection('aggregate_stats').doc('outcomes').set(Object.assign(Object.assign({}, stats), { totalCouples: couples.length, generatedAt: admin.firestore.Timestamp.now() }));
|
refactor(functions): B2 migrate 8 scheduled functions to v2 + harden
Migrate all scheduled jobs off functions.pubsub.schedule().onRun() to
firebase-functions/v2/scheduler onSchedule({ schedule, timeZone, ...opts }, handler):
sendChallengeDayReminders, unlockDueMemoryCapsules, sendDailyQuestionProactiveReminder,
sendStreakReminder, sendReengagementReminder, assignDailyQuestion (scheduled export),
aggregateOutcomeStats, scheduledOutcomesReminder.
Hardening folded in:
- Fan-out isolation: Promise.all → Promise.allSettled in dailyQuestionReminder (outer+inner),
reengagement, gameRetention (both jobs), scheduledOutcomesReminder — one bad couple can no
longer abort a whole run. streakReminder / assignDailyQuestion already isolated.
- Resource options: assignDailyQuestion + aggregateOutcomeStats memory 512MiB + timeout 300s
(they iterate all couples); the four fan-out reminders get timeout 180s.
- Adopt shared sendPushToUser()/logger everywhere; remove five copied getUserTokens() and the
copied send/prune blocks (no plaintext token logging remains here).
- Consolidate duplicated date/time helpers into notifications/time.ts (chicagoDateKey, toMillis),
replacing streakReminder's + scheduledOutcomesReminder's per-file copies.
assignDailyQuestion.ts callable export stays v1 for now (migrates in B3); its tested CST helpers
are untouched. Scanner pagination for assignDailyQuestion/aggregateOutcomes is deferred to B6.
Build clean; 70 tests green (tested pure helpers preserved). dist rebuilt. Still on v5.1.1.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-07 23:50:59 -05:00
|
|
|
log_1.logger.log(`[aggregateOutcomeStats] ${couples.length} couples → ` +
|
2026-07-06 21:10:02 -05:00
|
|
|
exports.FOLLOWUP_DAYS.map((d) => `${d}:${stats.windows[d].suppressed ? 'suppressed' : stats.windows[d].feltCloserPct + '%'}`).join(' '));
|
|
|
|
|
});
|
|
|
|
|
//# sourceMappingURL=aggregateOutcomes.js.map
|