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
This commit is contained in:
@@ -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"
|
||||||
*/
|
*/
|
||||||
|
function normalizePatch(patch: string): string {
|
||||||
|
const parts = patch.split('.')
|
||||||
|
if (parts.length >= 2) {
|
||||||
|
return `${parts[0]}.${parts[1]}`
|
||||||
|
}
|
||||||
|
return patch
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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[]> {
|
async function getAllPatchesFromCollections(client: MongoClient): Promise<string[]> {
|
||||||
const matchesDb = client.db('matches')
|
const patches = new Set<string>()
|
||||||
const collections = await matchesDb.listCollections().toArray()
|
|
||||||
|
// 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)
|
const collectionNames = collections.map(c => c.name)
|
||||||
|
|
||||||
// Extract unique patch versions from collection names
|
|
||||||
const patches = new Set<string>()
|
|
||||||
for (const name of collectionNames) {
|
for (const name of collectionNames) {
|
||||||
// Collection names are either "patch_platform" or just "patch"
|
// Collection names are either "patch_platform" or just "patch"
|
||||||
const patch = name.split('_')[0]
|
const patch = name.split('_')[0]
|
||||||
if (patch && /^\d+\.\d+$/.test(patch)) {
|
// Match any version format (e.g., "16.1", "16.1.1", "15.24.1")
|
||||||
patches.add(patch)
|
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,19 +105,24 @@ 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()
|
let droppedCount = 0
|
||||||
|
for (const dbName of ['matches', 'champions']) {
|
||||||
|
const db = client.db(dbName)
|
||||||
|
const collections = await db.listCollections().toArray()
|
||||||
const collectionNames = collections.map(c => c.name)
|
const collectionNames = collections.map(c => c.name)
|
||||||
|
|
||||||
let droppedCount = 0
|
|
||||||
for (const collectionName of collectionNames) {
|
for (const collectionName of collectionNames) {
|
||||||
const patch = collectionName.split('_')[0]
|
const patch = collectionName.split('_')[0]
|
||||||
if (patchesToRemove.includes(patch)) {
|
// Normalize the patch to 2-part version for comparison
|
||||||
console.log(`Cleanup: Dropping collection '${collectionName}'...`)
|
// (collection may use 3-part like "16.1.1_EUW1" while patchesToRemove has "16.1")
|
||||||
await matchesDb.dropCollection(collectionName)
|
if (isPatchVersion(patch) && patchesToRemove.includes(normalizePatch(patch))) {
|
||||||
|
console.log(`Cleanup: Dropping ${dbName}.${collectionName}...`)
|
||||||
|
await db.dropCollection(collectionName)
|
||||||
droppedCount++
|
droppedCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Delete stats for removed patches
|
// Delete stats for removed patches
|
||||||
await stats.deleteStats(client, patchesToRemove)
|
await stats.deleteStats(client, patchesToRemove)
|
||||||
|
|||||||
Reference in New Issue
Block a user