mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Domain specific data files
This commit is contained in:
		| @@ -33,9 +33,8 @@ import { | ||||
|   processIncomingRequest, | ||||
|   logResult, | ||||
|   backupData, | ||||
|   shouldSaveDataFile, | ||||
|   loadJSON, | ||||
|   getQuestionDbsWithoutFunct, | ||||
|   RecievedData, | ||||
|   Result, | ||||
| } from '../../utils/actions' | ||||
| import dbtools from '../../utils/dbtools' | ||||
| @@ -48,7 +47,13 @@ import { | ||||
| } from '../../utils/workerPool' | ||||
|  | ||||
| import { SetupData } from '../../server' | ||||
| import { ModuleType, User, DataFile, Request } from '../../types/basicTypes' | ||||
| import { | ||||
|   ModuleType, | ||||
|   User, | ||||
|   DataFile, | ||||
|   Request, | ||||
|   QuestionDb, | ||||
| } from '../../types/basicTypes' | ||||
|  | ||||
| // files | ||||
| const msgFile = 'stats/msgs' | ||||
| @@ -65,6 +70,7 @@ const todosFile = 'data/todos.json' | ||||
| const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js' | ||||
| const rootRedirectToFile = 'data/apiRootRedirectTo' | ||||
| const recievedQuestionFile = 'stats/recievedQuestions' | ||||
| const dbsFile = 'data/dbs.json' | ||||
|  | ||||
| // other constants | ||||
| const line = '====================================================' // lol | ||||
| @@ -90,30 +96,7 @@ function GetApp(): ModuleType { | ||||
|   // files in public dirs | ||||
|   const recivedFiles = publicDir + 'recivedfiles' | ||||
|   const uloadFiles = publicDir + 'f' | ||||
|   // FIXME: this to seperate file? | ||||
|   const dataFiles: Array<DataFile> = [ | ||||
|     { | ||||
|       path: `${publicDir}oldData.json`, | ||||
|       name: 'oldData', | ||||
|       shouldSave: (recData: RecievedData): boolean => { | ||||
|         return recData.version.startsWith('2.0.') | ||||
|       }, | ||||
|     }, | ||||
|     { | ||||
|       path: `${publicDir}data.json`, | ||||
|       name: 'newData', | ||||
|       shouldSave: (recData: RecievedData): boolean => { | ||||
|         return recData.version.startsWith('2.1.') | ||||
|       }, | ||||
|     }, | ||||
|     { | ||||
|       path: `${publicDir}fromwebsiteData.json`, | ||||
|       name: 'fromwebsiteData', | ||||
|       shouldSave: (recData: RecievedData): boolean => { | ||||
|         return recData.version === 'WEBSITE' | ||||
|       }, | ||||
|     }, | ||||
|   ] | ||||
|   const dataFiles: Array<DataFile> = utils.ReadJSON(dbsFile) | ||||
|   const motdFile = publicDir + 'motd' | ||||
|   const userSpecificMotdFile = publicDir + 'userSpecificMotd.json' | ||||
|  | ||||
| @@ -161,7 +144,7 @@ function GetApp(): ModuleType { | ||||
|     }) | ||||
|   ) | ||||
|  | ||||
|   const questionDbs = loadJSON(dataFiles) | ||||
|   const questionDbs = loadJSON(dataFiles, publicDir) | ||||
|   let version = '' | ||||
|   let rootRedirectURL = '' | ||||
|   let motd = '' | ||||
| @@ -169,7 +152,7 @@ function GetApp(): ModuleType { | ||||
|   // FIXME: check type from file | ||||
|   let testUsers: any = [] | ||||
|  | ||||
|   initWorkerPool(getQuestionDbsWithoutFunct(questionDbs)) | ||||
|   initWorkerPool(questionDbs) | ||||
|  | ||||
|   function mergeObjSum(a, b) { | ||||
|     const res = { ...b } | ||||
| @@ -931,7 +914,61 @@ function GetApp(): ModuleType { | ||||
|     const dryRun = testUsers.includes(user.id) | ||||
|  | ||||
|     try { | ||||
|       processIncomingRequest(req.body, questionDbs, dryRun, user) | ||||
|       let maxIndex = -1 | ||||
|       const suitedQuestionDbs = questionDbs.filter((qdb) => { | ||||
|         if (maxIndex < qdb.index) { | ||||
|           maxIndex = qdb.index | ||||
|         } | ||||
|         return shouldSaveDataFile(qdb, req.body) | ||||
|       }, []) | ||||
|  | ||||
|       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', | ||||
|         }) | ||||
|       } | ||||
|  | ||||
|       processIncomingRequest(req.body, suitedQuestionDbs, dryRun, user) | ||||
|         .then((resultArray: Array<Result>) => { | ||||
|           logResult(req.body, resultArray, user.id, dryRun) | ||||
|  | ||||
| @@ -947,7 +984,7 @@ function GetApp(): ModuleType { | ||||
|  | ||||
|           if (totalNewQuestions > 0) { | ||||
|             msgAllWorker({ | ||||
|               qdbs: getQuestionDbsWithoutFunct(questionDbs), | ||||
|               qdbs: questionDbs, | ||||
|               type: 'update', | ||||
|             }) | ||||
|           } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user