Workers can now search in multiple specified db-s

This commit is contained in:
mrfry 2020-12-19 12:07:31 +01:00
parent 68dcbff846
commit 19b8fdc5ab
5 changed files with 57 additions and 52 deletions

View file

@ -34,8 +34,9 @@ import {
logResult,
backupData,
loadJSON,
RecievedData,
getQuestionDbsWithoutFunct,
RecievedData,
Result,
} from '../../utils/actions'
import dbtools from '../../utils/dbtools'
import auth from '../../middlewares/auth.middleware'
@ -705,8 +706,6 @@ function GetApp(): ModuleType {
return new Date(a).getTime() - new Date(b).getTime()
})
// TODO: check if sort works, and only the first few gets deleted.
const diff = existingSessions.length - minimumAlowwedSessions
if (diff > 0) {
logger.Log(
@ -1030,16 +1029,25 @@ function GetApp(): ModuleType {
try {
processIncomingRequest(req.body, questionDbs, dryRun, user)
.then((resultArray) => {
.then((resultArray: Array<Result>) => {
logResult(req.body, resultArray, user.id, dryRun)
const totalNewQuestions = resultArray.reduce((acc, sres) => {
return acc + sres.newQuestions
}, 0)
res.json({
success: resultArray.length > 0,
newQuestions: resultArray,
totalNewQuestions: totalNewQuestions,
})
msgAllWorker({
qdbs: getQuestionDbsWithoutFunct(questionDbs),
type: 'update',
})
if (totalNewQuestions > 0) {
msgAllWorker({
qdbs: getQuestionDbsWithoutFunct(questionDbs),
type: 'update',
})
}
})
.catch((err) => {
logger.Log(
@ -1078,48 +1086,28 @@ function GetApp(): ModuleType {
const question = req.query.q
const recData: any = req.query.data
const promises = []
questionDbs.map((qdb) => {
promises.push(
doALongTask({
type: 'work',
data: {
searchIn: [qdb.index], // TODO: search in all
question: question,
subjName: subj,
questionData: recData,
},
})
)
doALongTask({
type: 'work',
data: {
searchIn: 'all',
question: question,
subjName: subj,
questionData: recData,
},
})
Promise.all(promises)
.then((result) => {
console.log(result)
try {
const mergedResult = result.reduce((acc, dbRes) => {
return [...acc, ...dbRes.result]
}, [])
const sortedResult = mergedResult.sort((q1, q2) => {
if (q1.match < q2.match) {
return 1
} else if (q1.match > q2.match) {
return -1
} else {
return 0
}
})
res.json({
result: sortedResult,
result: result,
success: true,
})
logger.DebugLog(
`Question result length: ${mergedResult.length}`,
`Question result length: ${result.length}`,
'ask',
1
)
logger.DebugLog(mergedResult, 'ask', 2)
logger.DebugLog(result, 'ask', 2)
} catch (err) {
console.error(err)
logger.Log(