Ranklist since parameterá

This commit is contained in:
mrfry 2020-11-10 15:14:50 +01:00
parent bd6bf2de62
commit 1aa2dc656c
2 changed files with 91 additions and 20 deletions

View file

@ -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) => {

View file

@ -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] = {}