Searching in matching questiondbs first

This commit is contained in:
mrfry 2021-02-10 17:29:17 +01:00
parent 87f9bbd3a3
commit 98f2cc4608
4 changed files with 68 additions and 22 deletions

View file

@ -919,7 +919,7 @@ function GetApp(): ModuleType {
`No suitable questiondbs found for ${location}, creating a new one...`
)
const newDb: DataFile = {
path: `${location}.json`,
path: `questionDbs/${location}.json`,
name: location,
shouldSearch: {
location: {
@ -968,7 +968,9 @@ function GetApp(): ModuleType {
const user: User = req.session.user
const dryRun = testUsers.includes(user.id)
if (!req.body.location) {
logger.Log('\tbody.location is missing')
logger.Log(
'\tbody.location is missing, client version:' + req.body.version
)
res.json({ msg: 'body.location is missing' })
return
}
@ -1092,13 +1094,14 @@ function GetApp(): ModuleType {
return
}
const subj: any = req.body.subj || ''
// TODO: test if testUrl is undefined
// TODO: test if testUrl is undefined (it shouldnt)
const testUrl = req.body.testUrl
? req.body.testUrl.split('/')[2]
: undefined
writeAskData(req.body)
// every question in a different thread
const resultPromises = req.body.questions.map((question) => {
return getResult(question, subj, null, req.body, testUrl)
})
@ -1156,25 +1159,68 @@ function GetApp(): ModuleType {
}
})
function getDbIndexesToSearchIn(testUrl, trueIfAlways?) {
return testUrl
? questionDbs.reduce((acc, qdb, i) => {
if (shouldSearchDataFile(qdb, testUrl, trueIfAlways)) {
acc.push(i)
}
return acc
}, [])
: []
}
function getResult(question, subj, recData, recievedData, testUrl?) {
return new Promise((resolve) => {
let searchIn = testUrl
? questionDbs.reduce((acc, qdb, i) => {
if (shouldSearchDataFile(qdb, testUrl, false)) {
acc.push(i)
const searchIn = getDbIndexesToSearchIn(testUrl, false)
searchInDbs(
question,
subj,
recData,
recievedData,
searchIn,
testUrl
).then((res: any) => {
if (res.result.length === 0) {
logger.DebugLog(
`No result while searching specific question db ${testUrl}`,
'ask',
1
)
const searchInMore = getDbIndexesToSearchIn(testUrl, true).filter(
(x) => {
return !searchIn.includes(x)
}
return acc
}, [])
: 'all'
)
searchInDbs(
question,
subj,
recData,
recievedData,
searchInMore,
testUrl
).then((res) => {
resolve(res)
})
} else {
resolve(res)
}
})
})
}
if (searchIn.length === 0) {
searchIn = 'all'
}
// console.log(searchIn)
// searchIn could be [0], [1], ... to search every db in different thread. Put this into a
// forEach(qdbs) to achieve this
function searchInDbs(
question,
subj,
recData,
recievedData,
searchIn,
testUrl?
) {
// searchIn could be [0], [1], ... to search every db in different thread. Put this into a
// forEach(qdbs) to achieve this
return new Promise((resolve) => {
doALongTask({
type: 'work',
data: {

View file

@ -298,8 +298,8 @@ export function shouldSearchDataFile(
}
}
// FIXME: always return true if it gets here?
return true
logger.DebugLog(`no suitable dbs for ${testUrl}`, 'shouldSearchDataFile', 1)
return false
}
export function shouldSaveDataFile(

View file

@ -564,7 +564,7 @@ if (!isMainThread) {
try {
qdbs.forEach((qdb) => {
if (searchIn === 'all' || searchIn.includes(qdb.index)) {
if (searchIn.length === 0 || searchIn.includes(qdb.index)) {
const res = doSearch(
qdb.data,
subjName,

@ -1 +1 @@
Subproject commit 6251456781998bfc4fad535ccdc9e90a56094001
Subproject commit e5138adf663fc6374c0e4dce43fc665f4cac3ed7