Skip to content

Commit 1355f24

Browse files
committed
feat: add "last 1000 sessions" label to Platform Stats header
1 parent dba49f5 commit 1355f24

5 files changed

Lines changed: 40 additions & 60 deletions

File tree

changelog.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Changed
1010

11-
- Platform Stats now loads ALL session data instead of last 1000 sessions
12-
- Changed publicPlatformStats query to use async iteration (streams data without memory limits)
13-
- Added total sessions and total tokens display in Platform Stats header
14-
- Same pattern used by publicMessageCount and publicMessageGrowth queries
11+
- Platform Stats header now displays "last 1000 sessions" label for clarity
12+
- Users know the leaderboard data is based on recent activity, not all-time totals
1513

1614
### Fixed
1715

convex/analytics.ts

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
700699
export 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

Comments
 (0)