Files
mrfrys-node-server/src/utils/processData.js
T

47 lines
1.5 KiB
JavaScript

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
}