Reqlogger exceptions, searchig: search till match percent and search in other subjs

This commit is contained in:
mrfry 2021-01-08 13:44:27 +01:00
parent bf4bdfb249
commit 64f41d6748
5 changed files with 61 additions and 46 deletions

View file

@ -319,7 +319,7 @@ function searchQuestion(
subj: Subject,
question: Question,
subjName: string,
firstMatchOnly?: Boolean
searchTillMatchPercent?: number
) {
assert(question)
@ -341,7 +341,7 @@ function searchQuestion(
})
}
if (firstMatchOnly && percent.avg === 100) {
if (searchTillMatchPercent && percent.avg >= searchTillMatchPercent) {
return false
}
@ -388,6 +388,7 @@ function addQuestion(
assert(typeof question === 'object')
let i = 0
// FIXME: this only adds to the first matched subject name. Check if this doesnt cause any bugs
while (
i < data.length &&
!subj
@ -443,14 +444,13 @@ function dataToString(data: Array<Subject>): string {
return result.join('\n\n')
}
// ------------------------------------------------------------------------
function doSearch(
data: Array<Subject>,
subjName: string,
question: Question | string,
questionData?: QuestionData,
firstMatchOnly?: Boolean
searchTillMatchPercent?: number,
searchInAllIfNoResult?: Boolean
): any {
let result = []
@ -469,53 +469,47 @@ function doSearch(
subj,
questionToSearch,
subjName,
firstMatchOnly
searchTillMatchPercent
)
result = result.concat(subjRes)
if (firstMatchOnly) {
if (searchTillMatchPercent) {
// TODO: this should return true to continue searching?
return subjRes.every((sr) => {
return sr.match < 100
return sr.match <= searchTillMatchPercent
})
}
return true
}
// TODO: data.every should always return something! true or false?
})
// FIXME: try to remove this? but this is also a good backup plan so idk
// its sufficent to check only result[0].match, since its sorted, and the first one should have
// the highest match
if (
result.length === 0 ||
result[0].match < minMatchToNotSearchOtherSubjects
) {
logger.DebugLog(
'Reqults length is zero when comparing names, trying all subjects',
'searchworker',
1
)
data.every((subj) => {
const subjRes = searchQuestion(
subj,
questionToSearch,
subjName,
firstMatchOnly
)
result = result.concat(subjRes)
if (firstMatchOnly) {
return subjRes.every((sr) => {
return sr.match < 100
})
}
return true
})
if (result.length > 0) {
if (searchInAllIfNoResult) {
// FIXME: dont research subject searched above
if (
result.length === 0 ||
result[0].match < minMatchToNotSearchOtherSubjects
) {
logger.DebugLog(
`FIXME: '${subjName}' gave no result but '' did!`,
'Reqults length is zero when comparing names, trying all subjects',
'searchworker',
1
)
console.error(`FIXME: '${subjName}' gave no result but '' did!`)
data.every((subj) => {
const subjRes = searchQuestion(
subj,
questionToSearch,
subjName,
searchTillMatchPercent
)
result = result.concat(subjRes)
if (searchTillMatchPercent) {
// TODO: this should return true to continue searching?
return subjRes.every((sr) => {
return sr.match < searchTillMatchPercent
})
}
return true
})
}
}
@ -532,6 +526,10 @@ function doSearch(
return result
}
// ---------------------------------------------------------------------------------------------------------
// Multi threaded stuff
// ---------------------------------------------------------------------------------------------------------
if (!isMainThread) {
const { workerIndex } = workerData
let qdbs: Array<any> = workerData.initData
@ -542,7 +540,14 @@ if (!isMainThread) {
parentPort.on('message', (msg) => {
if (msg.type === 'work') {
const { subjName, question, questionData, firstMatchOnly } = msg.data
const {
subjName,
question,
questionData,
searchTillMatchPercent,
searchInAllIfNoResult,
} = msg.data
const index = msg.index
const searchIn = msg.data.searchIn
@ -562,7 +567,8 @@ if (!isMainThread) {
subjName,
question,
questionData,
firstMatchOnly
searchTillMatchPercent,
searchInAllIfNoResult
)
searchResult = [...searchResult, ...res]
}