From 8f113d83086fb02ffe2e3f985c59c4570a61ad2e Mon Sep 17 00:00:00 2001 From: fatchan Date: Tue, 13 Aug 2019 10:33:56 +0000 Subject: [PATCH] calc pph and users in homepage independently until m~ongo fix --- helpers/build.js | 76 ++++++++++++++++++++++++++++++------------------ schedules.js | 1 + 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/helpers/build.js b/helpers/build.js index b6d8fafc..1bc9eb6d 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -105,40 +105,60 @@ module.exports = { buildHomepage: async () => { const label = '/index.html'; console.time(label); - const pastDay = Mongo.ObjectId.createFromTime(Math.floor((Date.now() - msTime.day)/1000)); - const datas = await Posts.db.aggregate([ - { - '$match': { - '_id': { - '$gt': pastDay + const [ activeUsers, postsPerHour ] = await Promise.all([ + Posts.db.aggregate([ + { + '$match': { + '_id': { + '$gt': Mongo.ObjectId.createFromTime(Math.floor((Date.now() - msTime.day*3)/1000)) + } + } + }, + { + '$group': { + '_id': '$board', + 'ips': { + '$addToSet': '$ip' + } + } + }, + { + '$project': { + 'ips': { + '$size': '$ips' + } } } - }, - { - '$group': { - '_id': '$board', - 'pph': { '$sum': 1 }, - 'ips': { '$addToSet': '$ip' } - } - }, - { - '$project': { - '_id': 1, - 'pph': { - '$floor': { //simple way, 24h average floored - '$divide': ['$pph', 24] + ]).toArray(), + Posts.db.aggregate([ + { + '$match': { + '_id': { + '$gt': Mongo.ObjectId.createFromTime(Math.floor((Date.now() - msTime.hour)/1000)) + } + } + }, + { + '$group': { + '_id': '$board', + 'pph': { + '$sum': 1 } - }, - 'ips': { - '$size': '$ips' } } - }, - ]).toArray(); + ]).toArray() + ]); + for (let i = 0; i < activeUsers.length; i++) { + const userboard = activeUsers[i]; + const pphboard = postsPerHour.find(b => b._id === userboard._id); + if (pphboard != null) { + userboard.pph = pphboard.pph; + } + } const bulkWrites = []; const updatedBoards = []; - for (let i = 0; i < datas.length; i++) { - const data = datas[i]; + for (let i = 0; i < activeUsers.length; i++) { + const data = activeUsers[i]; updatedBoards.push(data._id); //boards with pph get pph set bulkWrites.push({ @@ -148,7 +168,7 @@ module.exports = { }, 'update': { '$set': { - 'pph': data.pph, + 'pph': data.pph != null ? data.pph : 0, 'ips': data.ips } } diff --git a/schedules.js b/schedules.js index 42636f75..4e3b26e4 100644 --- a/schedules.js +++ b/schedules.js @@ -37,6 +37,7 @@ async function deleteCaptchas() { console.log('Starting schedules'); +await buildHomepage(); setInterval(async () => { try { await buildHomepage();