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

@ -8,10 +8,16 @@ interface Options {
export default function(options: Options): any { export default function(options: Options): any {
const loggableKeywords = options ? options.loggableKeywords : undefined const loggableKeywords = options ? options.loggableKeywords : undefined
const loggableModules = options ? options.loggableModules : undefined const loggableModules = options ? options.loggableModules : undefined
const exceptions = options ? options.exceptions : []
return function(req, res, next) { return function(req, res, next) {
res.on('finish', function() { res.on('finish', function() {
if (req.url.includes('_next/static')) { // TODO: test this
const isException = exceptions.some((ex) => {
return req.url.includes(ex)
})
if (isException) {
return return
} }
@ -26,7 +32,6 @@ export default function(options: Options): any {
console.log(req.headers) console.log(req.headers)
} }
// fixme: regexp includes checking
const hasLoggableKeyword = const hasLoggableKeyword =
loggableKeywords && loggableKeywords &&
loggableKeywords.some((keyword) => { loggableKeywords.some((keyword) => {

View file

@ -1166,7 +1166,7 @@ function GetApp(): ModuleType {
question: question, question: question,
subjName: subj, subjName: subj,
questionData: recData, questionData: recData,
firstMatchOnly: true, // search till the first match only searchInAllIfNoResult: true,
}, },
}) })
.then((taskResult) => { .then((taskResult) => {

View file

@ -148,12 +148,14 @@ app.use(
// origin: [ /\.frylabs\.net$/ ] // origin: [ /\.frylabs\.net$/ ]
}) })
) )
const cookieSecret = uuidv4() const cookieSecret = uuidv4()
app.use(cookieParser(cookieSecret)) app.use(cookieParser(cookieSecret))
app.use( app.use(
reqlogger({ reqlogger({
loggableKeywords: ['news.json'], loggableKeywords: ['news.json'],
loggableModules: ['dataeditor'], loggableModules: ['dataeditor'],
exceptions: ['stable.user.js?up', '_next/static'],
}) })
) )

View file

@ -32,7 +32,7 @@ import { QuestionDb, Question, User, DataFile } from '../types/basicTypes'
// if a recievend question doesnt match at least this % to any other question in the db it gets // if a recievend question doesnt match at least this % to any other question in the db it gets
// added to db // added to db
const minMatchToAmmountToAdd = 90 const minMatchAmmountToAdd = 90
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
let currWrites = 0 let currWrites = 0
@ -166,6 +166,7 @@ function processIncomingRequestUsingDb(
) )
logger.DebugLog(currentQuestion, 'actions', 3) logger.DebugLog(currentQuestion, 'actions', 3)
recievedQuestions.push(currentQuestion) recievedQuestions.push(currentQuestion)
// This here searches only in relevant subjects, and not all subjects
questionSearchPromises.push( questionSearchPromises.push(
doALongTask({ doALongTask({
type: 'work', type: 'work',
@ -174,6 +175,7 @@ function processIncomingRequestUsingDb(
qdb: qdb.data, qdb: qdb.data,
question: currentQuestion, question: currentQuestion,
subjName: recievedData.subj, subjName: recievedData.subj,
searchTillMatchPercent: minMatchAmmountToAdd,
}, },
}) })
) )
@ -184,7 +186,7 @@ function processIncomingRequestUsingDb(
const allQuestions = [] // all new questions here that do not have result const allQuestions = [] // all new questions here that do not have result
results.forEach((result, i) => { results.forEach((result, i) => {
const add = result.result.every((res) => { const add = result.result.every((res) => {
return res.match < minMatchToAmmountToAdd return res.match < minMatchAmmountToAdd
}) })
if (add) { if (add) {
allQuestions.push(recievedQuestions[i]) allQuestions.push(recievedQuestions[i])

View file

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