diff --git a/match_collector/src/index.ts b/match_collector/src/index.ts index e610404..a4e2d4d 100644 --- a/match_collector/src/index.ts +++ b/match_collector/src/index.ts @@ -27,6 +27,14 @@ function extractPatchFromGameVersion(gameVersion: string): string { * Collections are named like "15.1_EUW1", "15.2_NA1", etc. */ async function getLatestPatchFromCollections(client: MongoClient): Promise { + const patches = await getAllPatchesFromCollections(client) + return patches.length > 0 ? patches[0] : null +} + +/** + * Get all patches from existing match collections, sorted from latest to oldest. + */ +async function getAllPatchesFromCollections(client: MongoClient): Promise { const matchesDb = client.db('matches') const collections = await matchesDb.listCollections().toArray() const collectionNames = collections.map(c => c.name) @@ -42,10 +50,10 @@ async function getLatestPatchFromCollections(client: MongoClient): Promise { const [aMajor, aMinor] = a.split('.').map(Number) const [bMajor, bMinor] = b.split('.').map(Number) @@ -53,7 +61,44 @@ async function getLatestPatchFromCollections(client: MongoClient): Promise { + const matchesDb = client.db('matches') + const allPatches = await getAllPatchesFromCollections(client) + + if (allPatches.length <= keepPatches) { + console.log(`Cleanup: Only ${allPatches.length} patch(es) found, nothing to clean up.`) + return + } + + // Get patches to remove (everything after the first `keepPatches` patches) + const patchesToRemove = allPatches.slice(keepPatches) + console.log(`Cleanup: Found ${allPatches.length} patches, keeping ${keepPatches} most recent.`) + console.log(`Cleanup: Patches to remove: ${patchesToRemove.join(', ')}`) + + // Get all collections to find ones that match patches to remove + const collections = await matchesDb.listCollections().toArray() + const collectionNames = collections.map(c => c.name) + + let droppedCount = 0 + for (const collectionName of collectionNames) { + const patch = collectionName.split('_')[0] + if (patchesToRemove.includes(patch)) { + console.log(`Cleanup: Dropping collection '${collectionName}'...`) + await matchesDb.dropCollection(collectionName) + droppedCount++ + } + } + + console.log(`Cleanup: Dropped ${droppedCount} collection(s).`) } async function main() { @@ -118,6 +163,10 @@ async function main() { } } + // Clean up old patches (keep only 2 most recent patches) + console.log('\n=== Cleaning up old patches ===') + await cleanupOldPatches(client, 2) + // Get the latest patch from collections and generate stats for it const latestPatch = await getLatestPatchFromCollections(client) if (latestPatch) {