@@ -635,21 +635,21 @@ export const summaryStats = query({
635635} ) ;
636636
637637// TEMP: Public message count for milestone display on stats page
638- // Uses async iteration to avoid collecting all documents into memory
638+ // Sums messageCount from sessions table (fewer docs, avoids 16MB read limit)
639639export const publicMessageCount = query ( {
640640 args : { } ,
641641 returns : v . number ( ) ,
642642 handler : async ( ctx ) => {
643- let count = 0 ;
644- for await ( const _ of ctx . db . query ( "messages " ) ) {
645- count += 1 ;
643+ let total = 0 ;
644+ for await ( const session of ctx . db . query ( "sessions " ) ) {
645+ total += session . messageCount || 0 ;
646646 }
647- return count ;
647+ return total ;
648648 } ,
649649} ) ;
650650
651651// TEMP: Public message growth data for animated chart on stats page
652- // Uses pagination to avoid exceeding read limits
652+ // Groups by session createdAt date and sums messageCount (avoids 16MB read limit)
653653export const publicMessageGrowth = query ( {
654654 args : { } ,
655655 returns : v . array (
@@ -660,17 +660,16 @@ export const publicMessageGrowth = query({
660660 } )
661661 ) ,
662662 handler : async ( ctx ) => {
663- // Group messages by date using async iteration (no collect)
663+ // Group sessions by date and sum their messageCount
664664 const byDate : Record < string , number > = { } ;
665665
666- for await ( const msg of ctx . db . query ( "messages" ) ) {
667- // Safety check: skip messages with invalid createdAt
668- if ( ! msg . createdAt || typeof msg . createdAt !== "number" ) continue ;
666+ for await ( const session of ctx . db . query ( "sessions" ) ) {
667+ if ( ! session . createdAt || typeof session . createdAt !== "number" ) continue ;
669668 try {
670- const dateObj = new Date ( msg . createdAt ) ;
669+ const dateObj = new Date ( session . createdAt ) ;
671670 if ( isNaN ( dateObj . getTime ( ) ) ) continue ;
672671 const date = dateObj . toISOString ( ) . split ( "T" ) [ 0 ] ;
673- byDate [ date ] = ( byDate [ date ] || 0 ) + 1 ;
672+ byDate [ date ] = ( byDate [ date ] || 0 ) + ( session . messageCount || 0 ) ;
674673 } catch {
675674 continue ;
676675 }
0 commit comments