fix/match_collector: 3 bugs in cleanup old patches in db
pipeline / lint-and-format (push) Successful in 4m4s
pipeline / build-and-push-images (push) Successful in 25s
pipeline / lint-and-publish-chart (push) Successful in 8s

- Regex only matched 2-part patch versions
- champions database never cleaned up
- Patch comparison didn't normalize
This commit is contained in:
2026-08-12 00:39:09 +02:00
parent c45e808c73
commit 59840ffe34
+48 -23
View File
@@ -33,20 +33,41 @@ async function getLatestPatchFromCollections(client: MongoClient): Promise<strin
} }
/** /**
* Get all patches from existing match collections, sorted from latest to oldest. * Normalize a patch version string to its major.minor form.
* E.g. "16.1.1" -> "16.1", "16.14" -> "16.14"
*/ */
async function getAllPatchesFromCollections(client: MongoClient): Promise<string[]> { function normalizePatch(patch: string): string {
const matchesDb = client.db('matches') const parts = patch.split('.')
const collections = await matchesDb.listCollections().toArray() if (parts.length >= 2) {
const collectionNames = collections.map(c => c.name) 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<string[]> {
const patches = new Set<string>() const patches = new Set<string>()
for (const name of collectionNames) {
// Collection names are either "patch_platform" or just "patch" // Check both matches and champions databases for patches
const patch = name.split('_')[0] for (const dbName of ['matches', 'champions']) {
if (patch && /^\d+\.\d+$/.test(patch)) { const db = client.db(dbName)
patches.add(patch) 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<string
} }
/** /**
* Clean up match collections that are more than 2 patches old. * Clean up match and champion collections that are more than `keepPatches` patches old.
* This helps reduce database storage by removing outdated match data. * This helps reduce database storage by removing outdated match data.
* @param client MongoDB client * @param client MongoDB client
* @param keepPatches Number of recent patches to keep (default: 2) * @param keepPatches Number of recent patches to keep (default: 2)
*/ */
async function cleanupOldPatches(client: MongoClient, keepPatches: number): Promise<void> { async function cleanupOldPatches(client: MongoClient, keepPatches: number): Promise<void> {
const matchesDb = client.db('matches')
const allPatches = await getAllPatchesFromCollections(client) const allPatches = await getAllPatchesFromCollections(client)
if (allPatches.length <= keepPatches) { 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: Found ${allPatches.length} patches, keeping ${keepPatches} most recent.`)
console.log(`Cleanup: Patches to remove: ${patchesToRemove.join(', ')}`) console.log(`Cleanup: Patches to remove: ${patchesToRemove.join(', ')}`)
// Get all collections to find ones that match patches to remove // Clean up collections in both matches and champions databases
const collections = await matchesDb.listCollections().toArray()
const collectionNames = collections.map(c => c.name)
let droppedCount = 0 let droppedCount = 0
for (const collectionName of collectionNames) { for (const dbName of ['matches', 'champions']) {
const patch = collectionName.split('_')[0] const db = client.db(dbName)
if (patchesToRemove.includes(patch)) { const collections = await db.listCollections().toArray()
console.log(`Cleanup: Dropping collection '${collectionName}'...`) const collectionNames = collections.map(c => c.name)
await matchesDb.dropCollection(collectionName)
droppedCount++ 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++
}
} }
} }