mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Added debug more debug logging, saving every question that doesnt has a match with at leas 90%
This commit is contained in:
parent
a7a75bd9d4
commit
d1e03f4c41
2 changed files with 23 additions and 18 deletions
|
@ -32,12 +32,16 @@ const idStats = require('../utils/ids.js')
|
||||||
idStats.Load() // FIXME: dont always load when actions.js is used
|
idStats.Load() // FIXME: dont always load when actions.js is used
|
||||||
const utils = require('../utils/utils.js')
|
const utils = require('../utils/utils.js')
|
||||||
const classes = require('./question-classes/classes.js')
|
const classes = require('./question-classes/classes.js')
|
||||||
|
classes.initLogger(logger.DebugLog)
|
||||||
|
// if a recievend question doesnt match at least this % to any other question in the db it gets
|
||||||
|
// added to db
|
||||||
|
const minMatchAmmountToAdd = 90 // FIXME: test this value
|
||||||
|
|
||||||
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
|
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
|
||||||
var currWrites = 0
|
var currWrites = 0
|
||||||
|
|
||||||
function ProcessIncomingRequest (recievedData, qdb, infos) {
|
function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
logger.DebugLog('Processing incoming request', 1)
|
logger.DebugLog('Processing incoming request', 'actions', 1)
|
||||||
if (recievedData === undefined) {
|
if (recievedData === undefined) {
|
||||||
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
||||||
return
|
return
|
||||||
|
@ -49,7 +53,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
towrite += recievedData
|
towrite += recievedData
|
||||||
towrite += '\n------------------------------------------------------------------------------\n'
|
towrite += '\n------------------------------------------------------------------------------\n'
|
||||||
utils.AppendToFile(towrite, recDataFile)
|
utils.AppendToFile(towrite, recDataFile)
|
||||||
logger.DebugLog('recDataFile written', 1)
|
logger.DebugLog('recDataFile written', 'actions', 1)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.log('Error writing recieved data.')
|
logger.log('Error writing recieved data.')
|
||||||
}
|
}
|
||||||
|
@ -57,29 +61,27 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
try {
|
try {
|
||||||
// recievedData: { version: "", id: "", subj: "" quiz: {} }
|
// recievedData: { version: "", id: "", subj: "" quiz: {} }
|
||||||
let d = JSON.parse(recievedData)
|
let d = JSON.parse(recievedData)
|
||||||
logger.DebugLog('recievedData JSON parsed', 1)
|
logger.DebugLog('recievedData JSON parsed', 'actions', 1)
|
||||||
let allQLength = d.quiz.length
|
let allQLength = d.quiz.length
|
||||||
let allQuestions = []
|
let allQuestions = []
|
||||||
|
|
||||||
logger.DebugLog(logger.hr, 2)
|
|
||||||
d.quiz.forEach((question) => {
|
d.quiz.forEach((question) => {
|
||||||
logger.DebugLog('Question:', 2)
|
logger.DebugLog('Question:', 'actions', 2)
|
||||||
logger.DebugLog(question, 2)
|
logger.DebugLog(question, 'actions', 2)
|
||||||
let q = new classes.Question(question.Q, question.A, question.data)
|
let q = new classes.Question(question.Q, question.A, question.data)
|
||||||
let sames = qdb.Search(q, d.subj)
|
let sames = qdb.Search(q, d.subj)
|
||||||
logger.DebugLog('Same questions:', 2)
|
logger.DebugLog('Same questions:', 'actions', 2)
|
||||||
logger.DebugLog('Length: ' + sames.length, 2)
|
logger.DebugLog('Length: ' + sames.length, 'actions', 2)
|
||||||
logger.DebugLog(sames, 3)
|
logger.DebugLog(sames, 'actions', 3)
|
||||||
// if it didnt find any question, or every found questions match is lower thatn 80
|
// if it didnt find any question, or every found questions match is lower thatn 80
|
||||||
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
||||||
return searchResItem.match < classes.minMatchAmmount
|
return searchResItem.match < minMatchAmmountToAdd
|
||||||
})
|
})
|
||||||
logger.DebugLog('isNew: ' + isNew, 2)
|
logger.DebugLog('isNew: ' + isNew, 'actions', 2)
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
allQuestions.push(q)
|
allQuestions.push(q)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
logger.DebugLog(logger.hr, 2)
|
|
||||||
|
|
||||||
let color = logger.GetColor('green')
|
let color = logger.GetColor('green')
|
||||||
let msg = ''
|
let msg = ''
|
||||||
|
@ -92,17 +94,17 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
})
|
})
|
||||||
|
|
||||||
currWrites++
|
currWrites++
|
||||||
logger.DebugLog('currWrites for data.json: ' + currWrites, 1)
|
logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
|
||||||
if (currWrites >= writeAfter) {
|
if (currWrites >= writeAfter) {
|
||||||
currWrites = 0
|
currWrites = 0
|
||||||
try {
|
try {
|
||||||
qdb.version = infos.version
|
qdb.version = infos.version
|
||||||
qdb.motd = infos.motd
|
qdb.motd = infos.motd
|
||||||
logger.DebugLog('version and motd set for data.json', 3)
|
logger.DebugLog('version and motd set for data.json', 'actions', 3)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log('MOTD/Version writing/reading error!')
|
logger.Log('MOTD/Version writing/reading error!')
|
||||||
}
|
}
|
||||||
logger.DebugLog('Writing data.json', 1)
|
logger.DebugLog('Writing data.json', 'actions', 1)
|
||||||
utils.WriteFile(JSON.stringify(qdb), dataFile)
|
utils.WriteFile(JSON.stringify(qdb), dataFile)
|
||||||
logger.Log('\tData file written', color)
|
logger.Log('\tData file written', color)
|
||||||
}
|
}
|
||||||
|
@ -119,7 +121,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
if (d.version !== undefined) { msg += '. Version: ' + d.version }
|
if (d.version !== undefined) { msg += '. Version: ' + d.version }
|
||||||
|
|
||||||
logger.Log('\t' + msg, color)
|
logger.Log('\t' + msg, color)
|
||||||
logger.DebugLog('ProcessIncomingRequest done', 1)
|
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
|
||||||
return allQuestions.length
|
return allQuestions.length
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
|
|
@ -71,10 +71,13 @@ function GetDateString () {
|
||||||
return GetRandomColor(m.getHours().toString()) + d + C()
|
return GetRandomColor(m.getHours().toString()) + d + C()
|
||||||
}
|
}
|
||||||
|
|
||||||
function DebugLog (msg, lvl) {
|
function DebugLog (msg, name, lvl) {
|
||||||
if (lvl <= debugLevel) {
|
if (lvl <= debugLevel) {
|
||||||
|
if (msg === 'hr') {
|
||||||
|
msg = hr
|
||||||
|
}
|
||||||
let s = msg
|
let s = msg
|
||||||
let header = C('red') + '#DEBUG ' + lvl + '#' + C() + DELIM + C()
|
let header = `${C('red')}#DEBUG${lvl}#${C('yellow')}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
|
||||||
if (typeof msg !== 'object') {
|
if (typeof msg !== 'object') {
|
||||||
s = header + msg
|
s = header + msg
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue