mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Searching in matching questiondbs first
This commit is contained in:
parent
87f9bbd3a3
commit
98f2cc4608
4 changed files with 68 additions and 22 deletions
|
@ -919,7 +919,7 @@ function GetApp(): ModuleType {
|
||||||
`No suitable questiondbs found for ${location}, creating a new one...`
|
`No suitable questiondbs found for ${location}, creating a new one...`
|
||||||
)
|
)
|
||||||
const newDb: DataFile = {
|
const newDb: DataFile = {
|
||||||
path: `${location}.json`,
|
path: `questionDbs/${location}.json`,
|
||||||
name: location,
|
name: location,
|
||||||
shouldSearch: {
|
shouldSearch: {
|
||||||
location: {
|
location: {
|
||||||
|
@ -968,7 +968,9 @@ function GetApp(): ModuleType {
|
||||||
const user: User = req.session.user
|
const user: User = req.session.user
|
||||||
const dryRun = testUsers.includes(user.id)
|
const dryRun = testUsers.includes(user.id)
|
||||||
if (!req.body.location) {
|
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' })
|
res.json({ msg: 'body.location is missing' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1092,13 +1094,14 @@ function GetApp(): ModuleType {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const subj: any = req.body.subj || ''
|
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
|
const testUrl = req.body.testUrl
|
||||||
? req.body.testUrl.split('/')[2]
|
? req.body.testUrl.split('/')[2]
|
||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
writeAskData(req.body)
|
writeAskData(req.body)
|
||||||
|
|
||||||
|
// every question in a different thread
|
||||||
const resultPromises = req.body.questions.map((question) => {
|
const resultPromises = req.body.questions.map((question) => {
|
||||||
return getResult(question, subj, null, req.body, testUrl)
|
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?) {
|
function getResult(question, subj, recData, recievedData, testUrl?) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let searchIn = testUrl
|
const searchIn = getDbIndexesToSearchIn(testUrl, false)
|
||||||
? questionDbs.reduce((acc, qdb, i) => {
|
|
||||||
if (shouldSearchDataFile(qdb, testUrl, false)) {
|
searchInDbs(
|
||||||
acc.push(i)
|
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
|
)
|
||||||
}, [])
|
searchInDbs(
|
||||||
: 'all'
|
question,
|
||||||
|
subj,
|
||||||
|
recData,
|
||||||
|
recievedData,
|
||||||
|
searchInMore,
|
||||||
|
testUrl
|
||||||
|
).then((res) => {
|
||||||
|
resolve(res)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
resolve(res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (searchIn.length === 0) {
|
function searchInDbs(
|
||||||
searchIn = 'all'
|
question,
|
||||||
}
|
subj,
|
||||||
|
recData,
|
||||||
// console.log(searchIn)
|
recievedData,
|
||||||
|
searchIn,
|
||||||
// searchIn could be [0], [1], ... to search every db in different thread. Put this into a
|
testUrl?
|
||||||
// forEach(qdbs) to achieve this
|
) {
|
||||||
|
// 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({
|
doALongTask({
|
||||||
type: 'work',
|
type: 'work',
|
||||||
data: {
|
data: {
|
||||||
|
|
|
@ -298,8 +298,8 @@ export function shouldSearchDataFile(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: always return true if it gets here?
|
logger.DebugLog(`no suitable dbs for ${testUrl}`, 'shouldSearchDataFile', 1)
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function shouldSaveDataFile(
|
export function shouldSaveDataFile(
|
||||||
|
|
|
@ -564,7 +564,7 @@ if (!isMainThread) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
qdbs.forEach((qdb) => {
|
qdbs.forEach((qdb) => {
|
||||||
if (searchIn === 'all' || searchIn.includes(qdb.index)) {
|
if (searchIn.length === 0 || searchIn.includes(qdb.index)) {
|
||||||
const res = doSearch(
|
const res = doSearch(
|
||||||
qdb.data,
|
qdb.data,
|
||||||
subjName,
|
subjName,
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 6251456781998bfc4fad535ccdc9e90a56094001
|
Subproject commit e5138adf663fc6374c0e4dce43fc665f4cac3ed7
|
Loading…
Add table
Add a link
Reference in a new issue