mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2026-04-29 03:37:38 +02:00
Added worker to search for stuff in db #10
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const dataFile = './qminingPublic/data.json'
|
||||
const recDataFile = './stats/recdata'
|
||||
|
||||
const {
|
||||
Worker,
|
||||
isMainThread,
|
||||
parentPort,
|
||||
workerData,
|
||||
} = require('worker_threads')
|
||||
const logger = require('../utils/logger.js')
|
||||
const actions = require('../utils/actions.js')
|
||||
const classes = require('./classes.js')
|
||||
classes.initLogger(logger.DebugLog)
|
||||
|
||||
const minMatchAmmountToAdd = 90 // FIXME: test this value
|
||||
|
||||
if (!isMainThread) {
|
||||
logger.DebugLog('Starting worker thread', 'processdata', 1)
|
||||
console.log(workerData)
|
||||
|
||||
parentPort.postMessage(
|
||||
ProcessData(workerData.data, actions.LoadJSONFromObject(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 = new classes.Question(
|
||||
question.Q,
|
||||
question.A,
|
||||
question.data
|
||||
)
|
||||
logger.DebugLog('Searching for question in subj ' + data.subj, 'actions', 3)
|
||||
logger.DebugLog(currentQuestion, 'actions', 3)
|
||||
|
||||
let sames = qdb.Search(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
|
||||
}
|
||||
Reference in New Issue
Block a user