feat/match_collector: cleanup older patches data automatically
This commit is contained in:
@@ -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<string | null> {
|
||||
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<string[]> {
|
||||
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<strin
|
||||
}
|
||||
|
||||
if (patches.size === 0) {
|
||||
return null
|
||||
return []
|
||||
}
|
||||
|
||||
// Sort patches and return the latest (highest version number)
|
||||
// Sort patches from latest to oldest (highest version number first)
|
||||
const sortedPatches = Array.from(patches).sort((a, b) => {
|
||||
const [aMajor, aMinor] = a.split('.').map(Number)
|
||||
const [bMajor, bMinor] = b.split('.').map(Number)
|
||||
@@ -53,7 +61,44 @@ async function getLatestPatchFromCollections(client: MongoClient): Promise<strin
|
||||
return bMinor - aMinor
|
||||
})
|
||||
|
||||
return sortedPatches[0]
|
||||
return sortedPatches
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up match collections that are more than 2 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) {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user