From 59840ffe34313085b2106ef8475750f497dc092f Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Wed, 12 Aug 2026 00:39:09 +0200 Subject: [PATCH] fix/match_collector: 3 bugs in cleanup old patches in db - Regex only matched 2-part patch versions - champions database never cleaned up - Patch comparison didn't normalize --- match_collector/src/index.ts | 71 ++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/match_collector/src/index.ts b/match_collector/src/index.ts index c15856b..28d08cd 100644 --- a/match_collector/src/index.ts +++ b/match_collector/src/index.ts @@ -33,20 +33,41 @@ async function getLatestPatchFromCollections(client: MongoClient): Promise "16.1", "16.14" -> "16.14" */ -async function getAllPatchesFromCollections(client: MongoClient): Promise { - const matchesDb = client.db('matches') - const collections = await matchesDb.listCollections().toArray() - const collectionNames = collections.map(c => c.name) +function normalizePatch(patch: string): string { + const parts = patch.split('.') + if (parts.length >= 2) { + return `${parts[0]}.${parts[1]}` + } + return patch +} - // Extract unique patch versions from collection names +/** + * Check if a string looks like a patch version (e.g. "16.1", "16.1.1", "15.24.1") + */ +function isPatchVersion(s: string): boolean { + return /^\d+(\.\d+)+$/.test(s) +} + +async function getAllPatchesFromCollections(client: MongoClient): Promise { const patches = new Set() - for (const name of collectionNames) { - // Collection names are either "patch_platform" or just "patch" - const patch = name.split('_')[0] - if (patch && /^\d+\.\d+$/.test(patch)) { - patches.add(patch) + + // Check both matches and champions databases for patches + for (const dbName of ['matches', 'champions']) { + const db = client.db(dbName) + const collections = await db.listCollections().toArray() + const collectionNames = collections.map(c => c.name) + + for (const name of collectionNames) { + // Collection names are either "patch_platform" or just "patch" + const patch = name.split('_')[0] + // Match any version format (e.g., "16.1", "16.1.1", "15.24.1") + if (patch && isPatchVersion(patch)) { + // Normalize to 2-part version (major.minor) for grouping + patches.add(normalizePatch(patch)) + } } } @@ -66,13 +87,12 @@ async function getAllPatchesFromCollections(client: MongoClient): Promise { - const matchesDb = client.db('matches') const allPatches = await getAllPatchesFromCollections(client) if (allPatches.length <= keepPatches) { @@ -85,17 +105,22 @@ async function cleanupOldPatches(client: MongoClient, keepPatches: number): Prom 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) - + // Clean up collections in both matches and champions databases 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++ + for (const dbName of ['matches', 'champions']) { + const db = client.db(dbName) + const collections = await db.listCollections().toArray() + const collectionNames = collections.map(c => c.name) + + for (const collectionName of collectionNames) { + const patch = collectionName.split('_')[0] + // Normalize the patch to 2-part version for comparison + // (collection may use 3-part like "16.1.1_EUW1" while patchesToRemove has "16.1") + if (isPatchVersion(patch) && patchesToRemove.includes(normalizePatch(patch))) { + console.log(`Cleanup: Dropping ${dbName}.${collectionName}...`) + await db.dropCollection(collectionName) + droppedCount++ + } } }