mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
searchData is now using workers, fixed random question letter in question
This commit is contained in:
parent
63b08244dc
commit
6c781eba97
5 changed files with 52 additions and 95 deletions
|
@ -42,8 +42,6 @@ const reqlogger = require('./middlewares/reqlogger.middleware.js')
|
||||||
const extraModulesFile = './src/extraModules.json'
|
const extraModulesFile = './src/extraModules.json'
|
||||||
const modulesFile = './src/modules.json'
|
const modulesFile = './src/modules.json'
|
||||||
const usersDBPath = 'data/dbs/users.db'
|
const usersDBPath = 'data/dbs/users.db'
|
||||||
const { initLogger } = require('./utils/classes.js')
|
|
||||||
initLogger(logger.DebugLog)
|
|
||||||
const idStats = require('./utils/ids.js')
|
const idStats = require('./utils/ids.js')
|
||||||
idStats.Load()
|
idStats.Load()
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ const dataFile = './publicDirs/qminingPublic/data.json'
|
||||||
const recDataFile = './stats/recdata'
|
const recDataFile = './stats/recdata'
|
||||||
|
|
||||||
const logger = require('../utils/logger.js')
|
const logger = require('../utils/logger.js')
|
||||||
const { searchData } = require('../utils/classes.js')
|
const { searchData, createQuestion } = require('../utils/classes.js')
|
||||||
const idStats = require('../utils/ids.js')
|
const idStats = require('../utils/ids.js')
|
||||||
const utils = require('../utils/utils.js')
|
const utils = require('../utils/utils.js')
|
||||||
const { addQuestion, getSubjNameWithoutYear } = require('./classes.js')
|
const { addQuestion, getSubjNameWithoutYear } = require('./classes.js')
|
||||||
|
@ -72,7 +72,8 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun, user) {
|
||||||
logger.DebugLog('recievedData JSON parsed', 'actions', 1)
|
logger.DebugLog('recievedData JSON parsed', 'actions', 1)
|
||||||
logger.DebugLog(data, 'actions', 3)
|
logger.DebugLog(data, 'actions', 3)
|
||||||
let allQLength = data.quiz.length
|
let allQLength = data.quiz.length
|
||||||
|
const questionSearchPromises = []
|
||||||
|
data.quiz.forEach((question) => {
|
||||||
logger.DebugLog('Question:', 'actions', 2)
|
logger.DebugLog('Question:', 'actions', 2)
|
||||||
logger.DebugLog(question, 'actions', 2)
|
logger.DebugLog(question, 'actions', 2)
|
||||||
let currentQuestion = createQuestion(
|
let currentQuestion = createQuestion(
|
||||||
|
@ -86,11 +87,17 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun, user) {
|
||||||
3
|
3
|
||||||
)
|
)
|
||||||
logger.DebugLog(currentQuestion, 'actions', 3)
|
logger.DebugLog(currentQuestion, 'actions', 3)
|
||||||
|
questionSearchPromises.push(searchData(qdb, currentQuestion, data.subj))
|
||||||
|
})
|
||||||
|
|
||||||
searchData(qdb, currentQuestion, data.subj)
|
Promise.all(questionSearchPromises)
|
||||||
.then((allQuestions) => {
|
.then((results) => {
|
||||||
logger.DebugLog('Message from processData', 'actions', 1)
|
const allQuestions = [] // all new questions here that do not have result
|
||||||
logger.DebugLog(allQuestions, 'actions', 3)
|
results.forEach((result) => {
|
||||||
|
if (result.length === 0) {
|
||||||
|
allQuestions.push(result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let color = logger.GetColor('green')
|
let color = logger.GetColor('green')
|
||||||
|
@ -158,9 +165,11 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun, user) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
logger.Log('Process Data Worker error!', logger.GetColor('redbg'))
|
logger.Log(
|
||||||
|
'Error while searching for questions in ProcessIncomingRequest!',
|
||||||
|
logger.GetColor('redbg')
|
||||||
|
)
|
||||||
console.error(err)
|
console.error(err)
|
||||||
reject(err)
|
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
|
|
|
@ -4,9 +4,16 @@ const {
|
||||||
parentPort,
|
parentPort,
|
||||||
workerData,
|
workerData,
|
||||||
} = require('worker_threads')
|
} = require('worker_threads')
|
||||||
const searchDataWorkerFile = './src/utils/searchData.js'
|
|
||||||
|
|
||||||
const logger = require('../../utils/logger.js')
|
const logger = require('./logger.js')
|
||||||
|
|
||||||
|
const searchDataWorkerFile = './src/utils/classes.js'
|
||||||
|
|
||||||
|
const assert = (val) => {
|
||||||
|
if (!val) {
|
||||||
|
throw new Error('Assertion failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const commonUselessAnswerParts = [
|
const commonUselessAnswerParts = [
|
||||||
'A helyes válasz az ',
|
'A helyes válasz az ',
|
||||||
|
@ -18,17 +25,12 @@ const commonUselessAnswerParts = [
|
||||||
'The correct answer is:',
|
'The correct answer is:',
|
||||||
"'",
|
"'",
|
||||||
]
|
]
|
||||||
|
|
||||||
const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
|
const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
|
||||||
const specialChars = ['&', '\\+']
|
const specialChars = ['&', '\\+']
|
||||||
const lengthDiffMultiplier = 10 /* Percent minus for length difference */
|
const lengthDiffMultiplier = 10 /* Percent minus for length difference */
|
||||||
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
|
const minMatchAmmount = 60 /* Minimum ammount to consider that two questions match during answering */
|
||||||
|
|
||||||
const assert = (val) => {
|
|
||||||
if (!val) {
|
|
||||||
throw new Error('Assertion failed')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
// String Utils
|
// String Utils
|
||||||
// ---------------------------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------------------------
|
||||||
|
@ -153,16 +155,22 @@ function simplifyAnswer(value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function simplifyQuestion(question) {
|
function simplifyQuestion(question) {
|
||||||
if (typeof q === 'string') {
|
if (typeof question === 'string') {
|
||||||
return simplifyQA(question, [removeSpecialChars, removeUnnecesarySpaces])
|
return simplifyQA(question, [
|
||||||
|
removeSpecialChars,
|
||||||
|
removeUnnecesarySpaces,
|
||||||
|
removeAnswerLetters,
|
||||||
|
])
|
||||||
} else {
|
} else {
|
||||||
question.Q = simplifyQA(question.Q, [
|
question.Q = simplifyQA(question.Q, [
|
||||||
removeSpecialChars,
|
removeSpecialChars,
|
||||||
removeUnnecesarySpaces,
|
removeUnnecesarySpaces,
|
||||||
|
removeAnswerLetters,
|
||||||
])
|
])
|
||||||
question.A = simplifyQA(question.A, [
|
question.A = simplifyQA(question.A, [
|
||||||
removeSpecialChars,
|
removeSpecialChars,
|
||||||
removeUnnecesarySpaces,
|
removeUnnecesarySpaces,
|
||||||
|
removeAnswerLetters,
|
||||||
])
|
])
|
||||||
return question
|
return question
|
||||||
}
|
}
|
||||||
|
@ -374,6 +382,7 @@ function searchData(data, question, subjName, questionData) {
|
||||||
logger.DebugLog('No subject name as param!', 'qdb search', 1)
|
logger.DebugLog('No subject name as param!', 'qdb search', 1)
|
||||||
}
|
}
|
||||||
question = simplifyQuestion(question)
|
question = simplifyQuestion(question)
|
||||||
|
console.log('RESULT: ' + question)
|
||||||
|
|
||||||
const worker = new Worker(searchDataWorkerFile, {
|
const worker = new Worker(searchDataWorkerFile, {
|
||||||
workerData: { data, subjName, question, questionData },
|
workerData: { data, subjName, question, questionData },
|
||||||
|
@ -443,6 +452,7 @@ function dataToString(data) {
|
||||||
return result.join('\n\n')
|
return result.join('\n\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
if (!isMainThread) {
|
if (!isMainThread) {
|
||||||
const { data, subjName, question, questionData } = workerData
|
const { data, subjName, question, questionData } = workerData
|
||||||
let result = []
|
let result = []
|
||||||
|
@ -493,7 +503,9 @@ if (!isMainThread) {
|
||||||
})
|
})
|
||||||
|
|
||||||
parentPort.postMessage(result)
|
parentPort.postMessage(result)
|
||||||
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
minMatchAmmount,
|
minMatchAmmount,
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
const { isMainThread, parentPort, workerData } = require('worker_threads')
|
|
||||||
const logger = require('../utils/logger.js')
|
|
||||||
const { createQuestion, searchData } = require('./classes.js')
|
|
||||||
|
|
||||||
const minMatchAmmountToAdd = 90 // FIXME: test this value
|
|
||||||
|
|
||||||
if (!isMainThread) {
|
|
||||||
logger.DebugLog('Starting worker thread', 'processdata', 1)
|
|
||||||
logger.DebugLog(workerData, 'processdata', 3)
|
|
||||||
|
|
||||||
parentPort.postMessage(ProcessData(workerData.data, workerData.qdb))
|
|
||||||
} else {
|
|
||||||
logger.Log(
|
|
||||||
'Porcess data should not run on main thread!',
|
|
||||||
logger.GetColor('redbg')
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
function ProcessData(data, qdb) {
|
|
||||||
let allQuestions = []
|
|
||||||
|
|
||||||
data.quiz.forEach((question) => {
|
|
||||||
logger.DebugLog('Question:', 'actions', 2)
|
|
||||||
logger.DebugLog(question, 'actions', 2)
|
|
||||||
let currentQuestion = createQuestion(question.Q, question.A, question.data)
|
|
||||||
logger.DebugLog('Searching for question in subj ' + data.subj, 'actions', 3)
|
|
||||||
logger.DebugLog(currentQuestion, 'actions', 3)
|
|
||||||
|
|
||||||
let sames = searchData(qdb, currentQuestion, data.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(currentQuestion)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return allQuestions
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
const { isMainThread, parentPort, workerData } = require('worker_threads')
|
|
||||||
const logger = require('../utils/logger.js')
|
|
||||||
const { searchData } = require('./classes.js')
|
|
||||||
|
|
||||||
if (!isMainThread) {
|
|
||||||
logger.DebugLog('Starting worker thread', 'searchdata', 1)
|
|
||||||
logger.DebugLog(workerData, 'searchdata', 3)
|
|
||||||
const { data, question, subj, recData } = workerData
|
|
||||||
|
|
||||||
parentPort.postMessage(searchData(data, question, subj, recData))
|
|
||||||
} else {
|
|
||||||
logger.Log(
|
|
||||||
'Porcess data should not run on main thread!',
|
|
||||||
logger.GetColor('redbg')
|
|
||||||
)
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue