|
/* |
|
|
|
la funzioanlità ricerca nel server tutti i db like <db_like_name> e per ogni collecion presente nel singolo db |
|
esegue una eliminazione per spurgare i log. |
|
|
|
per eseguire lo script |
|
./mongo --host <indirizzo_host> --port <porta> PurgeLogCollection.js --quiet --eval "var deleteData=false;var beforePurgeDay=200;" |
|
|
|
parametri in eval |
|
deleteDate = true|false (se false visualizza solo questo che dovrà fare senza eliminare default false) |
|
beforePurgeDay = indica i giorni indietro da oggi che deve eliminare, default 365. |
|
|
|
*/ |
|
|
|
//set to True to enable DEBUG |
|
var debug = false; |
|
|
|
//name db inde of |
|
var dbNameIndexOf = "<db_like_name>"; |
|
|
|
//Collection to exclude |
|
var excludeCollection = ['local','<collection_name_to_eclude_tio_purge>']; |
|
|
|
if(!deleteData) |
|
var deleteData = false; |
|
|
|
if(typeof(db) === 'undefined' || !db){ |
|
print('ERROR --> specify serve name --host <host> --port <port>'); |
|
quit(1); |
|
} |
|
|
|
|
|
if(typeof(beforePurgeDay) === 'undefined' || !beforePurgeDay){ |
|
var beforePurgeDay = 365; |
|
print('INFO --> set default beforePurgeDay = ' + beforePurgeDay + ' to change add var beforePurgeDay=200; to eval'); |
|
} |
|
|
|
|
|
function print_debug(pretext, text){ |
|
if(!debug) |
|
return; |
|
if(text === 'object' || typeof text === 'object' || text === '[object Object]') |
|
print(pretext + "==>" + JSON.stringify(text)); |
|
else |
|
print(pretext + "==>" + text); |
|
}//print_debug(pretext, text) |
|
|
|
|
|
function convertDate(d) { |
|
|
|
function pad(s) { |
|
return (s < 10) ? '0' + s : s; |
|
} |
|
|
|
return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/'); |
|
}//convertDate(d) |
|
|
|
|
|
if(!deleteData){ |
|
print("****************************************************"); |
|
print("****************************************************"); |
|
print("DEBUG IS ENABLE NO DELETE DATA"); |
|
print('to remove DEBUG use --eval "var deleteData=true"') |
|
print("****************************************************"); |
|
print("****************************************************"); |
|
print("****************************************************"); |
|
}//if(!deleteData) |
|
|
|
|
|
for(name in db.getMongo().getDBNames()){ |
|
|
|
var dbName = db.getMongo().getDBNames()[name]; |
|
|
|
if(dbName.indexOf(dbNameIndexOf) === -1){ |
|
print("db " + dbName + " is not a " + dbNameIndexOf); |
|
continue; |
|
}//if(dbName.indexOf(dbNameIndexOf) === -1) |
|
|
|
var dbObj = db.getSisterDB(dbName); |
|
|
|
for(var colName in dbObj.getCollectionNames()){ |
|
var collectionName = dbObj.getCollectionNames()[colName]; |
|
|
|
|
|
if(excludeCollection.indexOf(collectionName) > -1) |
|
continue; |
|
|
|
print("DB Name => " + dbName); |
|
print("collection Name => " + collectionName); |
|
print("start analyze..."); |
|
|
|
|
|
|
|
var coll = dbObj[collectionName]; |
|
var maxminDateAggregate = coll.aggregate([ {$group: {_id:"1",max:{$max:"$ts"}, min:{$min:"$ts"} } } ]); |
|
|
|
print_debug("DEBUG",maxminDateAggregate); |
|
|
|
var dateMin = maxminDateAggregate.toArray()[0].min; |
|
var dateMax = maxminDateAggregate.toArray()[0].max; |
|
|
|
print_debug("DEBUG dateMax",dateMax); |
|
|
|
var deleteFrom = new Date(); |
|
deleteFrom.setDate(dateMax.getDate()-beforePurgeDay); |
|
|
|
print_debug("DEBUG deleteFrom ",deleteFrom); |
|
|
|
|
|
print("Max Date in log => " + convertDate(dateMax)); |
|
print("Delete all before day: -" + beforePurgeDay); |
|
print("Delete ==> from " + convertDate(dateMin) + " to " + convertDate(deleteFrom)); |
|
|
|
var query = {"ts" : {"$lte" : deleteFrom }}; |
|
print_debug("DEBUG query to delete: ",query); |
|
|
|
var count = coll.count(query); |
|
|
|
print("Tot to delete => " + count); |
|
|
|
if(deleteData){ |
|
var start = new Date().getTime(); |
|
print("running..."); |
|
var result = coll.remove(query); |
|
print("RESULT ==>", result); |
|
var end = new Date().getTime(); |
|
var time = end - start; |
|
print("Tot time => " + time); |
|
}//if(deleteData) |
|
|
|
}//for(var colName in dbObj.getCollectionNames()) |
|
|
|
}//for(name in db.getMongo().getDBNames()) |
|
|
|
|