fix: pre-compute stats to make the API endpoint fast
All checks were successful
pipeline / lint-and-format (push) Successful in 4m22s
pipeline / build-and-push-images (push) Successful in 2m14s

This commit is contained in:
2026-05-14 00:02:23 +02:00
parent e76cb3ea8e
commit 0224b7812c
3 changed files with 181 additions and 2 deletions

View File

@@ -1,14 +1,29 @@
import type { MongoClient } from 'mongodb'
import { connectToDatabase, fetchLatestPatch, getAvailablePlatforms } from '../utils/mongo'
interface StatsDocument {
patch: string
total: number
platforms: Record<string, number>
updatedAt: Date
}
async function fetchGameCount(client: MongoClient, patch: string) {
const database = client.db('matches')
const statsCollection = database.collection<StatsDocument>('stats')
// Check for platform-specific collections
// Try to get stats from the pre-computed stats collection
const stats = await statsCollection.findOne({ patch })
if (stats) {
return { total: stats.total, platforms: stats.platforms }
}
// Fallback: compute stats from collections if stats document doesn't exist
// This handles the migration case where stats weren't pre-computed
const platforms = await getAvailablePlatforms(client, patch)
if (platforms.length > 0) {
// Sum counts from all platform-specific collections
let totalCount = 0
const platformCounts: Record<string, number> = {}