mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Seperate dbs
This commit is contained in:
parent
728931d56e
commit
4e34267d44
9 changed files with 149 additions and 82 deletions
|
@ -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')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,15 @@ import {
|
|||
Subject,
|
||||
} from '../types/basicTypes'
|
||||
|
||||
// TODO
|
||||
interface SearchResultQuestion extends Question {
|
||||
match: number
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
result: Array<SearchResultQuestion>
|
||||
dbName: string
|
||||
}
|
||||
|
||||
const searchDataWorkerFile = './src/utils/classes.ts'
|
||||
|
||||
const assert = (val) => {
|
||||
|
@ -217,7 +221,10 @@ function createQuestion(
|
|||
}
|
||||
|
||||
function compareImage(data: QuestionData, data2: QuestionData) {
|
||||
return compareString(data.images.join(' '), data2.images.join(' '))
|
||||
// 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(' '))
|
||||
}
|
||||
|
||||
function compareData(q1: Question, q2: Question) {
|
||||
|
@ -412,23 +419,22 @@ function searchDatas(
|
|||
question: any,
|
||||
subjName: string,
|
||||
questionData?: QuestionData
|
||||
): Promise<Array<Array<SearchResultQuestion>>> {
|
||||
): Promise<Array<SearchResult>> {
|
||||
return Promise.all(
|
||||
data.map((db) => {
|
||||
return searchData(db.data, question, subjName, questionData)
|
||||
data.map((db: QuestionDb) => {
|
||||
return searchData(db, question, subjName, questionData)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: remove questionData, make question only Question type
|
||||
// FIXME: remove questionData, make question only Question type
|
||||
function searchData(
|
||||
data: Array<Subject>,
|
||||
qdb: QuestionDb,
|
||||
question: any,
|
||||
subjName: string,
|
||||
questionData?: QuestionData
|
||||
): Promise<Array<SearchResultQuestion>> {
|
||||
): Promise<SearchResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
assert(data)
|
||||
assert(question)
|
||||
logger.DebugLog('Searching for question', 'qdb search', 1)
|
||||
logger.DebugLog('Question:', 'qdb search', 2)
|
||||
|
@ -447,7 +453,7 @@ function searchData(
|
|||
question = simplifyQuestion(question)
|
||||
|
||||
const worker = workerTs(searchDataWorkerFile, {
|
||||
workerData: { data, subjName, question, questionData },
|
||||
workerData: { data: qdb.data, subjName, question, questionData },
|
||||
})
|
||||
|
||||
worker.on('error', (err) => {
|
||||
|
@ -478,7 +484,10 @@ function searchData(
|
|||
'qdb search',
|
||||
1
|
||||
)
|
||||
resolve(result)
|
||||
resolve({
|
||||
result: result,
|
||||
dbName: qdb.name,
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -494,10 +503,10 @@ function dataToString(data: Array<Subject>): string {
|
|||
// ------------------------------------------------------------------------
|
||||
|
||||
function searchWorker(
|
||||
data: any,
|
||||
subjName: any,
|
||||
question: any,
|
||||
questionData: any
|
||||
data: Array<Subject>,
|
||||
subjName: string,
|
||||
question: Question,
|
||||
questionData?: QuestionData
|
||||
): any {
|
||||
let result = []
|
||||
|
||||
|
|
|
@ -126,7 +126,6 @@ function CreateTable(db: any, name: any, columns: any, foreignKeys: any): any {
|
|||
const cols = Object.keys(columns)
|
||||
.reduce((acc, key) => {
|
||||
const item = columns[key]
|
||||
// FIXME: array, and push stuff, then join()
|
||||
const flags = []
|
||||
const toCheck = {
|
||||
primary: 'PRIMARY KEY',
|
||||
|
|
|
@ -22,7 +22,7 @@ const hr =
|
|||
'---------------------------------------------------------------------------------'
|
||||
|
||||
export default {
|
||||
GetDateString: GetDateString,
|
||||
getColoredDateString: getColoredDateString,
|
||||
Log: Log,
|
||||
DebugLog: DebugLog,
|
||||
GetColor: GetColor,
|
||||
|
@ -70,7 +70,7 @@ function setNewLogfileName(): void {
|
|||
logFileName = utils.GetDateString(true)
|
||||
}
|
||||
|
||||
function GetDateString(): string {
|
||||
function getColoredDateString(): string {
|
||||
const date = new Date()
|
||||
const dateString = utils.GetDateString()
|
||||
return GetRandomColor(date.getHours().toString()) + dateString + C()
|
||||
|
@ -99,7 +99,7 @@ function Log(msg: string | object, color?: string): void {
|
|||
let log = msg
|
||||
if (typeof msg !== 'object') {
|
||||
const delimiter = DELIM + C(color)
|
||||
log = C(color) + GetDateString() + delimiter + msg + C()
|
||||
log = getColoredDateString() + delimiter + C(color) + msg + C()
|
||||
}
|
||||
|
||||
console.log(log)
|
||||
|
@ -109,7 +109,6 @@ function Log(msg: string | object, color?: string): void {
|
|||
)
|
||||
}
|
||||
|
||||
// FIXME: express.Request type, but with .cookies and .session
|
||||
function LogReq(
|
||||
req: any /*express.Request*/,
|
||||
toFile?: boolean,
|
||||
|
@ -162,7 +161,7 @@ function LogReq(
|
|||
if (!toFile) {
|
||||
Log(logEntry)
|
||||
} else {
|
||||
const defLogs = GetDateString() + dl + logEntry
|
||||
const defLogs = getColoredDateString() + dl + logEntry
|
||||
|
||||
utils.AppendToFile(defLogs, vlogDir + logFileName)
|
||||
}
|
||||
|
|
|
@ -17,14 +17,22 @@ import fs from 'fs'
|
|||
import logger from '../utils/logger'
|
||||
|
||||
interface URLFormatOptions {
|
||||
query: any
|
||||
pathname?: string
|
||||
query?: any
|
||||
}
|
||||
|
||||
// TODO
|
||||
function formatUrl(options: URLFormatOptions): string {
|
||||
console.log(options.pathname, options.query)
|
||||
return options.pathname + JSON.stringify(options.query)
|
||||
const queryString = options.query
|
||||
? '?' +
|
||||
Object.keys(options.query)
|
||||
.map((key) => {
|
||||
return `${key}=${encodeURIComponent(options.query[key])}`
|
||||
})
|
||||
.join('&')
|
||||
: ''
|
||||
const path = options.pathname || ''
|
||||
return path + queryString
|
||||
}
|
||||
|
||||
function GetDateString(noTime?: boolean): string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue