mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
/ask POST, and other fixes
This commit is contained in:
parent
19b8fdc5ab
commit
c6da65ce40
3 changed files with 119 additions and 58 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue