get dates array directly from mongo w/ aggregate

merge-requests/208/head
fatchan 5 years ago
parent 90fa066261
commit bed7008d55
  1. 43
      db/modlogs.js
  2. 2
      gulpfile.js
  3. 14
      helpers/build.js
  4. 4
      models/pages/modloglist.js
  5. 6
      views/pages/modloglist.pug

@ -5,16 +5,39 @@ const Mongo = require(__dirname+'/db.js')
module.exports = {
getFirst: (board) => {
return db.find({
'board': board._id
}).sort({_id:1}).limit(1).toArray();
},
getLast: (board) => {
return db.find({
'board': board._id
}).sort({_id:-1}).limit(1).toArray();
getDates: (board) => {
return db.aggregate([
{
'$match': {
'board': board._id
}
},
{
'$project': {
'year': {
'$year': '$_id'
},
'month': {
'$month': '$_id'
},
'day': {
'$dayOfMonth': '$_id'
}
}
},
{
'$group': {
'_id': null,
'dates': {
'$addToSet': {
'year': '$year',
'month': '$month',
'day': '$day',
}
}
}
},
]).toArray().then(res => res[0].dates);
},
findBetweenDate: (board, start, end) => {

@ -96,6 +96,8 @@ async function wipe() {
, Captchas.db.dropIndexes()
, Ratelimits.db.dropIndexes()
, Posts.db.dropIndexes()
, Modlogs.db.dropIndexes()
, Modlogs.db.createIndex({ 'board': 1 })
, Files.db.createIndex({ 'count': 1 })
, Bans.db.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 0 }) //custom expiry, i.e. it will expire when current date > than this date
, Captchas.db.createIndex({ "expireAt": 1 }, { expireAfterSeconds: 300 }) //captchas valid for 5 minutes

@ -2,7 +2,6 @@
const Mongo = require(__dirname+'/../db/db.js')
, msTime = require(__dirname+'/mstime.js')
, dateArray = require(__dirname+'/datearray.js')
, { Posts, Files, Boards, News, Modlogs } = require(__dirname+'/../db/')
, render = require(__dirname+'/render.js');
@ -136,17 +135,8 @@ module.exports = {
buildModLogList: async (board) => {
const label = `/${board._id}/logs.html`;
console.time(label);
let dates = []
const [ firstLog, lastLog ] = await Promise.all([
Modlogs.getFirst(board),
Modlogs.getLast(board)
]);
if (firstLog.length > 0 && lastLog.length > 0) {
const firstLogDate = firstLog[0].date;
firstLogDate.setHours(1,0,0,0);
const lastLogDate = lastLog[0].date;
dates = dateArray(firstLogDate, lastLogDate).reverse();
}
const dates = (await Modlogs.getDates(board)).reverse();
console.log(dates)
await render(label, 'modloglist.pug', {
board,
dates

@ -1,8 +1,6 @@
'use strict';
const { Posts, Modlogs } = require(__dirname+'/../../db/')
, { buildModLogList } = require(__dirname+'/../../helpers/build.js')
, dateArray = require(__dirname+'/../../helpers/datearray.js')
const { buildModLogList } = require(__dirname+'/../../helpers/build.js')
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js');
module.exports = async (req, res, next) => {

@ -24,9 +24,9 @@ block content
for date in dates
tr
-
const day = ('0'+date.getDate()).slice(-2);
const month = ('0'+(date.getMonth()+1)).slice(-2);
const year = date.getFullYear();
const day = ('0'+date.day).slice(-2);
const month = ('0'+date.month).slice(-2);
const year = date.year;
const logName = `${month}-${day}-${year}`;
td: a(href=`logs/${logName}.html`) #{logName}
hr(size=1)

Loading…
Cancel
Save