mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
ask api changes, so it wont search unnecesary question dbs
This commit is contained in:
parent
0463ab3cd2
commit
b53fd84bf2
5 changed files with 116 additions and 65 deletions
|
@ -34,6 +34,7 @@ import {
|
|||
logResult,
|
||||
backupData,
|
||||
shouldSaveDataFile,
|
||||
shouldSearchDataFile,
|
||||
loadJSON,
|
||||
Result,
|
||||
} from '../../utils/actions'
|
||||
|
@ -930,10 +931,62 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
})
|
||||
|
||||
function getNewQdb(location, maxIndex) {
|
||||
// TODO: should check if location is a not empty string
|
||||
logger.Log(
|
||||
`No suitable questiondbs found for ${location}, creating a new one...`
|
||||
)
|
||||
const newDb: DataFile = {
|
||||
path: `${location}.json`,
|
||||
name: location,
|
||||
shouldSearch: {
|
||||
location: {
|
||||
val: location,
|
||||
},
|
||||
},
|
||||
shouldSave: {
|
||||
location: {
|
||||
val: location,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
utils.WriteFile(
|
||||
JSON.stringify(
|
||||
[
|
||||
...utils.ReadJSON(dbsFile),
|
||||
newDb, // stored as 'data.json', but is './publicDirs/.../data.json' runtime
|
||||
],
|
||||
null,
|
||||
2
|
||||
),
|
||||
dbsFile
|
||||
)
|
||||
|
||||
// "loading" new db
|
||||
const loadedNewDb: QuestionDb = {
|
||||
...newDb,
|
||||
data: [],
|
||||
path: publicDir + newDb.path,
|
||||
index: maxIndex,
|
||||
}
|
||||
utils.WriteFile('[]', loadedNewDb.path)
|
||||
|
||||
questionDbs.push(loadedNewDb)
|
||||
// TODO: problem: new dbs wont get to workers before trying to search with them.
|
||||
msgAllWorker({
|
||||
newdb: loadedNewDb,
|
||||
type: 'newdb',
|
||||
})
|
||||
|
||||
return loadedNewDb
|
||||
}
|
||||
|
||||
app.post('/isAdding', function(req: Request, res: any) {
|
||||
logger.LogReq(req)
|
||||
const user: User = req.session.user
|
||||
const dryRun = testUsers.includes(user.id)
|
||||
const location = req.body.location.split('/')[2]
|
||||
|
||||
try {
|
||||
let maxIndex = -1
|
||||
|
@ -945,49 +998,7 @@ function GetApp(): ModuleType {
|
|||
}, [])
|
||||
|
||||
if (suitedQuestionDbs.length === 0) {
|
||||
const location = req.body.location.split('/')[2]
|
||||
// TODO: should check if location is a not empty string
|
||||
logger.Log(
|
||||
`No suitable questiondbs found for ${location}, creating a new one...`
|
||||
)
|
||||
const newDb: DataFile = {
|
||||
path: `${location}.json`,
|
||||
name: location,
|
||||
shouldSave: {
|
||||
location: {
|
||||
val: location,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
utils.WriteFile(
|
||||
JSON.stringify(
|
||||
[
|
||||
...utils.ReadJSON(dbsFile),
|
||||
newDb, // stored as 'data.json', but is './publicDirs/.../data.json' runtime
|
||||
],
|
||||
null,
|
||||
2
|
||||
),
|
||||
dbsFile
|
||||
)
|
||||
|
||||
// "loading" new db
|
||||
const loadedNewDb: QuestionDb = {
|
||||
...newDb,
|
||||
data: [],
|
||||
path: publicDir + newDb.path,
|
||||
index: maxIndex,
|
||||
}
|
||||
utils.WriteFile('[]', loadedNewDb.path)
|
||||
|
||||
suitedQuestionDbs.push(loadedNewDb)
|
||||
questionDbs.push(loadedNewDb)
|
||||
// TODO: problem: new dbs wont get to workers before trying to search with them.
|
||||
msgAllWorker({
|
||||
newdb: loadedNewDb,
|
||||
type: 'newdb',
|
||||
})
|
||||
suitedQuestionDbs.push(getNewQdb(location, maxIndex))
|
||||
}
|
||||
|
||||
processIncomingRequest(req.body, suitedQuestionDbs, dryRun, user)
|
||||
|
@ -1048,16 +1059,16 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
}
|
||||
|
||||
function saveQuestion(questions, subj, location, userid) {
|
||||
function saveQuestion(questions, subj, testUrl, userid) {
|
||||
// TODO: clear folder every now and then, check if saved questions exist
|
||||
const questionsToSave = {
|
||||
questions: questions,
|
||||
subj: subj,
|
||||
userid: userid,
|
||||
location: location,
|
||||
testUrl: testUrl,
|
||||
date: new Date(),
|
||||
}
|
||||
const fname = `${utils.GetDateString()}_${userid}_${location}.json`
|
||||
const fname = `${utils.GetDateString()}_${userid}_${testUrl}.json`
|
||||
const subject = getSubjNameWithoutYear(subj)
|
||||
const subjPath = `${savedQuestionsDir}/${subject}`
|
||||
const savedSubjQuestionsFilePath = `${subjPath}/${savedQuestionsFileName}`
|
||||
|
@ -1072,7 +1083,7 @@ function GetApp(): ModuleType {
|
|||
fname: fname,
|
||||
subj: subj,
|
||||
userid: userid,
|
||||
location: location,
|
||||
testUrl: testUrl,
|
||||
date: new Date(),
|
||||
})
|
||||
utils.WriteFile(JSON.stringify(savedQuestions), savedSubjQuestionsFilePath)
|
||||
|
@ -1094,12 +1105,14 @@ function GetApp(): ModuleType {
|
|||
return
|
||||
}
|
||||
const subj: any = req.body.subj || ''
|
||||
const location = req.body.location.split('/')[2]
|
||||
const testUrl = req.body.testUrl
|
||||
? req.body.testUrl.split('/')[2]
|
||||
: undefined
|
||||
|
||||
writeAskData(req.body)
|
||||
|
||||
const resultPromises = req.body.questions.map((question) => {
|
||||
return getResult(question, subj, null, req.query)
|
||||
return getResult(question, subj, null, req.body, testUrl)
|
||||
})
|
||||
|
||||
Promise.all(resultPromises).then((results) => {
|
||||
|
@ -1120,7 +1133,7 @@ function GetApp(): ModuleType {
|
|||
return acc
|
||||
}, [])
|
||||
|
||||
saveQuestion(saveableQuestions, subj, location, user.id)
|
||||
saveQuestion(saveableQuestions, subj, testUrl, user.id)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -1155,18 +1168,33 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
})
|
||||
|
||||
function getResult(question, subj, recData, recievedData) {
|
||||
function getResult(question, subj, recData, recievedData, testUrl?) {
|
||||
return new Promise((resolve) => {
|
||||
let searchIn = testUrl
|
||||
? questionDbs.reduce((acc, qdb, i) => {
|
||||
if (shouldSearchDataFile(qdb, testUrl)) {
|
||||
acc.push(i)
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
: 'all'
|
||||
|
||||
if (searchIn.length === 0) {
|
||||
searchIn = 'all'
|
||||
}
|
||||
|
||||
// searchIn could be [0], [1], ... to search every db in different thread. Put this into a
|
||||
// forEach(qdbs) to achieve this
|
||||
doALongTask({
|
||||
type: 'work',
|
||||
data: {
|
||||
searchIn: 'all',
|
||||
searchIn: searchIn,
|
||||
testUrl: testUrl,
|
||||
question: question,
|
||||
subjName: subj,
|
||||
questionData: recData,
|
||||
searchInAllIfNoResult: true,
|
||||
searchTillMatchPercent: 30,
|
||||
},
|
||||
})
|
||||
.then((taskResult) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue