@@ -696,7 +696,6 @@ export const publicMessageGrowth = query({
696696
697697// Public platform-wide stats for homepage leaderboard (no auth required)
698698// Returns top models and top CLI sources sorted by usage
699- // Uses async iteration to stream ALL sessions without hitting memory limits
700699export const publicPlatformStats = query ( {
701700 args : { } ,
702701 returns : v . object ( {
@@ -714,27 +713,28 @@ export const publicPlatformStats = query({
714713 totalTokens : v . number ( ) ,
715714 } ) ,
716715 ) ,
717- totalSessions : v . number ( ) ,
718- totalTokens : v . number ( ) ,
719716 } ) ,
720717 handler : async ( ctx ) => {
721- // Aggregate by model (streams all sessions without loading into memory)
718+ // Fetch recent sessions for platform-wide stats (limit to 1000 to avoid timeout)
719+ const sessions = await ctx . db . query ( "sessions" ) . order ( "desc" ) . take ( 1000 ) ;
720+
721+ if ( sessions . length === 0 ) {
722+ return {
723+ topModels : [ ] ,
724+ topSources : [ ] ,
725+ } ;
726+ }
727+
728+ // Aggregate by model
722729 const modelMap : Record < string , { totalTokens : number ; sessions : number } > =
723730 { } ;
724731 // Aggregate by source (CLI tool)
725732 const sourceMap : Record < string , { sessions : number ; totalTokens : number } > =
726733 { } ;
727734
728- let totalSessions = 0 ;
729- let totalTokens = 0 ;
730-
731- // Stream through ALL sessions using async iteration (avoids 16MB read limit)
732- for await ( const s of ctx . db . query ( "sessions" ) ) {
733- totalSessions += 1 ;
734-
735+ for ( const s of sessions ) {
735736 // Safe token value (handle null/undefined)
736737 const tokens = s . totalTokens ?? 0 ;
737- totalTokens += tokens ;
738738
739739 // Model aggregation
740740 const model = s . model || "unknown" ;
@@ -753,16 +753,6 @@ export const publicPlatformStats = query({
753753 sourceMap [ source ] . totalTokens += tokens ;
754754 }
755755
756- // Return empty arrays if no data
757- if ( totalSessions === 0 ) {
758- return {
759- topModels : [ ] ,
760- topSources : [ ] ,
761- totalSessions : 0 ,
762- totalTokens : 0 ,
763- } ;
764- }
765-
766756 // Sort models by total tokens and take top 5
767757 const topModels = Object . entries ( modelMap )
768758 . map ( ( [ model , stats ] ) => ( {
@@ -786,8 +776,6 @@ export const publicPlatformStats = query({
786776 return {
787777 topModels,
788778 topSources,
789- totalSessions,
790- totalTokens,
791779 } ;
792780 } ,
793781} ) ;
0 commit comments