Skip to content

Commit 351f702

Browse files
committed
undo: fix stats page timeout: limit queries to 5000 sessions to prevent RESULT_CODE_HUNG
1 parent 3095907 commit 351f702

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

convex/analytics.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -635,23 +635,21 @@ export const summaryStats = query({
635635
});
636636

637637
// TEMP: Public message count for milestone display on stats page
638-
// Sums messageCount from sessions table (limit 5000 to prevent timeout)
638+
// Sums messageCount from sessions table (fewer docs, avoids 16MB read limit)
639639
export const publicMessageCount = query({
640640
args: {},
641641
returns: v.number(),
642642
handler: async (ctx) => {
643-
// Limit to 5000 sessions to prevent query timeout
644-
const sessions = await ctx.db.query("sessions").take(5000);
645643
let total = 0;
646-
for (const session of sessions) {
644+
for await (const session of ctx.db.query("sessions")) {
647645
total += session.messageCount || 0;
648646
}
649647
return total;
650648
},
651649
});
652650

653-
// TEMP: Public message growth data for chart on stats page
654-
// Groups by session createdAt date and sums messageCount (limit 5000 to prevent timeout)
651+
// TEMP: Public message growth data for animated chart on stats page
652+
// Groups by session createdAt date and sums messageCount (avoids 16MB read limit)
655653
export const publicMessageGrowth = query({
656654
args: {},
657655
returns: v.array(
@@ -662,13 +660,10 @@ export const publicMessageGrowth = query({
662660
})
663661
),
664662
handler: async (ctx) => {
665-
// Limit to 5000 sessions to prevent query timeout
666-
const sessions = await ctx.db.query("sessions").take(5000);
667-
668663
// Group sessions by date and sum their messageCount
669664
const byDate: Record<string, number> = {};
670665

671-
for (const session of sessions) {
666+
for await (const session of ctx.db.query("sessions")) {
672667
if (!session.createdAt || typeof session.createdAt !== "number") continue;
673668
try {
674669
const dateObj = new Date(session.createdAt);

0 commit comments

Comments
 (0)