mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Lotsa typescript bullshit
This commit is contained in:
parent
b7ac485689
commit
b927988017
65 changed files with 801 additions and 8447 deletions
|
@ -1,8 +1,13 @@
|
|||
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'
|
||||
import logger from './logger.js'
|
||||
import logger from './logger'
|
||||
import { Question, QuestionData, Subject } from '../types/basicTypes'
|
||||
|
||||
const searchDataWorkerFile = './src/utils/classes.js'
|
||||
// TODO
|
||||
interface SearchResultQuestion extends Question {
|
||||
match: number
|
||||
}
|
||||
|
||||
const searchDataWorkerFile = './src/utils/classes.ts'
|
||||
|
||||
const assert = (val) => {
|
||||
if (!val) {
|
||||
|
@ -37,7 +42,7 @@ const minMatchToNotSearchOtherSubjects = 90
|
|||
|
||||
// Exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function getSubjNameWithoutYear(subjName: string) {
|
||||
function getSubjNameWithoutYear(subjName: string): string {
|
||||
const t = subjName.split(' - ')
|
||||
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t[1] || subjName
|
||||
|
@ -51,7 +56,7 @@ function getSubjNameWithoutYear(subjName: string) {
|
|||
function removeStuff(
|
||||
value: string,
|
||||
removableStrings: Array<string>,
|
||||
toReplace: string
|
||||
toReplace?: string
|
||||
) {
|
||||
removableStrings.forEach((removableString) => {
|
||||
const regex = new RegExp(removableString, 'g')
|
||||
|
@ -92,24 +97,24 @@ function normalizeSpaces(input: string) {
|
|||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
function compareString(s1: string, s2: string) {
|
||||
if (!s1 || !s2) {
|
||||
if (!s1 && !s2) {
|
||||
function compareString(string1: string, string2: string) {
|
||||
if (!string1 || !string2) {
|
||||
if (!string1 && !string2) {
|
||||
return 100
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
s1 = simplifyStringForComparison(s1).split(' ')
|
||||
s2 = simplifyStringForComparison(s2).split(' ')
|
||||
const s1 = simplifyStringForComparison(string1).split(' ')
|
||||
const s2 = simplifyStringForComparison(string2).split(' ')
|
||||
let match = 0
|
||||
for (let i = 0; i < s1.length; i++) {
|
||||
if (s2.includes(s1[i])) {
|
||||
match++
|
||||
}
|
||||
}
|
||||
let percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
let percent = Math.round(parseFloat(((match / s1.length) * 100).toFixed(2)))
|
||||
const lengthDifference = Math.abs(s2.length - s1.length)
|
||||
percent -= lengthDifference * lengthDiffMultiplier
|
||||
if (percent < 0) {
|
||||
|
@ -161,7 +166,7 @@ function simplifyAnswer(value: string) {
|
|||
])
|
||||
}
|
||||
|
||||
function simplifyQuestion(question: Question) {
|
||||
function simplifyQuestion(question: Question | string) {
|
||||
if (!question) {
|
||||
return
|
||||
}
|
||||
|
@ -194,7 +199,11 @@ function simplifyQuestion(question: Question) {
|
|||
// Question
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
function createQuestion(question: string, answer: string, data: QuestionData) {
|
||||
function createQuestion(
|
||||
question: Question | string,
|
||||
answer: string,
|
||||
data: QuestionData
|
||||
): Question {
|
||||
return {
|
||||
Q: simplifyQuestion(question),
|
||||
A: simplifyAnswer(answer),
|
||||
|
@ -313,7 +322,7 @@ function searchQuestion(
|
|||
) {
|
||||
assert(question)
|
||||
|
||||
const result = []
|
||||
let result = []
|
||||
subj.Questions.forEach((currentQuestion) => {
|
||||
const percent = compareQuestionObj(
|
||||
currentQuestion,
|
||||
|
@ -359,7 +368,11 @@ function subjectToString(subj: Subject) {
|
|||
// ---------------------------------------------------------------------------------------------------------
|
||||
// QuestionDB
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function addQuestion(data: Array<Subject>, subj: string, question: Question) {
|
||||
function addQuestion(
|
||||
data: Array<Subject>,
|
||||
subj: string,
|
||||
question: Question
|
||||
): void {
|
||||
logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
|
||||
logger.DebugLog(question, 'qdb add', 3)
|
||||
assert(data)
|
||||
|
@ -389,12 +402,13 @@ function addQuestion(data: Array<Subject>, subj: string, question: Question) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: remove questionData, make question only Question type
|
||||
function searchData(
|
||||
data: Array<Subject>,
|
||||
question: Question,
|
||||
question: any,
|
||||
subjName: string,
|
||||
questionData: QuestionData
|
||||
) {
|
||||
questionData?: QuestionData
|
||||
): Promise<Array<SearchResultQuestion>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
assert(data)
|
||||
assert(question)
|
||||
|
@ -414,7 +428,7 @@ function searchData(
|
|||
}
|
||||
question = simplifyQuestion(question)
|
||||
|
||||
const worker = new Worker(searchDataWorkerFile, {
|
||||
const worker = workerTs(searchDataWorkerFile, {
|
||||
workerData: { data, subjName, question, questionData },
|
||||
})
|
||||
|
||||
|
@ -451,32 +465,7 @@ function searchData(
|
|||
})
|
||||
}
|
||||
|
||||
function addSubject(data: Array<Subject>, subj) {
|
||||
assert(data)
|
||||
assert(subj)
|
||||
|
||||
let i = 0
|
||||
while (i < length && subj.Name !== data[i].Name) {
|
||||
i++
|
||||
}
|
||||
|
||||
if (i < length) {
|
||||
return data.map((currSubj, j) => {
|
||||
if (j === i) {
|
||||
return {
|
||||
...currSubj,
|
||||
Questions: [...currSubj.Questions, ...subj.Questions],
|
||||
}
|
||||
} else {
|
||||
return currSubj
|
||||
}
|
||||
})
|
||||
} else {
|
||||
return [...data, subj]
|
||||
}
|
||||
}
|
||||
|
||||
function dataToString(data: Array<Subject>) {
|
||||
function dataToString(data: Array<Subject>): string {
|
||||
const result = []
|
||||
data.forEach((subj) => {
|
||||
result.push(subjectToString(subj))
|
||||
|
@ -485,9 +474,13 @@ function dataToString(data: Array<Subject>) {
|
|||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
if (!isMainThread) {
|
||||
logger.DebugLog(`Starting search worker ...`, 'worker', 1)
|
||||
const { data, subjName, question, questionData } = workerData
|
||||
|
||||
function searchWorker(
|
||||
data: any,
|
||||
subjName: any,
|
||||
question: any,
|
||||
questionData: any
|
||||
): any {
|
||||
let result = []
|
||||
|
||||
data.forEach((subj) => {
|
||||
|
@ -496,7 +489,7 @@ if (!isMainThread) {
|
|||
.toLowerCase()
|
||||
.includes(getSubjNameWithoutYear(subj.Name).toLowerCase())
|
||||
) {
|
||||
logger.DebugLog(`Searching in ${subj.Name} `, 2)
|
||||
logger.DebugLog(`Searching in ${subj.Name} `, 'searchworker', 2)
|
||||
result = result.concat(
|
||||
searchQuestion(subj, question, questionData, subjName)
|
||||
)
|
||||
|
@ -512,7 +505,7 @@ if (!isMainThread) {
|
|||
) {
|
||||
logger.DebugLog(
|
||||
'Reqults length is zero when comparing names, trying all subjects',
|
||||
'qdb search',
|
||||
'searchworker',
|
||||
1
|
||||
)
|
||||
data.forEach((subj) => {
|
||||
|
@ -523,7 +516,7 @@ if (!isMainThread) {
|
|||
if (result.length > 0) {
|
||||
logger.DebugLog(
|
||||
`FIXME: '${subjName}' gave no result but '' did!`,
|
||||
'qdb search',
|
||||
'searchworker',
|
||||
1
|
||||
)
|
||||
console.error(`FIXME: '${subjName}' gave no result but '' did!`)
|
||||
|
@ -543,14 +536,37 @@ if (!isMainThread) {
|
|||
parentPort.postMessage(result)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const workerTs = (file: string, wkOpts: any) => {
|
||||
wkOpts.eval = true
|
||||
if (!wkOpts.workerData) {
|
||||
wkOpts.workerData = {}
|
||||
}
|
||||
wkOpts.workerData.__filename = file
|
||||
return new Worker(
|
||||
`
|
||||
const wk = require('worker_threads');
|
||||
require('ts-node').register();
|
||||
let file = wk.workerData.__filename;
|
||||
delete wk.workerData.__filename;
|
||||
require(file);
|
||||
`,
|
||||
wkOpts
|
||||
)
|
||||
}
|
||||
|
||||
if (!isMainThread) {
|
||||
logger.DebugLog(`Starting search worker ...`, 'searchworker', 1)
|
||||
const { data, subjName, question, questionData } = workerData
|
||||
searchWorker(data, subjName, question, questionData)
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
export {
|
||||
minMatchAmmount,
|
||||
getSubjNameWithoutYear,
|
||||
createQuestion,
|
||||
addQuestion,
|
||||
addSubject,
|
||||
searchData,
|
||||
dataToString,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue