"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")); const scheduler_1 = require("firebase-functions/v2/scheduler"); const log_1 = require("../log"); 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 }; } exports.aggregateOutcomeStats = (0, scheduler_1.onSchedule)({ schedule: 'every 24 hours', memory: '512MiB', timeoutSeconds: 300 }, async () => { const db = admin.firestore(); const couples = []; const couplesSnap = await db.collection('couples').get(); for (const couple of couplesSnap.docs) { const outcomesSnap = await couple.ref.collection('outcomes').get(); couples.push(extractCoupleOutcome(outcomesSnap.docs.map((d) => ({ id: d.id, data: d.data() })))); } 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() })); log_1.logger.log(`[aggregateOutcomeStats] ${couples.length} couples → ` + exports.FOLLOWUP_DAYS.map((d) => `${d}:${stats.windows[d].suppressed ? 'suppressed' : stats.windows[d].feltCloserPct + '%'}`).join(' ')); }); //# sourceMappingURL=aggregateOutcomes.js.map