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 todosFile = 'data/todos.json'
|
||||||
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
||||||
const rootRedirectToFile = 'data/apiRootRedirectTo'
|
const rootRedirectToFile = 'data/apiRootRedirectTo'
|
||||||
|
const recievedQuestionFile = 'stats/recievedQuestions'
|
||||||
|
|
||||||
// other constants
|
// other constants
|
||||||
const line = '====================================================' // lol
|
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) {
|
app.get('/ask', function(req: Request, res) {
|
||||||
if (Object.keys(req.query).length === 0) {
|
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({
|
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: [],
|
result: [],
|
||||||
recievedData: JSON.stringify(req.query),
|
recievedData: JSON.stringify(req.query),
|
||||||
success: false,
|
success: false,
|
||||||
})
|
})
|
||||||
} else {
|
return
|
||||||
if (req.query.q && req.query.data) {
|
}
|
||||||
const subj: any = req.query.subj || ''
|
|
||||||
const question = req.query.q
|
|
||||||
const recData: any = req.query.data
|
|
||||||
|
|
||||||
doALongTask({
|
if (req.query.q && req.query.data) {
|
||||||
type: 'work',
|
const subj: any = req.query.subj || ''
|
||||||
data: {
|
const question = req.query.q
|
||||||
searchIn: 'all',
|
const recData: any = req.query.data
|
||||||
question: question,
|
getResult(question, subj, recData, req.query).then((result) => {
|
||||||
subjName: subj,
|
res.json(result)
|
||||||
questionData: recData,
|
})
|
||||||
},
|
} else {
|
||||||
})
|
logger.DebugLog(`Invalid question`, 'ask', 1)
|
||||||
.then((result) => {
|
res.json({
|
||||||
console.log(result)
|
message: `Invalid question :(`,
|
||||||
try {
|
result: [],
|
||||||
res.json({
|
recievedData: JSON.stringify(req.query),
|
||||||
result: result,
|
success: false,
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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) {
|
function getSubjCount(qdbs) {
|
||||||
return qdbs.reduce((acc, qdb) => {
|
return qdbs.reduce((acc, qdb) => {
|
||||||
return acc + qdb.data.length
|
return acc + qdb.data.length
|
||||||
|
|
|
@ -403,11 +403,21 @@ function prepareQuestion(
|
||||||
data: string | QuestionData
|
data: string | QuestionData
|
||||||
): Question {
|
): Question {
|
||||||
let preparedQuestion: Question
|
let preparedQuestion: Question
|
||||||
|
|
||||||
if (typeof question === 'object') {
|
if (typeof question === 'object') {
|
||||||
preparedQuestion = question
|
preparedQuestion = question
|
||||||
} else {
|
} else {
|
||||||
// FIXME data was checkedif its null, it should be never null. check if its really never null
|
let parsedData
|
||||||
const parsedData = typeof data === 'object' ? data : JSON.parse(data) // TODO: put in try?
|
if (typeof data === 'string') {
|
||||||
|
try {
|
||||||
|
parsedData = JSON.parse(data)
|
||||||
|
} catch (err) {
|
||||||
|
// asd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (typeof data === 'object') {
|
||||||
|
parsedData = data
|
||||||
|
}
|
||||||
preparedQuestion = createQuestion(question, null, parsedData)
|
preparedQuestion = createQuestion(question, null, parsedData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -103,6 +103,7 @@ function getAWorker(i, initData) {
|
||||||
})
|
})
|
||||||
|
|
||||||
worker.on('exit', (code) => {
|
worker.on('exit', (code) => {
|
||||||
|
// TODO: this is critical, whole server should stop, or child threads should be restarted
|
||||||
logger.Log(
|
logger.Log(
|
||||||
`[MAIN]: worker #${i} exit code: ${code}`,
|
`[MAIN]: worker #${i} exit code: ${code}`,
|
||||||
code === 0 ? logger.GetColor('redbg') : logger.GetColor('green')
|
code === 0 ? logger.GetColor('redbg') : logger.GetColor('green')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue