From c6da65ce40c04236d18feb2cce9df28bca2af699 Mon Sep 17 00:00:00 2001 From: mrfry Date: Sat, 19 Dec 2020 12:47:06 +0100 Subject: [PATCH] /ask POST, and other fixes --- src/modules/api/api.ts | 162 ++++++++++++++++++++++++++-------------- src/utils/classes.ts | 14 +++- src/utils/workerPool.ts | 1 + 3 files changed, 119 insertions(+), 58 deletions(-) diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index bcde7f1..815cc75 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -64,6 +64,7 @@ const idvStatFile = 'stats/idvstats' const todosFile = 'data/todos.json' const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js' const rootRedirectToFile = 'data/apiRootRedirectTo' +const recievedQuestionFile = 'stats/recievedQuestions' // other constants const line = '====================================================' // lol @@ -1071,73 +1072,122 @@ function GetApp(): ModuleType { } }) + function writeAskData(body) { + try { + let towrite = utils.GetDateString() + '\n' + towrite += + '------------------------------------------------------------------------------\n' + towrite += JSON.stringify(body) + towrite += + '\n------------------------------------------------------------------------------\n' + utils.AppendToFile(towrite, recievedQuestionFile) + } catch (err) { + logger.Log('Error writing revieved /ask POST data') + console.error(err) + } + } + + app.post('/ask', function(req: Request, res) { + if (!req.body.questions) { + res.json({ + message: + 'ask something! ?q=[question]&subj=[subject]&data=[question data]. "subj" is optimal for faster result', + result: [], + recievedData: JSON.stringify(req.body), + success: false, + }) + return + } + const subj: any = req.body.subj || '' + + writeAskData(req.body) + + const resultPromises = req.body.questions.map((question) => { + return getResult(question, subj, null, req.query) + }) + + Promise.all(resultPromises).then((result) => { + console.log(result) + res.json(result) + }) + }) + app.get('/ask', function(req: Request, res) { if (Object.keys(req.query).length === 0) { - logger.DebugLog(`No query params`, 'ask', 1) + logger.DebugLog(`No query params /ask GET`, 'ask', 1) res.json({ - message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`, + message: + 'ask something! ?q=[question]&subj=[subject]&data=[question data]. "subj" is optimal for faster result', result: [], recievedData: JSON.stringify(req.query), success: false, }) - } else { - if (req.query.q && req.query.data) { - const subj: any = req.query.subj || '' - const question = req.query.q - const recData: any = req.query.data + return + } - doALongTask({ - type: 'work', - data: { - searchIn: 'all', - question: question, - subjName: subj, - questionData: recData, - }, - }) - .then((result) => { - console.log(result) - try { - res.json({ - result: result, - success: true, - }) - logger.DebugLog( - `Question result length: ${result.length}`, - 'ask', - 1 - ) - logger.DebugLog(result, 'ask', 2) - } catch (err) { - console.error(err) - logger.Log( - 'Error while sending ask results', - logger.GetColor('redbg') - ) - } - }) - .catch((err) => { - logger.Log('Search Data error!', logger.GetColor('redbg')) - console.error(err) - res.json({ - message: `There was an error processing the question: ${err.message}`, - result: [], - recievedData: JSON.stringify(req.query), - success: false, - }) - }) - } else { - logger.DebugLog(`Invalid question`, 'ask', 1) - res.json({ - message: `Invalid question :(`, - result: [], - recievedData: JSON.stringify(req.query), - success: false, - }) - } + if (req.query.q && req.query.data) { + const subj: any = req.query.subj || '' + const question = req.query.q + const recData: any = req.query.data + getResult(question, subj, recData, req.query).then((result) => { + res.json(result) + }) + } else { + logger.DebugLog(`Invalid question`, 'ask', 1) + res.json({ + message: `Invalid question :(`, + result: [], + recievedData: JSON.stringify(req.query), + success: false, + }) } }) + function getResult(question, subj, recData, recievedData) { + return new Promise((resolve) => { + doALongTask({ + type: 'work', + data: { + searchIn: 'all', + question: question, + subjName: subj, + questionData: recData, + }, + }) + .then((result) => { + console.log(result) + try { + logger.DebugLog( + `Question result length: ${result.length}`, + 'ask', + 1 + ) + logger.DebugLog(result, 'ask', 2) + resolve({ + result: result, + success: true, + }) + } catch (err) { + console.error(err) + logger.Log( + 'Error while sending ask results', + logger.GetColor('redbg') + ) + } + }) + .catch((err) => { + logger.Log('Search Data error!', logger.GetColor('redbg')) + console.error(err) + resolve({ + message: `There was an error processing the question: ${err.message}`, + result: [], + recievedData: JSON.stringify(recievedData), + success: false, + }) + }) + }) + } + function getSubjCount(qdbs) { return qdbs.reduce((acc, qdb) => { return acc + qdb.data.length diff --git a/src/utils/classes.ts b/src/utils/classes.ts index c6667d1..399cb4d 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -403,11 +403,21 @@ function prepareQuestion( data: string | QuestionData ): Question { let preparedQuestion: Question + if (typeof question === 'object') { preparedQuestion = question } else { - // FIXME data was checkedif its null, it should be never null. check if its really never null - const parsedData = typeof data === 'object' ? data : JSON.parse(data) // TODO: put in try? + let parsedData + if (typeof data === 'string') { + try { + parsedData = JSON.parse(data) + } catch (err) { + // asd + } + } + if (typeof data === 'object') { + parsedData = data + } preparedQuestion = createQuestion(question, null, parsedData) } diff --git a/src/utils/workerPool.ts b/src/utils/workerPool.ts index 2ea84ac..9fd0fc5 100644 --- a/src/utils/workerPool.ts +++ b/src/utils/workerPool.ts @@ -103,6 +103,7 @@ function getAWorker(i, initData) { }) worker.on('exit', (code) => { + // TODO: this is critical, whole server should stop, or child threads should be restarted logger.Log( `[MAIN]: worker #${i} exit code: ${code}`, code === 0 ? logger.GetColor('redbg') : logger.GetColor('green')