Files
buildpath/frontend/server/utils/mongo.ts
Valentin Haudiquet dae65c8fa2
All checks were successful
pipeline / lint-and-format (push) Successful in 4m44s
pipeline / build-and-push-images (push) Successful in 4m7s
Allow collecting data from EUNE, NA, KR on top of EUW
- match_collector: query API and build collections for each platform
- match_collector: aggregate champion stats of each platform in one collection with platform annotations
- frontend: replace stats to count matches in platform-specific collections
- frontend: replace "EUW Challengers" with all supported platforms
- dev: adapted scripts to count match in platforms
2026-04-17 16:25:19 +02:00

51 lines
1.7 KiB
TypeScript

import { MongoClient } from 'mongodb'
// Available platforms for region-specific match data
const PLATFORMS = ['EUW1', 'EUN1', 'NA1', 'KR'] as const
type Platform = (typeof PLATFORMS)[number]
async function connectToDatabase() {
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
let uri = `mongodb://${process.env.MONGO_USER}:${process.env.MONGO_PASS}@${process.env.MONGO_HOST}`
if (
process.env.MONGO_URI != undefined &&
process.env.MONGO_URI != null &&
process.env.MONGO_URI != ''
) {
uri = process.env.MONGO_URI
}
const client = new MongoClient(uri)
await client.connect()
return client
}
async function fetchLatestPatch(client: MongoClient) {
const database = client.db('patches')
const patches = database.collection('patches')
const latestPatch = await patches.find().limit(1).sort({ date: -1 }).next()
return latestPatch!.patch as string
}
/**
* Get available platforms for a given patch by checking which match collections exist
* Note: Match collections are platform-specific (e.g., "15.1_EUW1")
* Champion collections are aggregated across all platforms (e.g., "15.1")
*/
async function getAvailablePlatforms(client: MongoClient, patch: string): Promise<Platform[]> {
const matchesDb = client.db('matches')
const collections = await matchesDb.listCollections().toArray()
const collectionNames = collections.map(c => c.name)
const availablePlatforms: Platform[] = []
for (const platform of PLATFORMS) {
if (collectionNames.includes(`${patch}_${platform}`)) {
availablePlatforms.push(platform)
}
}
return availablePlatforms
}
export { connectToDatabase, fetchLatestPatch, getAvailablePlatforms, PLATFORMS }
export type { Platform }