user and pph on homepage

merge-requests/208/head
fatchan 5 years ago
parent d412be3450
commit bcf477b2d3
  1. 2
      gulpfile.js
  2. 114
      helpers/build.js
  3. 1
      models/forms/create.js
  4. 8
      views/pages/home.pug

@ -48,6 +48,7 @@ async function wipe() {
'owner': '', 'owner': '',
'banners': [], 'banners': [],
'sequence_value': 1, 'sequence_value': 1,
'pph': 0,
'settings': { 'settings': {
'name': 'test', 'name': 'test',
'description': 'testing board', 'description': 'testing board',
@ -81,6 +82,7 @@ async function wipe() {
} }
}) })
//add indexes - should profiled and changed at some point if necessary //add indexes - should profiled and changed at some point if necessary
, Boards.db.createIndex({ips: 1, pph:1, sequence_value:1})
, Bans.db.dropIndexes() , Bans.db.dropIndexes()
, Captchas.captcha.dropIndexes() , Captchas.captcha.dropIndexes()
, Captchas.ratelimit.dropIndexes() , Captchas.ratelimit.dropIndexes()

@ -12,23 +12,31 @@ const Mongo = require(__dirname+'/../db/db.js')
module.exports = { module.exports = {
buildBanners: async(board) => { buildBanners: async(board) => {
await render(`${board._id}/banners.html`, 'banners.pug', { const label = `${board._id}/banners.html`;
console.time(label);
await render(label, 'banners.pug', {
board: board, board: board,
}); });
console.timeEnd(label);
}, },
buildCatalog: async (board) => { buildCatalog: async (board) => {
const label = `${board._id || board}/catalog.html`;
console.time(label);
if (!board._id) { if (!board._id) {
board = await Boards.findOne(board); board = await Boards.findOne(board);
} }
const threads = await Posts.getCatalog(board._id); const threads = await Posts.getCatalog(board._id);
await render(`${board._id}/catalog.html`, 'catalog.pug', { await render(label, 'catalog.pug', {
board, board,
threads, threads,
}); });
console.timeEnd(label);
}, },
buildThread: async (threadId, board) => { buildThread: async (threadId, board) => {
const label = `${board._id || board}/thread/${threadId}.html`;
console.time(label);
if (!board._id) { if (!board._id) {
board = await Boards.findOne(board); board = await Boards.findOne(board);
} }
@ -36,28 +44,34 @@ module.exports = {
if (!thread) { if (!thread) {
return; //this thread may have been an OP that was deleted return; //this thread may have been an OP that was deleted
} }
await render(`${board._id}/thread/${threadId}.html`, 'thread.pug', { await render(label, 'thread.pug', {
board, board,
thread, thread,
}); });
console.timeEnd(label);
}, },
buildBoard: async (board, page, maxPage=null) => { buildBoard: async (board, page, maxPage=null) => {
const label = `${board._id}/${page === 1 ? 'index' : page}.html`;
console.time(label);
const threads = await Posts.getRecent(board._id, page); const threads = await Posts.getRecent(board._id, page);
if (maxPage == null) { if (maxPage == null) {
maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10));
} }
await render(`${board._id}/${page === 1 ? 'index' : page}.html`, 'board.pug', { await render(label, 'board.pug', {
board, board,
threads, threads,
maxPage, maxPage,
page, page,
}); });
console.timeEnd(label);
}, },
//building multiple pages (for rebuilds) //building multiple pages (for rebuilds)
buildBoardMultiple: async (board, startpage=1, endpage) => { buildBoardMultiple: async (board, startpage=1, endpage) => {
const label = 'multiple';
console.time(label);
const maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); const maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10));
if (endpage === 0) { if (endpage === 0) {
//deleted only/all posts, so only 1 page will remain //deleted only/all posts, so only 1 page will remain
@ -68,6 +82,7 @@ module.exports = {
} }
const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0 const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0
const threads = await Posts.getRecent(board._id, startpage, difference*10); const threads = await Posts.getRecent(board._id, startpage, difference*10);
console.timeLog(label, `${board._id}/${startpage === 1 ? 'index' : startpage}.html => ${board._id}/${endpage === 1 ? 'index' : endpage}.html`)
const buildArray = []; const buildArray = [];
for (let i = startpage; i <= endpage; i++) { for (let i = startpage; i <= endpage; i++) {
let spliceStart = (i-1)*10; let spliceStart = (i-1)*10;
@ -84,19 +99,18 @@ module.exports = {
); );
} }
await Promise.all(buildArray); await Promise.all(buildArray);
console.timeEnd(label);
}, },
buildHomepage: async () => { buildHomepage: async () => {
//getting boards const label = '/index.html';
const boards = await Boards.find(); console.time(label);
//geting PPH for each board const pastDay = Mongo.ObjectId.createFromTime(Math.floor((Date.now() - msTime.day)/1000));
const pastHour = Math.floor((Date.now() - msTime.hour)/1000); const datas = await Posts.db.aggregate([
const pastHourObjectId = Mongo.ObjectId.createFromTime(pastHour);
const pph = await Posts.db.aggregate([
{ {
'$match': { '$match': {
'_id': { '_id': {
'$gt': pastHourObjectId '$gt': pastDay
} }
} }
}, },
@ -104,26 +118,77 @@ module.exports = {
'$group': { '$group': {
'_id': '$board', '_id': '$board',
'pph': { '$sum': 1 }, 'pph': { '$sum': 1 },
'ips': { '$addToSet': '$ip' }
} }
}, },
]).toArray().then(res => { {
return res.reduce((acc, item) => { '$project': {
acc[item._id] = item.pph; '_id': 1,
return acc; 'pph': {
}, {}); '$floor': { //simple way, 24h average floored
}); '$divide': ['$pph', 24]
for (let i = 0; i < boards.length; i++) { }
const board = boards[i]; },
board.pph = pph[board._id] || 0; 'ips': {
'$size': '$ips'
}
}
},
]).toArray();
const bulkWrites = [];
const updatedBoards = [];
for (let i = 0; i < datas.length; i++) {
const data = datas[i];
updatedBoards.push(data._id);
//boards with pph get pph set
bulkWrites.push({
'updateOne': {
'filter': {
'_id': data._id
},
'update': {
'$set': {
'pph': data.pph,
'ips': data.ips
}
}
}
})
} }
//getting file stats //boards with no pph get set to 0
bulkWrites.push({
'updateMany': {
'filter': {
'_id': {
'$nin': updatedBoards
}
},
'update': {
'$set': {
'pph': 0,
'ips': 0
}
}
}
});
await Boards.db.bulkWrite(bulkWrites);
//getting boards now that pph is set
const boards = await Boards.db.find({}).sort({
'ips': -1,
'pph': -1,
'sequence_value': -1,
}).limit(20).toArray(); //limit 20 homepage
//might move filestats to an $out or $merge in schedules file
const fileStats = await Files.db.aggregate([ const fileStats = await Files.db.aggregate([
{ {
'$group': { '$group': {
'_id': null, '_id': null,
//could add other interesting mongo aggregate stuff here like averages 'count': {
'count': { '$sum': 1 }, '$sum': 1
'size': { '$sum': '$size' } },
'size': {
'$sum': '$size'
}
} }
} }
]).toArray().then(res => { ]).toArray().then(res => {
@ -138,6 +203,7 @@ module.exports = {
boards, boards,
fileStats, fileStats,
}); });
console.timeEnd(label);
}, },
buildChangePassword: () => { buildChangePassword: () => {

@ -29,6 +29,7 @@ module.exports = async (req, res, next) => {
'owner': req.session.user.username, 'owner': req.session.user.username,
'banners': [], 'banners': [],
'sequence_value': 1, 'sequence_value': 1,
'pph': 0,
'settings': { 'settings': {
name, name,
description, description,

@ -23,16 +23,16 @@ block content
table table
tr tr
th Board th Board
th Title
th Description th Description
th Posts/h th(title='Average posts per hour over the past 24, rounded down') Posts/h
th(title='Unique IPs that have posted in the past 24h') Active Users
th Total Posts th Total Posts
each board in boards each board in boards
tr tr
td: a(href=`/${board._id}/`) /#{board._id}/ td: a(href=`/${board._id}/`) /#{board._id}/ - #{board.settings.name}
td #{board.settings.name}
td #{board.settings.description} td #{board.settings.description}
td #{board.pph} td #{board.pph}
td #{board.ips}
td #{board.sequence_value-1} td #{board.sequence_value-1}
.table-container.flex-center.mv-10.text-center .table-container.flex-center.mv-10.text-center
table table

Loading…
Cancel
Save