mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Handling multiple data files
This commit is contained in:
parent
d9b424cbd1
commit
728931d56e
4 changed files with 172 additions and 112 deletions
|
@ -29,13 +29,19 @@ import fs from 'fs'
|
|||
|
||||
import logger from '../../utils/logger'
|
||||
import utils from '../../utils/utils'
|
||||
import actions from '../../utils/actions'
|
||||
import {
|
||||
processIncomingRequest,
|
||||
logResult,
|
||||
backupData,
|
||||
loadJSON,
|
||||
RecievedData,
|
||||
} from '../../utils/actions'
|
||||
import dbtools from '../../utils/dbtools'
|
||||
import auth from '../../middlewares/auth.middleware'
|
||||
import { dataToString, searchData } from '../../utils/classes'
|
||||
import { dataToString, searchDatas } from '../../utils/classes'
|
||||
|
||||
import { SetupData } from '../../server'
|
||||
import { ModuleType, User } from '../../types/basicTypes'
|
||||
import { ModuleType, User, DataFile } from '../../types/basicTypes'
|
||||
|
||||
// files
|
||||
const msgFile = 'stats/msgs'
|
||||
|
@ -62,11 +68,6 @@ let userDB
|
|||
let url // eslint-disable-line
|
||||
let publicdirs = []
|
||||
|
||||
export interface DataFile {
|
||||
path: string
|
||||
name: string
|
||||
}
|
||||
|
||||
function GetApp(): ModuleType {
|
||||
const app = express()
|
||||
|
||||
|
@ -79,10 +80,21 @@ function GetApp(): ModuleType {
|
|||
const recivedFiles = publicDir + 'recivedfiles'
|
||||
const uloadFiles = publicDir + 'f'
|
||||
const dataFiles: Array<DataFile> = [
|
||||
{ path: `${publicDir}oldData.json`, name: 'oldData' },
|
||||
{ path: `${publicDir}data.json`, name: 'newData' },
|
||||
{
|
||||
path: `${publicDir}oldData.json`,
|
||||
name: 'oldData',
|
||||
shouldSave: (recData: RecievedData): boolean => {
|
||||
return recData.version.includes('2.0.')
|
||||
},
|
||||
},
|
||||
{
|
||||
path: `${publicDir}data.json`,
|
||||
name: 'newData',
|
||||
shouldSave: (recData: RecievedData): boolean => {
|
||||
return recData.version.includes('2.1.')
|
||||
},
|
||||
},
|
||||
]
|
||||
const data: any = {} // TODO: remove
|
||||
const motdFile = publicDir + 'motd'
|
||||
const userSpecificMotdFile = publicDir + 'userSpecificMotd.json'
|
||||
const versionFile = publicDir + 'version'
|
||||
|
@ -127,7 +139,7 @@ function GetApp(): ModuleType {
|
|||
})
|
||||
)
|
||||
|
||||
const questionDbs = actions.LoadJSON(dataFiles)
|
||||
const questionDbs = loadJSON(dataFiles)
|
||||
let version = ''
|
||||
let motd = ''
|
||||
let userSpecificMotd = {}
|
||||
|
@ -196,13 +208,16 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
|
||||
if (write) {
|
||||
utils.WriteFile(JSON.stringify(userSpecificMotd), userSpecificMotdFile)
|
||||
utils.WriteFile(
|
||||
JSON.stringify(userSpecificMotd, null, 2),
|
||||
userSpecificMotdFile
|
||||
)
|
||||
}
|
||||
return shouldSee
|
||||
}
|
||||
|
||||
function Load() {
|
||||
actions.backupData(questionDbs)
|
||||
backupData(questionDbs)
|
||||
|
||||
utils.WatchFile(userSpecificMotdFile, () => {
|
||||
logger.Log(`User Specific Motd updated`, logger.GetColor('green'))
|
||||
|
@ -939,16 +954,9 @@ function GetApp(): ModuleType {
|
|||
|
||||
const dryRun = testUsers.includes(user.id)
|
||||
|
||||
// automatically saves to dataFile every n write
|
||||
// FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future
|
||||
actions
|
||||
.ProcessIncomingRequest(
|
||||
req.body.datatoadd || req.body,
|
||||
questionDbs,
|
||||
dryRun,
|
||||
user
|
||||
)
|
||||
processIncomingRequest(req.body, questionDbs, dryRun, user)
|
||||
.then((resultArray) => {
|
||||
logResult(req.body, resultArray)
|
||||
res.json({
|
||||
success: resultArray.length > 0, // TODO check for -1s
|
||||
newQuestions: resultArray,
|
||||
|
@ -986,8 +994,10 @@ function GetApp(): ModuleType {
|
|||
)
|
||||
}
|
||||
|
||||
searchData(data, question, subj, recData)
|
||||
searchDatas(questionDbs, question, subj, recData)
|
||||
.then((result) => {
|
||||
// TODO: test
|
||||
console.log(result)
|
||||
res.json({
|
||||
result: result,
|
||||
success: true,
|
||||
|
@ -1021,21 +1031,38 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
})
|
||||
|
||||
function getSubjCount(qdbs) {
|
||||
return qdbs.reduce((acc, qdb) => {
|
||||
return acc + qdb.data.length
|
||||
}, 0)
|
||||
}
|
||||
|
||||
function getQuestionCount(qdbs) {
|
||||
return qdbs.reduce((acc, qdb) => {
|
||||
return (
|
||||
acc +
|
||||
qdb.data.reduce((qacc, subject) => {
|
||||
return qacc + subject.Questions.length
|
||||
}, 0)
|
||||
)
|
||||
}, 0)
|
||||
}
|
||||
|
||||
function getSimplreRes() {
|
||||
return {
|
||||
subjects: data.length,
|
||||
questions: data.reduce((acc, subj) => {
|
||||
return acc + subj.Questions.length
|
||||
}, 0),
|
||||
subjects: getSubjCount(questionDbs),
|
||||
questions: getQuestionCount(questionDbs),
|
||||
}
|
||||
}
|
||||
|
||||
function getDetailedRes() {
|
||||
return data.map((subj) => {
|
||||
return {
|
||||
name: subj.Name,
|
||||
count: subj.Questions.length,
|
||||
}
|
||||
return questionDbs.map((qdb) => {
|
||||
return qdb.data.map((subj) => {
|
||||
return {
|
||||
name: subj.Name,
|
||||
count: subj.Questions.length,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1091,10 +1118,8 @@ function GetApp(): ModuleType {
|
|||
utils.AppendToFile(
|
||||
JSON.stringify({
|
||||
date: utils.GetDateString(),
|
||||
subjectCount: data.length,
|
||||
questionCount: data.reduce((acc, subj) => {
|
||||
return acc + subj.Questions.length
|
||||
}, 0),
|
||||
subjectCount: getSubjCount(questionDbs),
|
||||
questionCount: getQuestionCount(questionDbs),
|
||||
userCount: dbtools.TableInfo(userDB, 'users').dataCount,
|
||||
}),
|
||||
dailyDataCountFile
|
||||
|
@ -1165,7 +1190,7 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
|
||||
function DailyAction() {
|
||||
actions.backupData(data)
|
||||
backupData(questionDbs)
|
||||
BackupDB()
|
||||
ExportDailyDataCount()
|
||||
IncrementAvaiblePWs()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue