From 98f2cc46083bea6ea33fa2bc6e772e040688bce0 Mon Sep 17 00:00:00 2001 From: mrfry Date: Wed, 10 Feb 2021 17:29:17 +0100 Subject: [PATCH] Searching in matching questiondbs first --- src/modules/api/api.ts | 82 ++++++++++++++++++++++++++++++++--------- src/utils/actions.ts | 4 +- src/utils/classes.ts | 2 +- submodules/qmining-page | 2 +- 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index 89d70a1..6a3beec 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -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: { diff --git a/src/utils/actions.ts b/src/utils/actions.ts index c938e68..b7c23b1 100755 --- a/src/utils/actions.ts +++ b/src/utils/actions.ts @@ -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( diff --git a/src/utils/classes.ts b/src/utils/classes.ts index 06eb9a9..6ec914f 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -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, diff --git a/submodules/qmining-page b/submodules/qmining-page index 6251456..e5138ad 160000 --- a/submodules/qmining-page +++ b/submodules/qmining-page @@ -1 +1 @@ -Subproject commit 6251456781998bfc4fad535ccdc9e90a56094001 +Subproject commit e5138adf663fc6374c0e4dce43fc665f4cac3ed7