From 1aa2dc656c50b7f8845706d99fc1fd20852ff8a4 Mon Sep 17 00:00:00 2001 From: mrfry Date: Tue, 10 Nov 2020 15:14:50 +0100 Subject: [PATCH] =?UTF-8?q?Ranklist=20since=20parameter=C3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/api/api.js | 103 ++++++++++++++++++++++++++++++++++------- src/utils/logger.js | 8 ++-- 2 files changed, 91 insertions(+), 20 deletions(-) diff --git a/src/modules/api/api.js b/src/modules/api/api.js index c14e58d..41c66cd 100644 --- a/src/modules/api/api.js +++ b/src/modules/api/api.js @@ -44,6 +44,7 @@ const quickVoteResultsDir = 'stats/qvote' const quickVotes = 'stats/qvote/votes.json' const testUsersFile = 'data/testUsers.json' const idStatFile = 'stats/idstats' +const idvStatFile = 'stats/idvstats' // other constants const maxVeteranPwGetCount = 10 @@ -116,6 +117,19 @@ function GetApp() { var userSpecificMotd = {} var testUsers = [] + function mergeObjSum(a, b) { + const res = { ...b } + Object.keys(a).forEach((key) => { + if (res[key]) { + res[key] += a[key] + } else { + res[key] = a[key] + } + }) + + return res + } + function LoadVersion() { version = utils.ReadFile(versionFile) } @@ -125,7 +139,12 @@ function GetApp() { } function LoadUserSpecificMOTD() { - userSpecificMotd = utils.ReadJSON(userSpecificMotdFile) + try { + userSpecificMotd = utils.ReadJSON(userSpecificMotdFile) + } catch (err) { + logger.Log('Couldnt parse user specific motd!', logger.GetColor('redbg')) + console.error(err) + } } function LoadTestUsers() { @@ -178,29 +197,81 @@ function GetApp() { app.get('/ranklist', (req, res) => { logger.LogReq(req) + let since = req.query.since const user = req.session.user - const data = utils.ReadJSON(idStatFile) - const list = [] + if (!since) { + const data = utils.ReadJSON(idStatFile) + const list = [] - Object.keys(data).forEach((key) => { - list.push({ - userId: parseInt(key), - ...data[key], + Object.keys(data).forEach((key) => { + list.push({ + userId: parseInt(key), + ...data[key], + }) }) - }) - if (list.length === 0) { + if (list.length === 0) { + res.json({ + msg: 'There are no users in the stats db :c', + }) + return + } + res.json({ - msg: 'There are no users in the stats db :c', + list: list, + selfuserId: user.id, }) - return - } + } else { + try { + since = new Date(since) + if (!(since instanceof Date) || isNaN(since)) { + throw new Error('Not a date') + } + const data = utils.ReadJSON(idvStatFile) + const result = {} - res.json({ - list: list, - selfuserId: user.id, - }) + Object.keys(data).forEach((key) => { + const dayliStat = data[key] + + if (new Date(key) > since) { + Object.keys(dayliStat).forEach((userId) => { + const userStat = dayliStat[userId] + const uidRes = result[userId] + + if (!uidRes) { + result[userId] = userStat + } else { + result[userId] = { + count: uidRes.count + userStat.count, + newQuestions: uidRes.newQuestions + userStat.newQuestions, + allQuestions: uidRes.allQuestions + userStat.allQuestions, + subjs: mergeObjSum(uidRes.subjs, userStat.subjs), + } + } + }) + } + }) + + 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', + }) + } + } }) app.get('/quickvote', (req, res) => { diff --git a/src/utils/logger.js b/src/utils/logger.js index 75a21b4..d87aa76 100755 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -258,9 +258,9 @@ function AddUserIdStat(userId) { var m = new Date() const now = m.getFullYear() + - '/' + + '-' + ('0' + (m.getMonth() + 1)).slice(-2) + - '/' + + '-' + ('0' + m.getDate()).slice(-2) if (uvData[now] === undefined) { uvData[now] = {} @@ -289,9 +289,9 @@ function AddVisitStat(name) { var m = new Date() const now = m.getFullYear() + - '/' + + '-' + ('0' + (m.getMonth() + 1)).slice(-2) + - '/' + + '-' + ('0' + m.getDate()).slice(-2) if (vData[now] === undefined) { vData[now] = {}