Possible answers penalty fixes, logging imporvements, added tests

This commit is contained in:
mrfry 2022-03-23 16:10:16 +01:00
parent 39cb92308d
commit f5ad460e24
8 changed files with 396 additions and 48 deletions

View file

@ -20,7 +20,8 @@
"dev": "npm run build && NS_THREAD_COUNT=2 NS_DEVEL=1 NS_NOUSER=1 NS_LOGLEVEL=1 node --inspect ./dist/server.js", "dev": "npm run build && NS_THREAD_COUNT=2 NS_DEVEL=1 NS_NOUSER=1 NS_LOGLEVEL=1 node --inspect ./dist/server.js",
"build": "tsc && bash -c './scripts/postBuild.sh'", "build": "tsc && bash -c './scripts/postBuild.sh'",
"export": "tsc && bash -c './scripts/postBuild.sh'", "export": "tsc && bash -c './scripts/postBuild.sh'",
"test": "jest --detectOpenHandles" "test": "NS_NOLOG=1 NS_THREAD_COUNT=1 jest --detectOpenHandles",
"test-debug": "NS_NOLOG=1 NS_THREAD_COUNT=1 node --inspect node_modules/.bin/jest --watch --runInBand src/tests/*.test.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/better-sqlite3": "^7.5.0", "@types/better-sqlite3": "^7.5.0",

View file

@ -0,0 +1,308 @@
import {
setNoPossibleAnswersPenalties,
SearchResultQuestion,
noPossibleAnswerMatchPenalty,
} from '../utils/classes'
import { Question } from '../types/basicTypes'
const matchPercent = 100
const questionWithNormalPossibleAnswers: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'rubber duck' },
{ type: 'txt', val: 'super laptop' },
{ type: 'txt', val: 'nothing in particular' },
{ type: 'txt', val: 'something giberish' },
],
},
}
const questionWithNormalPossibleAnswersWithLabels: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'a) nothing in particular' },
{ type: 'txt', val: 'b) super laptop' },
{ type: 'txt', val: 'c) something giberish' },
{ type: 'txt', val: 'd) rubber duck' },
],
},
}
const questionWithNormalPossibleAnswers2: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'rubber duck' },
{ type: 'txt', val: 'cat' },
{ type: 'txt', val: 'nothing in particular' },
{ type: 'txt', val: 'dog' },
],
},
}
const questionWithNormalPossibleAnswers3: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'rubber duck 2' },
{ type: 'txt', val: 'whale' },
{ type: 'txt', val: 'nothing in particular 2' },
{ type: 'txt', val: 'sea lion' },
],
},
}
const questionWithNormalPossibleAnswers4: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'rubber duck' },
{ type: 'txt', val: 'super laptop' },
],
},
}
const questionWithSimilarPossibleAnswers: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'asd' },
{ type: 'txt', val: 'basd' },
{ type: 'txt', val: 'aaa' },
{ type: 'txt', val: 'bbb' },
],
},
}
const questionWithTrueFalsePossibleAnser: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
possibleAnswers: [
{ type: 'txt', val: 'true' },
{ type: 'txt', val: 'false' },
],
},
}
const questionWithNoPossibleAnswer: Question = {
Q: 'asd',
A: 'asd',
data: {
type: 'simple',
},
}
const resNormal: SearchResultQuestion = {
q: questionWithNormalPossibleAnswers,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resNormal2: SearchResultQuestion = {
q: questionWithNormalPossibleAnswers2,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resNormal3: SearchResultQuestion = {
q: questionWithNormalPossibleAnswers3,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resNormal4: SearchResultQuestion = {
q: questionWithNormalPossibleAnswers4,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resSimilar: SearchResultQuestion = {
q: questionWithSimilarPossibleAnswers,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resTrueFalse: SearchResultQuestion = {
q: questionWithTrueFalsePossibleAnser,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const resNoPossibleAnswer: SearchResultQuestion = {
q: questionWithNoPossibleAnswer,
match: matchPercent,
detailedMatch: {
qMatch: matchPercent,
aMatch: matchPercent,
dMatch: matchPercent,
matchedSubjName: 'testSubj',
avg: matchPercent,
},
}
const testFunction = (
question: Question,
searchResult: SearchResultQuestion[],
index: number
) => {
const updated = setNoPossibleAnswersPenalties(
question.data.possibleAnswers,
searchResult
)
updated.forEach((x, i) => {
if (i !== index) {
expect(x.match).toBe(matchPercent - noPossibleAnswerMatchPenalty)
expect(x.detailedMatch.qMatch).toBe(
matchPercent - noPossibleAnswerMatchPenalty
)
} else {
expect(x.match).toBe(100)
expect(x.detailedMatch.qMatch).toBe(100)
}
})
return updated
}
test('Possible answer penalty applies correctly (normal possible answers)', () => {
testFunction(
questionWithNormalPossibleAnswers,
[
resNormal,
resNormal2,
resNormal3,
resNormal4,
resSimilar,
resTrueFalse,
resNoPossibleAnswer,
],
0
)
})
test('Possible answer penalty applies correctly (normal possible answers, with labels)', () => {
testFunction(
questionWithNormalPossibleAnswersWithLabels,
[
resNormal,
resNormal2,
resNormal3,
resNormal4,
resSimilar,
resTrueFalse,
resNoPossibleAnswer,
],
0
)
})
test('Possible answer penalty applies correctly (similar possible answers)', () => {
testFunction(
questionWithSimilarPossibleAnswers,
[
resNormal,
resNormal2,
resNormal3,
resNormal4,
resSimilar,
resTrueFalse,
resNoPossibleAnswer,
],
4
)
})
test('Possible answer penalty applies correctly (true false possible answers)', () => {
testFunction(
questionWithTrueFalsePossibleAnser,
[
resNormal,
resNormal2,
resNormal3,
resNormal4,
resSimilar,
resTrueFalse,
resNoPossibleAnswer,
],
5
)
})
test('Possible answer penalty applies correctly (no possible answers)', () => {
const updated = setNoPossibleAnswersPenalties(
questionWithNoPossibleAnswer.data.possibleAnswers,
[
resNormal,
resNormal2,
resNormal3,
resNormal4,
resSimilar,
resTrueFalse,
resNoPossibleAnswer,
]
)
updated.forEach((x) => {
expect(x.match).toBe(100)
expect(x.detailedMatch.qMatch).toBe(100)
})
})
test('Possible answer penalty applies correctly (empty searchResult)', () => {
const updated = testFunction(questionWithTrueFalsePossibleAnser, [], 0)
expect(updated.length).toBe(0)
})

View file

@ -31,8 +31,9 @@ export interface QuestionData {
images?: Array<string> images?: Array<string>
hashedImages?: Array<string> hashedImages?: Array<string>
possibleAnswers?: Array<{ possibleAnswers?: Array<{
text: string type: string
selectedByUser: boolean val: string
selectedByUser?: boolean
}> }>
} }
@ -96,7 +97,11 @@ export interface User {
export interface Request<T = any> extends express.Request { export interface Request<T = any> extends express.Request {
body: T body: T
cookies: any cookies: any
session: any session: {
user?: User
sessionID?: string
isException?: boolean
}
files: any files: any
query: { [key: string]: string } query: { [key: string]: string }
} }

View file

@ -204,7 +204,7 @@ function processIncomingRequestUsingDb(
const add = result.result.every((res: SearchResultQuestion) => { const add = result.result.every((res: SearchResultQuestion) => {
return res.match < minMatchAmmountToAdd return res.match < minMatchAmmountToAdd
}) })
if (add) { if (add && !result.error) {
allQuestions.push(recievedQuestions[i]) allQuestions.push(recievedQuestions[i])
} }
}) })

View file

@ -33,6 +33,7 @@ export interface WorkerResult {
msg: string msg: string
workerIndex: number workerIndex: number
result?: SearchResultQuestion[] result?: SearchResultQuestion[]
error?: boolean
} }
interface DetailedMatch { interface DetailedMatch {
@ -63,7 +64,7 @@ const commonUselessAnswerParts = [
// const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.'] // const commonUselessStringParts = [',', '\\.', ':', '!', '\\+', '\\s*\\.']
/* Percent minus for length difference */ /* Percent minus for length difference */
const lengthDiffMultiplier = 10 const lengthDiffMultiplier = 10
const noPossibleAnswerMatchPenalty = 5 export const noPossibleAnswerMatchPenalty = 5
/* Minimum ammount to consider that two questions match during answering */ /* Minimum ammount to consider that two questions match during answering */
const minMatchAmmount = 75 const minMatchAmmount = 75
const magicNumber = 0.7 // same as minMatchAmmount, but /100 const magicNumber = 0.7 // same as minMatchAmmount, but /100
@ -574,39 +575,52 @@ function doSearch(
} }
function setNoPossibleAnswersPenalties( function setNoPossibleAnswersPenalties(
possibleAnswers: QuestionData['possibleAnswers'], questionPossibleAnswers: QuestionData['possibleAnswers'],
result: SearchResultQuestion[] results: SearchResultQuestion[]
): SearchResultQuestion[] { ): SearchResultQuestion[] {
if (!Array.isArray(possibleAnswers)) { if (!Array.isArray(questionPossibleAnswers)) {
return result return results
} }
const noneHasPossibleAnswers = result.every((x) => { const noneHasPossibleAnswers = results.every((x) => {
return !Array.isArray(x.q.data.possibleAnswers) return !Array.isArray(x.q.data.possibleAnswers)
}) })
if (noneHasPossibleAnswers) return result if (noneHasPossibleAnswers) return results
let possibleAnswerMatch = false let possibleAnswerMatch = false
const updated = result.map((result) => { const updated = results.map((result) => {
const hasMatch = result.q.data.possibleAnswers.some((possibleAnswer) => { const matchCount = Array.isArray(result.q.data.possibleAnswers)
return possibleAnswers.some((questionPossibleAnswer) => { ? result.q.data.possibleAnswers.filter((resultPossibleAnswer) => {
// FIXME: this could be object: questionPossibleAnswer return questionPossibleAnswers.some((questionPossibleAnswer) => {
return questionPossibleAnswer.text.includes(possibleAnswer.text) if (questionPossibleAnswer.val && resultPossibleAnswer.val) {
}) return questionPossibleAnswer.val.includes(
}) resultPossibleAnswer.val
if (hasMatch) { )
} else {
return false
}
})
}).length
: 0
if (matchCount === questionPossibleAnswers.length) {
possibleAnswerMatch = true possibleAnswerMatch = true
return result
} else { } else {
result.match = result.match - noPossibleAnswerMatchPenalty return {
result.detailedMatch.qMatch = ...result,
result.detailedMatch.qMatch - noPossibleAnswerMatchPenalty match: result.match - noPossibleAnswerMatchPenalty,
detailedMatch: {
...result.detailedMatch,
qMatch: result.detailedMatch.qMatch - noPossibleAnswerMatchPenalty,
},
}
} }
return result
}) })
if (possibleAnswerMatch) { if (possibleAnswerMatch) {
return updated return updated
} else { } else {
return result return results
} }
} }
@ -646,6 +660,7 @@ if (!isMainThread) {
}: WorkData = msg.data }: WorkData = msg.data
let searchResult: SearchResultQuestion[] = [] let searchResult: SearchResultQuestion[] = []
let error = false
try { try {
qdbs.forEach((qdb) => { qdbs.forEach((qdb) => {
@ -674,14 +689,21 @@ if (!isMainThread) {
} catch (err) { } catch (err) {
logger.Log('Error in worker thread!', logger.GetColor('redbg')) logger.Log('Error in worker thread!', logger.GetColor('redbg'))
console.error(err) console.error(err)
console.error({ console.error(
subjName: subjName, JSON.stringify(
question: question, {
searchTillMatchPercent: searchTillMatchPercent, subjName: subjName,
searchInAllIfNoResult: searchInAllIfNoResult, question: question,
searchIn: searchIn, searchTillMatchPercent: searchTillMatchPercent,
index: index, searchInAllIfNoResult: searchInAllIfNoResult,
}) searchIn: searchIn,
index: index,
},
null,
2
)
)
error = true
} }
// sorting // sorting
@ -703,6 +725,7 @@ if (!isMainThread) {
}done`, }done`,
workerIndex: workerIndex, workerIndex: workerIndex,
result: sortedResult, result: sortedResult,
error: error,
} }
// ONDONE: // ONDONE:
@ -810,4 +833,5 @@ export {
addQuestion, addQuestion,
dataToString, dataToString,
doSearch, doSearch,
setNoPossibleAnswersPenalties,
} }

View file

@ -52,7 +52,7 @@ let uvData = {} // visit data, but per user
let udvData = {} // visit data, but per user and daily let udvData = {} // visit data, but per user and daily
let writes = 0 let writes = 0
let noLogips: string[] = [] let noLogIds: string[] = []
function getColoredDateString(): string { function getColoredDateString(): string {
const date = new Date() const date = new Date()
@ -86,7 +86,9 @@ function Log(msg: string | object, color?: string): void {
log = getColoredDateString() + delimiter + C(color) + msg + C() log = getColoredDateString() + delimiter + C(color) + msg + C()
} }
console.log(log) if (!process.env.NS_NOLOG) {
console.log(log)
}
utils.AppendToFile( utils.AppendToFile(
typeof log === 'string' ? log : JSON.stringify(log), typeof log === 'string' ? log : JSON.stringify(log),
logDir + logFileName logDir + logFileName
@ -160,20 +162,20 @@ function LogReq(
utils.AppendToFile(defLogs, vlogDir + logFileName) utils.AppendToFile(defLogs, vlogDir + logFileName)
} }
} catch (err) { } catch (err) {
console.log(err) console.error(err)
Log('Error at logging lol', GetColor('redbg')) Log('Error at logging lol', GetColor('redbg'))
} }
} }
function parseNoLogFile(newData: string) { function parseNoLogFile(newData: string) {
noLogips = newData.split('\n') noLogIds = newData.split('\n')
if (noLogips[noLogips.length - 1] === '') { if (noLogIds[noLogIds.length - 1] === '') {
noLogips.pop() noLogIds.pop()
} }
noLogips = noLogips.filter((noLogip) => { noLogIds = noLogIds.filter((noLogId) => {
return noLogip !== '' return noLogId !== ''
}) })
Log('\tNo Log IP-s changed: ' + noLogips.join(', ')) Log('\tNo Log user ID-s changed: ' + noLogIds.join(', '))
} }
function setNoLogReadInterval() { function setNoLogReadInterval() {
@ -219,16 +221,23 @@ function Load(): void {
setNoLogReadInterval() setNoLogReadInterval()
} }
function LogStat(url: string, hostname: string, userId: number): void { function LogStat(url: string, hostname: string, userId: number | string): void {
const nolog = noLogIds.some((noLogId) => {
return noLogId === userId.toString()
})
if (nolog) {
return
}
url = hostname + url.split('?')[0] url = hostname + url.split('?')[0]
Inc(url) Inc(url)
AddUserIdStat(userId) AddUserIdStat(userId.toString())
IncUserStat(userId) IncUserStat(userId.toString())
AddVisitStat(url) AddVisitStat(url)
Save() Save()
} }
function IncUserStat(userId: number) { function IncUserStat(userId: string) {
try { try {
if (uvData[userId] === undefined) { if (uvData[userId] === undefined) {
uvData[userId] = 0 uvData[userId] = 0
@ -240,7 +249,7 @@ function IncUserStat(userId: number) {
} }
} }
function AddUserIdStat(userId: number) { function AddUserIdStat(userId: string) {
try { try {
const date = new Date() const date = new Date()
const now = const now =

@ -1 +1 @@
Subproject commit 4f82b9d8c9c32ebbd9d73b951fc1db26ad1ceb22 Subproject commit 4748c23769d16450731562bb9ffd1553ce2eb704

View file

@ -26,6 +26,7 @@
"src/**/*" "src/**/*"
], ],
"exclude": [ "exclude": [
"src/tests/",
"node_modules", "node_modules",
"submodules", "submodules",
"devel", "devel",