Seperate dbs

This commit is contained in:
mrfry 2020-11-26 09:16:12 +01:00
parent 728931d56e
commit 4e34267d44
9 changed files with 149 additions and 82 deletions

View file

@ -24,7 +24,7 @@ import logger from '../utils/logger'
import { searchData, createQuestion } from '../utils/classes'
import idStats from '../utils/ids'
import utils from '../utils/utils'
import { addQuestion, getSubjNameWithoutYear } from './classes'
import { SearchResult, addQuestion, getSubjNameWithoutYear } from './classes'
// types
import { QuestionDb, Question, User, DataFile } from '../types/basicTypes'
@ -51,29 +51,42 @@ interface Result {
export function logResult(
recievedData: RecievedData,
result: Array<Result>
result: Array<Result>,
userId: number,
dryRun?: boolean
): void {
let subjRow = '\t' + recievedData.subj
if (recievedData.id) {
subjRow += ' ( CID: ' + logger.logHashed(recievedData.id) + ')'
}
logger.Log(subjRow)
logger.Log('\t' + recievedData.subj)
result.forEach((res: Result) => {
const allQLength = recievedData.quiz.length
let msg = `${res.qdbName}: `
let color = logger.GetColor('green')
if (res.newQuestions > 0) {
color = logger.GetColor('blue')
msg += `New questions: ${res.newQuestions} ( All: ${allQLength} )`
} else {
msg += `No new data ( ${allQLength} )`
}
if (recievedData.version !== undefined) {
msg += '. Version: ' + recievedData.version
}
logger.Log('\t' + msg, color)
})
if (dryRun) {
logger.Log('\tDry run')
}
let idRow = '\t'
if (recievedData.version) {
idRow += 'Version: ' + logger.logHashed(recievedData.version)
}
if (recievedData.id) {
idRow += ', CID: ' + logger.logHashed(recievedData.id)
}
idRow += `, User #${logger.logHashed(userId.toString())}`
logger.Log(idRow)
if (result.length > 0) {
result.forEach((res: Result) => {
const allQLength = recievedData.quiz.length
let msg = `${res.qdbName}: `
let color = logger.GetColor('green')
if (res.newQuestions > 0) {
color = logger.GetColor('blue')
msg += `New questions: ${res.newQuestions} ( All: ${allQLength} )`
} else {
msg += `No new data ( ${allQLength} )`
}
logger.Log('\t' + msg, color)
})
} else {
logger.Log('\tNo db-s passed shouldSave!', logger.GetColor('red'))
}
}
export function processIncomingRequest(
@ -90,7 +103,7 @@ export function processIncomingRequest(
}
try {
let towrite = logger.GetDateString() + '\n'
let towrite = utils.GetDateString() + '\n'
towrite +=
'------------------------------------------------------------------------------\n'
if (typeof recievedData === 'object') {
@ -153,15 +166,15 @@ function processIncomingRequestUsingDb(
logger.DebugLog(currentQuestion, 'actions', 3)
recievedQuestions.push(currentQuestion)
questionSearchPromises.push(
searchData(qdb.data, currentQuestion, recievedData.subj)
searchData(qdb, currentQuestion, recievedData.subj)
)
})
Promise.all(questionSearchPromises)
.then((results) => {
.then((results: Array<SearchResult>) => {
const allQuestions = [] // all new questions here that do not have result
results.forEach((result, i) => {
const add = result.every((res) => {
const add = result.result.every((res) => {
return res.match < minMatchToAmmountToAdd
})
if (add) {
@ -192,8 +205,6 @@ function processIncomingRequestUsingDb(
currWrites = 0
logger.DebugLog('Writing data.json', 'actions', 1)
utils.WriteFile(JSON.stringify(qdb.data), qdb.path)
} else if (dryRun) {
logger.Log('\tDry run')
}
}