diff --git a/src/modules/api/api.js b/src/modules/api/api.js index 41c66cd..c1b9b7c 100644 --- a/src/modules/api/api.js +++ b/src/modules/api/api.js @@ -197,31 +197,12 @@ function GetApp() { app.get('/ranklist', (req, res) => { logger.LogReq(req) + let result let since = req.query.since const user = req.session.user if (!since) { - const data = 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, - }) + result = utils.ReadJSON(idStatFile) } else { try { since = new Date(since) @@ -229,14 +210,14 @@ function GetApp() { throw new Error('Not a date') } const data = utils.ReadJSON(idvStatFile) - const result = {} + result = {} Object.keys(data).forEach((key) => { - const dayliStat = data[key] + const dailyStat = data[key] if (new Date(key) > since) { - Object.keys(dayliStat).forEach((userId) => { - const userStat = dayliStat[userId] + Object.keys(dailyStat).forEach((userId) => { + const userStat = dailyStat[userId] const uidRes = result[userId] 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) { res.json({ 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) => {