diff --git a/db/stats.js b/db/stats.js index ee3b593c..84b5b060 100644 --- a/db/stats.js +++ b/db/stats.js @@ -37,48 +37,57 @@ module.exports = { }, updateBoards: () => { +//todo: figure out how to get single result set and $group $facets so I can fix this and improve resetStats return db.aggregate([ { + '$unwind': { + 'path': '$ips', + 'preserveNullAndEmptyArrays': true + //provides empty array instead of null so that $project stage $size will work and + //update dead boards back to 0 ips + } + }, { '$group': { '_id': '$board', - 'ips': { - '$sum': { - '$size': '$ips' - } - }, 'pph': { '$sum': '$pph' + }, + 'ips': { + '$addToSet': '$ips' } } + }, { + '$project': { + 'ips': { + '$size': '$ips' + }, + 'pph': 1 + } }, { '$merge': { 'into': 'boards' } } - ]).toArray(); + ]); }, - //reset IP list for previous hour - resetIps: () => { - const hour = new Date(); - return db.updateMany({ - 'hour': hour.setHours(hour.getHours()-1) - }, { - '$set': { - 'ips': [] - } - }); - }, - - //reset all hours. -//TODO: implement a $facet with 2 groups in updateBoards so I can keep pph across hours - resetPph: () => { - return db.updateMany({}, { - '$set': { - 'pph': 0, - 'tph': 0 - } - }); + //reset stats, used at start of each hour + resetStats: () => { + return Promise.all([ + db.updateMany({ + 'hour': new Date().getHours() + }, { + '$set': { + 'ips': [], + } + }), + db.updateMany({}, { + '$set': { + 'pph': 0, + 'tph': 0 + } + }), + ]); }, deleteBoard: (board) => { diff --git a/helpers/build.js b/helpers/build.js index 1cf3e14c..c96ae631 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -196,8 +196,7 @@ module.exports = { const label = 'Hourly stats rollover'; const start = process.hrtime(); await Stats.updateBoards(); - await Stats.resetPph(); - await Stats.resetIps(); + await Stats.resetStats(); const end = process.hrtime(start); console.log(timeDiffString(label, end)); }, diff --git a/models/forms/actionhandler.js b/models/forms/actionhandler.js index 13f4db09..e2e30f60 100644 --- a/models/forms/actionhandler.js +++ b/models/forms/actionhandler.js @@ -134,7 +134,7 @@ module.exports = async (req, res, next) => { if (action) { if (req.body.unlink_file) { modlogActions.push('Unlink files'); - } else if () { + } else if (req.body.delete_file) { modlogActions.push('Delete files'); } aggregateNeeded = true; diff --git a/schedules.js b/schedules.js index be4da810..a84c6efd 100644 --- a/schedules.js +++ b/schedules.js @@ -43,7 +43,7 @@ const msTime = require(__dirname+'/helpers/mstime.js') 'options': {} }, { 'repeat': { - 'cron': '1 * * * *' + 'cron': '0 * * * *' } });