Added sum to ranklist, simplified ranklist code a bit

This commit is contained in:
mrfry 2020-11-10 16:11:39 +01:00
parent 1aa2dc656c
commit c9df0fdce5

View file

@ -197,31 +197,12 @@ function GetApp() {
app.get('/ranklist', (req, res) => { app.get('/ranklist', (req, res) => {
logger.LogReq(req) logger.LogReq(req)
let result
let since = req.query.since let since = req.query.since
const user = req.session.user const user = req.session.user
if (!since) { if (!since) {
const data = utils.ReadJSON(idStatFile) result = utils.ReadJSON(idStatFile)
const list = []
Object.keys(data).forEach((key) => {
list.push({
userId: parseInt(key),
...data[key],
})
})
if (list.length === 0) {
res.json({
msg: 'There are no users in the stats db :c',
})
return
}
res.json({
list: list,
selfuserId: user.id,
})
} else { } else {
try { try {
since = new Date(since) since = new Date(since)
@ -229,14 +210,14 @@ function GetApp() {
throw new Error('Not a date') throw new Error('Not a date')
} }
const data = utils.ReadJSON(idvStatFile) const data = utils.ReadJSON(idvStatFile)
const result = {} result = {}
Object.keys(data).forEach((key) => { Object.keys(data).forEach((key) => {
const dayliStat = data[key] const dailyStat = data[key]
if (new Date(key) > since) { if (new Date(key) > since) {
Object.keys(dayliStat).forEach((userId) => { Object.keys(dailyStat).forEach((userId) => {
const userStat = dayliStat[userId] const userStat = dailyStat[userId]
const uidRes = result[userId] const uidRes = result[userId]
if (!uidRes) { if (!uidRes) {
@ -252,26 +233,43 @@ function GetApp() {
}) })
} }
}) })
const list = []
Object.keys(result).forEach((key) => {
list.push({
userId: parseInt(key),
...result[key],
})
})
res.json({
since: since,
list: list,
selfuserId: user.id,
})
} catch (err) { } catch (err) {
res.json({ res.json({
msg: 'invalid date format, or other error occured', msg: 'invalid date format, or other error occured',
}) })
} }
} }
const list = []
const sum = {
count: 0,
newQuestions: 0,
allQuestions: 0,
}
Object.keys(result).forEach((key) => {
list.push({
userId: parseInt(key),
...result[key],
})
sum.count = sum.count + result[key].count
sum.newQuestions = sum.newQuestions + result[key].newQuestions
sum.allQuestions = sum.allQuestions + result[key].allQuestions
})
if (list.length === 0) {
res.json({
msg: 'There are no users in the stats db :c',
})
return
}
res.json({
since: since,
sum: sum,
list: list,
selfuserId: user.id,
})
}) })
app.get('/quickvote', (req, res) => { app.get('/quickvote', (req, res) => {