Added debug more debug logging, saving every question that doesnt has a match with at leas 90%

This commit is contained in:
MrFry 2020-03-15 09:58:20 +01:00
parent a7a75bd9d4
commit d1e03f4c41
2 changed files with 23 additions and 18 deletions

View file

@ -32,12 +32,16 @@ const idStats = require('../utils/ids.js')
idStats.Load() // FIXME: dont always load when actions.js is used
const utils = require('../utils/utils.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
var currWrites = 0
function ProcessIncomingRequest (recievedData, qdb, infos) {
logger.DebugLog('Processing incoming request', 1)
logger.DebugLog('Processing incoming request', 'actions', 1)
if (recievedData === undefined) {
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
return
@ -49,7 +53,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
towrite += recievedData
towrite += '\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, recDataFile)
logger.DebugLog('recDataFile written', 1)
logger.DebugLog('recDataFile written', 'actions', 1)
} catch (e) {
logger.log('Error writing recieved data.')
}
@ -57,29 +61,27 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
try {
// recievedData: { version: "", id: "", subj: "" quiz: {} }
let d = JSON.parse(recievedData)
logger.DebugLog('recievedData JSON parsed', 1)
logger.DebugLog('recievedData JSON parsed', 'actions', 1)
let allQLength = d.quiz.length
let allQuestions = []
logger.DebugLog(logger.hr, 2)
d.quiz.forEach((question) => {
logger.DebugLog('Question:', 2)
logger.DebugLog(question, 2)
logger.DebugLog('Question:', 'actions', 2)
logger.DebugLog(question, 'actions', 2)
let q = new classes.Question(question.Q, question.A, question.data)
let sames = qdb.Search(q, d.subj)
logger.DebugLog('Same questions:', 2)
logger.DebugLog('Length: ' + sames.length, 2)
logger.DebugLog(sames, 3)
logger.DebugLog('Same questions:', 'actions', 2)
logger.DebugLog('Length: ' + sames.length, 'actions', 2)
logger.DebugLog(sames, 'actions', 3)
// if it didnt find any question, or every found questions match is lower thatn 80
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) {
allQuestions.push(q)
}
})
logger.DebugLog(logger.hr, 2)
let color = logger.GetColor('green')
let msg = ''
@ -92,17 +94,17 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
})
currWrites++
logger.DebugLog('currWrites for data.json: ' + currWrites, 1)
logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
if (currWrites >= writeAfter) {
currWrites = 0
try {
qdb.version = infos.version
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) {
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)
logger.Log('\tData file written', color)
}
@ -119,7 +121,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
if (d.version !== undefined) { msg += '. Version: ' + d.version }
logger.Log('\t' + msg, color)
logger.DebugLog('ProcessIncomingRequest done', 1)
logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
return allQuestions.length
} catch (e) {
console.log(e)