Added Request type with cookies and session, and fixed stuff that it broke

This commit is contained in:
mrfry 2020-12-16 17:22:15 +01:00
parent 865e97a754
commit 3b902d736f
9 changed files with 117 additions and 95 deletions

View file

@ -215,16 +215,20 @@ function createQuestion(
): Question {
return {
Q: simplifyQuestion(question),
A: simplifyAnswer(answer),
A: answer ? simplifyAnswer(answer) : null,
data,
}
}
function compareImage(data: QuestionData, data2: QuestionData) {
// TODO: img comparing (hashed images vs images)
const imgs1 = data.hashedImages ? data.hashedImages : data.images
const imgs2 = data2.hashedImages ? data2.hashedImages : data2.images
return compareString(imgs1.join(' '), imgs2.join(' '))
if (data.hashedImages && data2.hashedImages) {
return compareString(
data.hashedImages.join(' '),
data.hashedImages.join(' ')
)
} else {
return compareString(data.images.join(' '), data2.images.join(' ')) - 10
}
}
function compareData(q1: Question, q2: Question) {
@ -273,24 +277,15 @@ function compareQuestionObj(
assert(q1)
assert(typeof q1 === 'object')
assert(q2)
let qObj
assert(typeof q2 === 'object')
if (typeof q2 === 'string') {
qObj = {
Q: q2,
data: data,
}
} else {
qObj = q2
}
const qMatch = compareQuestion(q1, qObj)
const aMatch = compareAnswer(q1, qObj)
const qMatch = compareQuestion(q1, q2)
const aMatch = q2.A ? compareAnswer(q1, q2) : 0
// -1 if botth questions are simple
const dMatch = compareData(q1, qObj)
const dMatch = compareData(q1, q2)
let avg = -1
if (qObj.A) {
if (q2.A) {
if (dMatch === -1) {
avg = (qMatch + aMatch) / 2
} else {
@ -427,33 +422,46 @@ function searchDatas(
)
}
// FIXME: remove questionData, make question only Question type
function prepareQuestion(
question: string | Question,
data: string | QuestionData
): Question {
let preparedQuestion: Question
if (typeof question === 'object') {
preparedQuestion = question
} else {
// FIXME data was checkedif its null, it should be never null. check if its really never null
const parsedData = typeof data === 'object' ? data : JSON.parse(data)
preparedQuestion = createQuestion(question, null, parsedData)
}
return simplifyQuestion(preparedQuestion)
}
function searchData(
qdb: QuestionDb,
question: any,
question: Question | string,
subjName: string,
questionData?: QuestionData
questionData?: QuestionData | string
): Promise<SearchResult> {
// FIXME subjName was checkedif its null, it should be never null. check if its really never null
return new Promise((resolve, reject) => {
assert(question)
logger.DebugLog('Searching for question', 'qdb search', 1)
logger.DebugLog('Question:', 'qdb search', 2)
logger.DebugLog(question, 'qdb search', 2)
logger.DebugLog(`Subject name: ${subjName}`, 'qdb search', 2)
logger.DebugLog('Data:', 'qdb search', 2)
logger.DebugLog(questionData || question.data, 'qdb search', 2)
if (!questionData) {
questionData = question.data || { type: 'simple' }
}
if (!subjName) {
subjName = ''
logger.DebugLog('No subject name as param!', 'qdb search', 1)
}
question = simplifyQuestion(question)
const preparedQuestion = prepareQuestion(question, questionData)
logger.DebugLog('Question:', 'qdb search', 2)
logger.DebugLog(preparedQuestion, 'qdb search', 2)
logger.DebugLog(`Subject name: ${subjName}`, 'qdb search', 2)
const worker = workerTs(searchDataWorkerFile, {
workerData: { data: qdb.data, subjName, question, questionData },
workerData: {
data: qdb.data,
subjName,
question: preparedQuestion,
questionData,
},
})
worker.on('error', (err) => {
@ -474,6 +482,14 @@ function searchData(
})
worker.on('message', (result) => {
// TODO: remove (?)
if (typeof result === 'string') {
try {
console.log(JSON.parse(result))
} catch (err) {
console.log(result)
}
}
logger.DebugLog(`Worker message arrived`, 'worker', 2)
logger.DebugLog(result, 'worker', 3)
logger.DebugLog(`Question result length: ${result.length}`, 'ask', 1)