Saving all/new questions added to idstats

This commit is contained in:
mrfry 2020-10-08 16:39:31 +02:00
parent 01a8e83b3b
commit b77a6bc0ad
2 changed files with 26 additions and 18 deletions

View file

@ -142,7 +142,7 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun, user) {
let subjRow = '\t' + data.subj let subjRow = '\t' + data.subj
if (data.id) { if (data.id) {
subjRow += ' ( CID: ' + logger.logHashed(data.id) + ')' subjRow += ' ( CID: ' + logger.logHashed(data.id) + ')'
idStats.LogId(user.id, data.subj) idStats.LogId(user.id, data.subj, allQuestions.length, allQLength)
} }
logger.Log(subjRow) logger.Log(subjRow)
if (data.version !== undefined) { if (data.version !== undefined) {

View file

@ -38,29 +38,29 @@ function Load() {
try { try {
var prevData = utils.ReadFile(idStatFile) var prevData = utils.ReadFile(idStatFile)
data = JSON.parse(prevData) data = JSON.parse(prevData)
} catch (e) { } catch (err) {
logger.Log( logger.Log(
'Error at loading id logs! (@ first run its normal)', 'Error at loading id logs! (@ first run its normal)',
logger.GetColor('redbg') logger.GetColor('redbg')
) )
console.log(e) console.error(err)
} }
try { try {
var prevVData = utils.ReadFile(idVStatFile) var prevVData = utils.ReadFile(idVStatFile)
vData = JSON.parse(prevVData) vData = JSON.parse(prevVData)
} catch (e) { } catch (err) {
logger.Log( logger.Log(
'Error at loading id logs! (@ first run its normal)', 'Error at loading id logs! (@ first run its normal)',
logger.GetColor('redbg') logger.GetColor('redbg')
) )
console.log(e) console.error(err)
} }
} }
function LogId(id, subj) { function LogId(id, subj, newQuestions, allQuestions) {
Inc(id, subj) Inc(id, subj, newQuestions, allQuestions)
AddVisitStat(id, subj) AddVisitStat(id, subj, newQuestions, allQuestions)
Save() Save()
} }
@ -71,35 +71,43 @@ function AddSubjToList(list, subj) {
list[subj]++ list[subj]++
} }
function Inc(value, subj) { function Inc(value, subj, newQuestions, allQuestions) {
if (data[value] === undefined) { if (data[value] === undefined) {
data[value] = { data[value] = {
count: 0, count: 0,
newQuestions: 0,
allQLength: 0,
subjs: {}, subjs: {},
} }
} }
data[value].count++ data[value].count++
data[value].newQuestions += newQuestions
data[value].allQuestions += allQuestions
AddSubjToList(data[value].subjs, subj) AddSubjToList(data[value].subjs, subj)
} }
function AddVisitStat(name, subj) { function AddVisitStat(name, subj, newQuestions, allQuestions) {
var m = new Date() var date = new Date()
const now = const now =
m.getFullYear() + date.getFullYear() +
'/' + '/' +
('0' + (m.getMonth() + 1)).slice(-2) + ('0' + (date.getMonth() + 1)).slice(-2) +
'/' + '/' +
('0' + m.getDate()).slice(-2) ('0' + date.getDate()).slice(-2)
if (vData[now] === undefined) { if (vData[now] === undefined) {
vData[now] = {} vData[now] = {}
} }
if (vData[now][name] === undefined) { if (vData[now][name] === undefined) {
vData[now][name] = { vData[now][name] = {
count: 0, count: 0,
newQuestions: 0,
allQLength: 0,
subjs: {}, subjs: {},
} }
} }
vData[now][name].count++ vData[now][name].count++
vData[now][name].newQuestions += newQuestions
vData[now][name].allQuestions += allQuestions
AddSubjToList(vData[now][name].subjs, subj) AddSubjToList(vData[now][name].subjs, subj)
} }
@ -109,16 +117,16 @@ function Save() {
try { try {
utils.WriteFile(JSON.stringify(data), idStatFile) utils.WriteFile(JSON.stringify(data), idStatFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (e) { } catch (err) {
logger.Log('Error at writing logs!', logger.GetColor('redbg')) logger.Log('Error at writing logs!', logger.GetColor('redbg'))
console.log(e) console.error(err)
} }
try { try {
utils.WriteFile(JSON.stringify(vData), idVStatFile) utils.WriteFile(JSON.stringify(vData), idVStatFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (e) { } catch (err) {
logger.Log('Error at writing visit logs!', logger.GetColor('redbg')) logger.Log('Error at writing visit logs!', logger.GetColor('redbg'))
console.log(e) console.error(err)
} }
writes = 0 writes = 0
} }