Domain specific data files

This commit is contained in:
mrfry 2020-12-22 16:11:01 +01:00
parent 81577ad621
commit f87e165084
9 changed files with 174 additions and 71 deletions

View file

@ -43,6 +43,7 @@ export interface RecievedData {
id: string
version: string
scriptVersion: string
location: string
}
export interface Result {
@ -86,7 +87,7 @@ export function logResult(
logger.Log('\t' + msg, color)
})
} else {
logger.Log('\tNo db-s passed shouldSave!', logger.GetColor('red'))
logger.Log('\tResults length is zero!', logger.GetColor('red'))
}
}
@ -130,9 +131,7 @@ export function processIncomingRequest(
// FIXME: this many promises and stuff might be unnecesarry
const promises: Array<Promise<Result>> = questionDbs.reduce((acc, qdb) => {
if (qdb.shouldSave(recievedData)) {
acc.push(processIncomingRequestUsingDb(recievedData, qdb, dryRun, user))
}
acc.push(processIncomingRequestUsingDb(recievedData, qdb, dryRun, user))
return acc
}, [])
return Promise.all(promises)
@ -262,17 +261,53 @@ function processIncomingRequestUsingDb(
})
}
export function loadJSON(dataFiles: Array<DataFile>): Array<QuestionDb> {
export function shouldSaveDataFile(
df: DataFile,
recievedData: RecievedData
): Boolean {
if (df.shouldSave.version) {
const { compare, val } = df.shouldSave.version
if (compare === 'equals') {
return recievedData.version === val
} else if (compare === 'startsWith') {
return recievedData.version.startsWith(val)
}
}
if (df.shouldSave.version) {
const { compare, val } = df.shouldSave.version
if (compare === 'equals') {
return recievedData.version === val
} else if (compare === 'startsWith') {
return recievedData.version.startsWith(val)
}
}
if (df.shouldSave.location) {
const { val } = df.shouldSave.location
return recievedData.location.includes(val)
}
return false
}
export function loadJSON(
dataFiles: Array<DataFile>,
dataDir: string
): Array<QuestionDb> {
return dataFiles.reduce((acc, dataFile, index) => {
if (!utils.FileExists(dataFile.path)) {
utils.WriteFile(JSON.stringify([]), dataFile.path)
const dataPath = dataDir + dataFile.path
if (!utils.FileExists(dataPath)) {
utils.WriteFile(JSON.stringify([]), dataPath)
}
try {
acc.push({
...dataFile,
path: dataPath,
index: index,
data: JSON.parse(utils.ReadFile(dataFile.path)),
data: JSON.parse(utils.ReadFile(dataPath)),
})
} catch (err) {
console.error(err)
@ -305,12 +340,3 @@ export function backupData(questionDbs: Array<QuestionDb>): void {
}
})
}
export function getQuestionDbsWithoutFunct(
questionDbs: Array<QuestionDb> // FIXME: type for dis
): Array<any> {
return questionDbs.map((qdb) => {
const { shouldSave, ...res } = qdb // eslint-disable-line
return res
})
}

View file

@ -509,11 +509,11 @@ if (!isMainThread) {
const index = msg.index
const searchIn = msg.data.searchIn
console.log(
`[THREAD #${workerIndex}]: staring work${
!isNaN(index) ? ` on job index #${index}` : ''
}`
)
// console.log(
// `[THREAD #${workerIndex}]: staring work${
// !isNaN(index) ? ` on job index #${index}` : ''
// }`
// )
let searchResult = []
@ -549,18 +549,21 @@ if (!isMainThread) {
result: sortedResult,
})
console.log(
`[THREAD #${workerIndex}]: Work ${
!isNaN(index) ? `#${index}` : ''
}done!`
)
// console.log(
// `[THREAD #${workerIndex}]: Work ${
// !isNaN(index) ? `#${index}` : ''
// }done!`
// )
} else if (msg.type === 'update') {
qdbs = msg.qdbs
console.log(`[THREAD #${workerIndex}]: update`)
// console.log(`[THREAD #${workerIndex}]: update`)
} else if (msg.type === 'newdb') {
qdbs.push(msg.newdb)
// console.log(`[THREAD #${workerIndex}]: newdb`)
}
})
} else {
console.log('[THREAD]: Main thread!')
// console.log('[THREAD]: Main thread!')
}
// ------------------------------------------------------------------------

View file

@ -32,7 +32,7 @@ export default {
logHashed: logHashed,
hr: hr,
C: C,
setNewLogfileName,
setNewLogfileName: setNewLogfileName,
}
const DELIM = C('green') + '|' + C()
@ -166,7 +166,7 @@ function LogReq(req: Request, toFile?: boolean, statusCode?: string): void {
if (!toFile) {
Log(logEntry)
} else {
const defLogs = getColoredDateString() + dl + logEntry
const defLogs = utils.GetDateString() + dl + logEntry
utils.AppendToFile(defLogs, vlogDir + logFileName)
}

View file

@ -27,7 +27,7 @@ export function doALongTask(obj: any): Promise<any> {
resolve(res)
// TODO: check if result is really a result, and want to release port
pool.release(client)
console.log('[RELEASE]: #' + client.index)
// console.log('[RELEASE]: #' + client.index)
})
})
.catch(function(err) {
@ -56,9 +56,9 @@ export function initWorkerPool(initData: any): void {
}
},
destroy: function(client) {
console.log('[DESTROY]')
// console.log('[DESTROY]')
client.worker.terminate()
console.log('[DESTROYED] #' + client.index)
// console.log('[DESTROYED] #' + client.index)
},
}
@ -115,9 +115,9 @@ function getAWorker(i, initData) {
// ---------------------------------------------------------------------------
function doSomething(client, obj) {
const { index, worker } = client
const { /* index, */ worker } = client
return new Promise((resolve) => {
console.log('[ACCUIRE]: #' + index)
// console.log('[ACCUIRE]: #' + index)
worker.postMessage(obj)
worker.once('message', (msg) => {
resolve(msg)