From 6ea0263b02ada3f960c84ff23d341d3fc0b0b06f Mon Sep 17 00:00:00 2001 From: mrfry Date: Sat, 12 Sep 2020 12:56:15 +0200 Subject: [PATCH] some-random-actions-modification --- modules/qmining/qmining-page | 2 +- qminingPublic/moodle-test-userscript | 2 +- utils/actions.js | 194 ++++++++++++++------------- 3 files changed, 100 insertions(+), 98 deletions(-) diff --git a/modules/qmining/qmining-page b/modules/qmining/qmining-page index 7ac6534..ada93f0 160000 --- a/modules/qmining/qmining-page +++ b/modules/qmining/qmining-page @@ -1 +1 @@ -Subproject commit 7ac65348e41463670fc303fffc788f0baa03d288 +Subproject commit ada93f0bb9b87863e04130e2db77ca52e8087058 diff --git a/qminingPublic/moodle-test-userscript b/qminingPublic/moodle-test-userscript index cbaaa7a..f8d4bf2 160000 --- a/qminingPublic/moodle-test-userscript +++ b/qminingPublic/moodle-test-userscript @@ -1 +1 @@ -Subproject commit cbaaa7a376a9ac40e95b534c8a1181a017e95ddd +Subproject commit f8d4bf2a414d2973582c08bfa8b8b8f19389b2e9 diff --git a/utils/actions.js b/utils/actions.js index a230100..2201180 100755 --- a/utils/actions.js +++ b/utils/actions.js @@ -39,113 +39,115 @@ const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate var currWrites = 0 function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) { - logger.DebugLog('Processing incoming request', 'actions', 1) - if (recievedData === undefined) { - logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) - return - } - - try { - let towrite = logger.GetDateString() + '\n' - towrite += '------------------------------------------------------------------------------\n' - if (typeof recievedData === 'object') { - towrite += JSON.stringify(recievedData) - } else { - towrite += recievedData + return new Promise((resolve, reject) => { + logger.DebugLog('Processing incoming request', 'actions', 1) + if (recievedData === undefined) { + logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) + reject(new Error('Recieved data is undefined!')) } - towrite += '\n------------------------------------------------------------------------------\n' - utils.AppendToFile(towrite, recDataFile) - logger.DebugLog('recDataFile written', 'actions', 1) - } catch (e) { - logger.log('Error writing recieved data.') - } - try { + try { + let towrite = logger.GetDateString() + '\n' + towrite += '------------------------------------------------------------------------------\n' + if (typeof recievedData === 'object') { + towrite += JSON.stringify(recievedData) + } else { + towrite += recievedData + } + towrite += '\n------------------------------------------------------------------------------\n' + utils.AppendToFile(towrite, recDataFile) + logger.DebugLog('recDataFile written', 'actions', 1) + } catch (e) { + logger.log('Error writing recieved data.') + } + + try { // recievedData: { version: "", id: "", subj: "" quiz: {} } - let d = recievedData - // FIXME: if is for backwards compatibility, remove this sometime in the future - if (typeof d !== 'object') { - d = JSON.parse(recievedData) - } - - logger.DebugLog('recievedData JSON parsed', 'actions', 1) - logger.DebugLog(d, 'actions', 3) - let allQLength = d.quiz.length - let allQuestions = [] - - d.quiz.forEach((question) => { - logger.DebugLog('Question:', 'actions', 2) - logger.DebugLog(question, 'actions', 2) - let q = new classes.Question(question.Q, question.A, question.data) - logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3) - logger.DebugLog(q, 'actions', 3) - - let sames = qdb.Search(q, d.subj) - 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 < minMatchAmmountToAdd - }) - logger.DebugLog('isNew: ' + isNew, 'actions', 2) - if (isNew) { - allQuestions.push(q) + let d = recievedData + // FIXME: if is for backwards compatibility, remove this sometime in the future + if (typeof d !== 'object') { + d = JSON.parse(recievedData) } - }) - let color = logger.GetColor('green') - let msg = '' - if (allQuestions.length > 0) { - color = logger.GetColor('blue') - msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )` - allQuestions.forEach((q) => { - const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj) - logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3) + logger.DebugLog('recievedData JSON parsed', 'actions', 1) + logger.DebugLog(d, 'actions', 3) + let allQLength = d.quiz.length + let allQuestions = [] + + d.quiz.forEach((question) => { + logger.DebugLog('Question:', 'actions', 2) + logger.DebugLog(question, 'actions', 2) + let q = new classes.Question(question.Q, question.A, question.data) + logger.DebugLog('Searching for question in subj ' + d.subj, 'actions', 3) logger.DebugLog(q, 'actions', 3) - qdb.AddQuestion(sName, q) + + let sames = qdb.Search(q, d.subj) + 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 < minMatchAmmountToAdd + }) + logger.DebugLog('isNew: ' + isNew, 'actions', 2) + if (isNew) { + allQuestions.push(q) + } }) - currWrites++ - logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1) - if (currWrites >= writeAfter && !dryRun) { - currWrites = 0 - try { - qdb.version = infos.version - qdb.motd = infos.motd - logger.DebugLog('version and motd set for data.json', 'actions', 3) - } catch (e) { - logger.Log('MOTD/Version writing/reading error!') + let color = logger.GetColor('green') + let msg = '' + if (allQuestions.length > 0) { + color = logger.GetColor('blue') + msg += `New questions: ${allQuestions.length} ( All: ${allQLength} )` + allQuestions.forEach((q) => { + const sName = classes.SUtils.GetSubjNameWithoutYear(d.subj) + logger.DebugLog('Adding question with subjName: ' + sName + ' :', 'actions', 3) + logger.DebugLog(q, 'actions', 3) + qdb.AddQuestion(sName, q) + }) + + currWrites++ + logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1) + if (currWrites >= writeAfter && !dryRun) { + currWrites = 0 + try { + qdb.version = infos.version + qdb.motd = infos.motd + 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', 'actions', 1) + utils.WriteFile(JSON.stringify(qdb), dataFile) + logger.Log('\tData file written', color) + } else if (dryRun) { + logger.Log('\tDry run') } - logger.DebugLog('Writing data.json', 'actions', 1) - utils.WriteFile(JSON.stringify(qdb), dataFile) - logger.Log('\tData file written', color) - } else if (dryRun) { - logger.Log('\tDry run') + } else { + msg += `No new data ( ${allQLength} )` } - } else { - msg += `No new data ( ${allQLength} )` + + let subjRow = '\t' + d.subj + if (d.id) { + subjRow += ' ( CID: ' + logger.logHashed(d.id) + ')' + idStats.LogId(d.id, d.subj) + } + logger.Log(subjRow) + if (d.version !== undefined) { msg += '. Version: ' + d.version } + + logger.Log('\t' + msg, color) + logger.DebugLog('New Questions:', 'actions', 2) + logger.DebugLog(allQuestions, 'actions', 2) + + logger.DebugLog('ProcessIncomingRequest done', 'actions', 1) + resolve(allQLength.length) + } catch (e) { + console.log(e) + logger.Log('Couldnt parse JSON data', logger.GetColor('redbg')) + reject(new Error('Couldnt parse JSON data')) } - - let subjRow = '\t' + d.subj - if (d.id) { - subjRow += ' ( CID: ' + logger.logHashed(d.id) + ')' - idStats.LogId(d.id, d.subj) - } - logger.Log(subjRow) - if (d.version !== undefined) { msg += '. Version: ' + d.version } - - logger.Log('\t' + msg, color) - logger.DebugLog('New Questions:', 'actions', 2) - logger.DebugLog(allQuestions, 'actions', 2) - - logger.DebugLog('ProcessIncomingRequest done', 'actions', 1) - return allQuestions.length - } catch (e) { - console.log(e) - logger.Log('Couldnt parse JSON data', logger.GetColor('redbg')) - return -1 - } + }) } // loading stuff