The 1st decentralized social network for sharing when you're on the toilet. Post a "flush" today! Powered by the AT Protocol.

leaderboard fixes

+29 -6
+29 -6
src/app/api/bluesky/stats/route.ts
··· 318 318 } 319 319 320 320 // 3. Get top flushers (leaderboard) - excluding test accounts 321 - const { data: leaderboardData, error: leaderboardError } = await supabase 322 - .from('flushing_records') 323 - .select('did, handle') 324 - .order('created_at', { ascending: false }); 321 + // Note: We need ALL records to get accurate counts, not just recent ones 322 + console.log('Fetching ALL flushing records for accurate leaderboard counts...'); 325 323 326 - if (leaderboardError) { 327 - throw new Error(`Failed to get leaderboard data: ${leaderboardError.message}`); 324 + let allLeaderboardData: any[] = []; 325 + let from = 0; 326 + const pageSize = 1000; // Supabase's default limit 327 + let hasMore = true; 328 + 329 + while (hasMore) { 330 + console.log(`Fetching leaderboard page: ${from} to ${from + pageSize - 1}`); 331 + 332 + const { data: pageData, error: pageError } = await supabase 333 + .from('flushing_records') 334 + .select('did, handle') 335 + .range(from, from + pageSize - 1); 336 + 337 + if (pageError) { 338 + throw new Error(`Failed to get leaderboard data: ${pageError.message}`); 339 + } 340 + 341 + if (!pageData || pageData.length === 0) { 342 + hasMore = false; 343 + } else { 344 + allLeaderboardData = [...allLeaderboardData, ...pageData]; 345 + hasMore = pageData.length === pageSize; // If we got a full page, there might be more 346 + from += pageSize; 347 + } 328 348 } 349 + 350 + console.log(`Total leaderboard records fetched: ${allLeaderboardData.length}`); 351 + const leaderboardData = allLeaderboardData; 329 352 330 353 // Count flushes by DID from Supabase (for ranking only) 331 354 const didCounts = new Map<string, number>();