mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Old question removing and updating
This commit is contained in:
parent
4305fe2023
commit
5c2b46f2a3
7 changed files with 440 additions and 36 deletions
|
@ -17,6 +17,7 @@
|
|||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------------- */
|
||||
// FIXME: this should be renamed to worker.ts or something
|
||||
|
||||
import { isMainThread, parentPort, workerData } from 'worker_threads'
|
||||
|
||||
|
@ -27,12 +28,13 @@ import {
|
|||
QuestionDb,
|
||||
Subject,
|
||||
} from '../types/basicTypes'
|
||||
import { editDb, Edits } from './actions'
|
||||
import { editDb, Edits, updateQuestionsInArray } from './actions'
|
||||
// import { TaskObject } from './workerPool'
|
||||
|
||||
export interface WorkerResult {
|
||||
msg: string
|
||||
workerIndex: number
|
||||
result?: SearchResultQuestion[]
|
||||
result?: SearchResultQuestion[] | number[][]
|
||||
error?: boolean
|
||||
}
|
||||
|
||||
|
@ -115,12 +117,15 @@ function removeUnnecesarySpaces(toremove: string): string {
|
|||
return normalizeSpaces(toremove).replace(/\s+/g, ' ')
|
||||
}
|
||||
|
||||
export function compareString(
|
||||
function compareString(
|
||||
s1: string,
|
||||
s1a: Array<string>,
|
||||
s2: string,
|
||||
s2a: Array<string>
|
||||
s1cache?: Array<string>,
|
||||
s2cache?: Array<string>
|
||||
): number {
|
||||
const s1a = s1cache || s1.split(' ')
|
||||
const s2a = s2cache || s2.split(' ')
|
||||
|
||||
if (s1 === s2) {
|
||||
return 100
|
||||
}
|
||||
|
@ -277,16 +282,16 @@ function compareImage(data: QuestionData, data2: QuestionData): number {
|
|||
if (data.hashedImages && data2.hashedImages) {
|
||||
return compareString(
|
||||
data.hashedImages.join(' '),
|
||||
data.hashedImages,
|
||||
data2.hashedImages.join(' '),
|
||||
data.hashedImages,
|
||||
data2.hashedImages
|
||||
)
|
||||
} else if (data.images && data2.images) {
|
||||
return (
|
||||
compareString(
|
||||
data.images.join(' '),
|
||||
data.images,
|
||||
data2.images.join(' '),
|
||||
data.images,
|
||||
data2.images
|
||||
) - 10
|
||||
)
|
||||
|
@ -324,7 +329,7 @@ function compareData(q1: Question, q2: Question): number {
|
|||
}
|
||||
|
||||
function compareQuestion(q1: Question, q2: Question): number {
|
||||
return compareString(q1.Q, q1.cache.Q, q2.Q, q2.cache.Q)
|
||||
return compareString(q1.Q, q2.Q, q1.cache.Q, q2.cache.Q)
|
||||
// return compareString(
|
||||
// q1.Q,
|
||||
// q1.Q ? q1.Q.split(' ') : [],
|
||||
|
@ -334,7 +339,7 @@ function compareQuestion(q1: Question, q2: Question): number {
|
|||
}
|
||||
|
||||
function compareAnswer(q1: Question, q2: Question): number {
|
||||
return compareString(q1.A, q1.cache.A, q2.A, q2.cache.A)
|
||||
return compareString(q1.A, q2.A, q1.cache.A, q2.cache.A)
|
||||
// return compareString(
|
||||
// q1.A,
|
||||
// q1.A ? q1.A.split(' ') : [],
|
||||
|
@ -648,7 +653,7 @@ if (!isMainThread) {
|
|||
`[THREAD #${workerIndex}]: Worker ${workerIndex} reporting for duty`
|
||||
)
|
||||
|
||||
parentPort.on('message', (msg) => {
|
||||
parentPort.on('message', (msg /*: TaskObject */) => {
|
||||
if (msg.type === 'work') {
|
||||
const {
|
||||
subjName,
|
||||
|
@ -814,15 +819,93 @@ if (!isMainThread) {
|
|||
workerIndex: workerIndex,
|
||||
})
|
||||
// console.log(`[THREAD #${workerIndex}]: newdb`)
|
||||
} else if (msg.type === 'dbClean') {
|
||||
const removedIndexes = cleanDb(msg.data, qdbs)
|
||||
|
||||
const workerResult: WorkerResult = {
|
||||
msg: `From thread #${workerIndex}: db clean done`,
|
||||
workerIndex: workerIndex,
|
||||
result: removedIndexes,
|
||||
}
|
||||
|
||||
parentPort.postMessage(workerResult)
|
||||
} else if (msg.type === 'rmQuestions') {
|
||||
const {
|
||||
questionIndexesToRemove,
|
||||
subjIndex,
|
||||
qdbIndex,
|
||||
recievedQuestions,
|
||||
} = msg.data
|
||||
|
||||
qdbs[qdbIndex].data[subjIndex].Questions = updateQuestionsInArray(
|
||||
questionIndexesToRemove,
|
||||
qdbs[qdbIndex].data[subjIndex].Questions,
|
||||
recievedQuestions
|
||||
)
|
||||
|
||||
parentPort.postMessage({
|
||||
msg: `From thread #${workerIndex}: rm question done`,
|
||||
workerIndex: workerIndex,
|
||||
})
|
||||
} else {
|
||||
logger.Log(`Invalid msg type!`, logger.GetColor('redbg'))
|
||||
console.error(msg)
|
||||
|
||||
parentPort.postMessage({
|
||||
msg: `From thread #${workerIndex}: Invalid message type (${msg.type})!`,
|
||||
workerIndex: workerIndex,
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// console.log('[THREAD]: Main thread!')
|
||||
}
|
||||
|
||||
export function cleanDb(
|
||||
{
|
||||
questions: recievedQuestions,
|
||||
subjToClean,
|
||||
overwriteFromDate,
|
||||
qdbIndex,
|
||||
}: {
|
||||
questions: Question[]
|
||||
subjToClean: string
|
||||
overwriteFromDate: number
|
||||
qdbIndex: number
|
||||
},
|
||||
qdbs: QuestionDb[]
|
||||
): number[][] {
|
||||
const subjIndex = qdbs[qdbIndex].data.findIndex((x) => {
|
||||
return x.Name.toLowerCase().includes(subjToClean.toLowerCase())
|
||||
})
|
||||
|
||||
if (!qdbs[qdbIndex].data[subjIndex]) {
|
||||
return recievedQuestions.map(() => [])
|
||||
}
|
||||
|
||||
const questionIndexesToRemove = recievedQuestions.map((recievedQuestion) =>
|
||||
qdbs[qdbIndex].data[subjIndex].Questions.reduce<number[]>(
|
||||
(acc, question, i) => {
|
||||
const res = compareString(
|
||||
simplifyQuestion(recievedQuestion.Q),
|
||||
simplifyQuestion(question.Q)
|
||||
)
|
||||
|
||||
if (
|
||||
res > minMatchToNotSearchOtherSubjects &&
|
||||
(!question.data.date || question.data.date < overwriteFromDate)
|
||||
) {
|
||||
return [...acc, i]
|
||||
}
|
||||
return acc
|
||||
},
|
||||
[]
|
||||
)
|
||||
)
|
||||
|
||||
return questionIndexesToRemove
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue