···140140 new Date(entry.created_at) >= thirtyDaysAgo
141141 );
142142143143- // Get unique DIDs from recent records
143143+ // Get unique DIDs from recent records - excluding test accounts and plumber
144144 const recentUniqueDids = new Set<string>();
145145 recentRecords?.forEach(entry => {
146146- if (entry.did) {
146146+ // Only count if not an excluded account
147147+ if (entry.did &&
148148+ !excludedDids.includes(entry.did) &&
149149+ !(entry.handle && excludedHandles.includes(entry.handle))) {
147150 recentUniqueDids.add(entry.did);
148151 }
149152 });
150153151151- const monthlyActiveFlushers = recentUniqueDids.size;
154154+ let monthlyActiveFlushers = recentUniqueDids.size;
152155 console.log(`Monthly Active Flushers (last 30 days): ${monthlyActiveFlushers}`);
153156154157 // Calculate Daily Active Flushers (DAFs)
155158 // This is the average number of unique users who post per day over the last 30 days
156159 const dailyActiveUserCounts = new Map<string, Set<string>>();
157160158158- // Group users by day
161161+ // Group users by day - excluding test accounts and plumber
159162 recentRecords?.forEach(entry => {
160163 if (!entry.did) return; // Skip entries without a DID
164164+165165+ // Skip excluded accounts
166166+ if (excludedDids.includes(entry.did) ||
167167+ (entry.handle && excludedHandles.includes(entry.handle))) {
168168+ return;
169169+ }
161170162171 const date = new Date(entry.created_at);
163172 const dateKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
···179188 }
180189 console.log(`Daily Active Flushers (average over last 30 days): ${dailyActiveFlushers}`);
181190191191+ // Sanity check: daily active flushers (average) should not exceed monthly active flushers
192192+ if (dailyActiveFlushers > monthlyActiveFlushers) {
193193+ console.error(`Warning: Daily active flushers avg (${dailyActiveFlushers}) exceeds monthly active flushers (${monthlyActiveFlushers}). This should not happen.`);
194194+ // Cap daily active flushers at the monthly active flushers value
195195+ dailyActiveFlushers = parseFloat(Math.min(monthlyActiveFlushers, dailyActiveFlushers).toFixed(1));
196196+ console.log(`Correcting daily active flushers to ${dailyActiveFlushers}`);
197197+ }
198198+182199 // 3. Get top flushers (leaderboard) - excluding test accounts
183200 const { data: leaderboardData, error: leaderboardError } = await supabase
184201 .from('flushing_records')
···235252 // Calculate total unique flushers (count of unique DIDs)
236253 const totalFlushers = didCounts.size;
237254 console.log(`Total unique flushers: ${totalFlushers}`);
255255+256256+ // Sanity check: make sure monthly active flushers is not greater than total flushers
257257+ if (monthlyActiveFlushers > totalFlushers) {
258258+ console.error(`Warning: Monthly active flushers (${monthlyActiveFlushers}) exceeds total flushers (${totalFlushers}). This should never happen.`);
259259+ // If we somehow still have an inconsistency, cap the monthly active flushers
260260+ const correctedMAF = Math.min(totalFlushers, monthlyActiveFlushers);
261261+ console.log(`Correcting monthly active flushers from ${monthlyActiveFlushers} to ${correctedMAF}`);
262262+ monthlyActiveFlushers = correctedMAF;
263263+ }
238264239265 // Return the data
240266 return NextResponse.json({