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[]> {
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<string[]> {
const patches = new Set<string>()
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<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.
* @param client MongoDB client
* @param keepPatches Number of recent patches to keep (default: 2)
*/
async function cleanupOldPatches(client: MongoClient, keepPatches: number): Promise<void> {
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++
}
}
}