Date string update, midnight timer update, other stuff

This commit is contained in:
MrFry 2020-04-07 15:00:39 +02:00
parent c764c4f402
commit dbb23a6724
6 changed files with 92 additions and 61 deletions

View file

@ -1,4 +1,5 @@
const logger = require('../utils/logger.js')
const utils = require('../utils/utils.js')
const dbtools = require('../utils/dbtools.js')
module.exports = function (options) {
@ -52,14 +53,14 @@ module.exports = function (options) {
UpdateAccess(authDB, user, ip, sessionID)
dbtools.Update(authDB, 'sessions', {
lastAccess: new Date().toString()
lastAccess: utils.GetDateString()
}, {
id: sessionID
})
dbtools.Update(authDB, 'users', {
lastIP: ip,
lastAccess: new Date().toString()
lastAccess: utils.GetDateString()
}, {
id: user.id
})
@ -79,7 +80,7 @@ function UpdateAccess (db, user, ip, sessionID) {
userID: user.id,
ip: ip,
sessionID: sessionID,
date: new Date().toString()
date: utils.GetDateString()
})
}
}

View file

@ -45,8 +45,12 @@ const passwordFile = 'data/dataEditorPasswords.json'
const dataEditsLog = 'stats/dataEdits'
const dailyDataCountFile = 'stats/dailyDataCount'
const usersDBPath = 'data/dbs/users.db'
const usersDbBackupPath = 'data/dbs/backup'
const maxVeteranPwGetCount = 5
const addPWPerDay = 3 // every x day a user can give a pw
const maxPWCount = 2 // maximum pw give opportunities a user can have at once
const daysAfterUserGetsPWs = 2 // days after user gets pw-s
if (!utils.FileExists(usersDBPath)) {
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
@ -137,7 +141,7 @@ app.post('/getpw', function (req, res) {
const pw = uuidv4()
const insertRes = dbtools.Insert(authDB, 'users', {
pw: pw,
created: new Date().toString()
created: utils.GetDateString()
})
logger.Log(`User #${requestingUser.id} creted new user #${insertRes.lastInsertRowid}`, logger.GetColor('cyan'))
@ -170,7 +174,7 @@ app.post('/getveteranpw', function (req, res) {
} else {
dbtools.Update(authDB, 'veteranPWRequests', {
count: tries.count + 1,
lastDate: new Date().toString()
lastDate: utils.GetDateString()
}, {
id: tries.id
})
@ -178,7 +182,7 @@ app.post('/getveteranpw', function (req, res) {
} else {
dbtools.Insert(authDB, 'veteranPWRequests', {
ip: ip,
lastDate: new Date().toString()
lastDate: utils.GetDateString()
})
}
@ -253,7 +257,7 @@ app.post('/login', (req, res) => {
dbtools.Update(authDB, 'users', {
loginCount: user.loginCount + 1,
lastIP: ip,
lastLogin: new Date().toString()
lastLogin: utils.GetDateString()
}, {
id: user.id
})
@ -262,7 +266,7 @@ app.post('/login', (req, res) => {
id: sessionID,
ip: ip,
userID: user.id,
createDate: new Date().toString()
createDate: utils.GetDateString()
})
// TODO: cookie age
@ -315,7 +319,7 @@ app.post('/postfeedbackfile', function (req, res) {
app.post('/postfeedback', function (req, res) {
logger.LogReq(req)
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
utils.AppendToFile(logger.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
utils.AppendToFile(utils.GetDateString() + ':\n' + JSON.stringify(req.body), msgFile)
res.json({ success: true })
})
@ -403,7 +407,7 @@ app.post('/uploaddata', (req, res) => {
// returning if user password is not ok
if (!user) {
logger.Log(`Data upload: invalid password ${password}`, logger.GetColor('red'))
utils.AppendToFile(logger.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
utils.AppendToFile(utils.GetDateString() + '\n' + password + '(FAILED PASSWORD)\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
res.json({ status: respStatuses.invalidPass })
return
}
@ -411,10 +415,10 @@ app.post('/uploaddata', (req, res) => {
logger.Log(`Password accepted for ${user.name}`, logger.GetColor('bluebg'))
logger.Log(`Old Subjects/Questions: ${initialCount.subjectCount} / ${initialCount.questionCount} | New: ${count.subjectCount} / ${count.questionCount} | Edited question count: ${Object.keys(editedQuestions).length}`, logger.GetColor('bluebg'))
// saving detailed editedCount
utils.AppendToFile(logger.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
utils.AppendToFile(utils.GetDateString() + '\n' + JSON.stringify(user) + '\n' + JSON.stringify(editedQuestions) + '\n\n', dataEditsLog)
// making backup
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${new Date().toString().replace(/ /g, '_')}`)
utils.CopyFile('./' + dataFile, `./public/backs/data_before_${user.name}_${utils.GetDateString().replace(/ /g, '_')}`)
logger.Log('Backup made')
// writing data
utils.WriteFile(JSON.stringify(newData), dataFile)
@ -550,8 +554,9 @@ app.post('*', function (req, res) {
})
function ExportDailyDataCount () {
logger.Log('Saving daily data count ...')
utils.AppendToFile(JSON.stringify({
date: new Date(),
date: utils.GetDateString(),
subjectCount: data.Subjects.length,
questionCOunt: data.Subjects.reduce((acc, subj) => {
return acc + subj.Questions.length
@ -560,6 +565,47 @@ function ExportDailyDataCount () {
}), dailyDataCountFile)
}
function BackupDB () {
logger.Log('Backing up auth DB ...')
utils.CreatePath(usersDbBackupPath, true)
authDB.backup(`${usersDbBackupPath}/users.${utils.GetDateString().replace(/ /g, '_')}.db`)
.then(() => {
logger.Log('Auth DB backup complete!')
})
.catch((err) => {
logger.Log('Auth DB backup failed!', logger.GetColor('redbg'))
console.error(err)
})
}
function IncrementAvaiblePWs () {
const users = dbtools.SelectAll(authDB, 'users')
const today = new Date()
const getDayDiff = (dateString) => {
let msdiff = today - new Date(dateString)
return Math.floor(msdiff / (1000 * 3600 * 24))
}
users.forEach((u) => {
if (u.avaiblePWRequests >= maxPWCount) {
return
}
const dayDiff = getDayDiff(u.created)
if (dayDiff < daysAfterUserGetsPWs) {
return
}
if (dayDiff % addPWPerDay === 0) {
dbtools.Update(authDB, 'users', {
avaiblePWRequests: u.avaiblePWRequests + 1
}, {
id: u.id
})
}
})
}
exports.app = app
exports.cleanup = () => {
logger.Log('Closing Auth DB')
@ -567,8 +613,8 @@ exports.cleanup = () => {
}
exports.dailyAction = () => {
ExportDailyDataCount()
// TODO: selectAll from users, check if date is more than x, and increment every y
BackupDB()
IncrementAvaiblePWs()
}
logger.Log('API module started', logger.GetColor('yellow'))

View file

@ -61,6 +61,7 @@ process.on('SIGTERM', () => exit('SIGTERM'))
process.on('SIGTERM', () => exit('SIGTERM'))
function exit (reason) {
console.log()
logger.Log(`Exiting, reason: ${reason}`)
Object.keys(modules).forEach((k, i) => {
const x = modules[k]
@ -134,25 +135,24 @@ if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFil
}
}
setLogTimer()
function setLogTimer () {
const d = new Date()
const h = new Date(
d.getFullYear(),
d.getMonth(),
d.getDate() + 1,
0,
0,
0,
0
const now = new Date()
const night = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1, // the next day, ...
0, 0, 0 // ...at 00:00:00 hours
)
const e = h - d
const msToMidnight = night.getTime() - now.getTime()
if (e > 100) {
setTimeout(setLogTimer, e)
} else {
logger.Log('Log timer malfunction :/', logger.GetColor('redbg'))
}
setTimeout(function () {
LogTimerAction()
setLogTimer()
}, msToMidnight)
}
function LogTimerAction () {
Object.keys(modules).forEach((k, i) => {
const x = modules[k]
if (x.dailyAction) {
@ -171,8 +171,6 @@ function setLogTimer () {
utils.AppendToFile(line, allLogFile)
}
setLogTimer()
logger.Log('Node version: ' + process.version)
logger.Log('Current working directory: ' + process.cwd())
logger.Log('Listening on port: ' + port)

View file

@ -11,15 +11,6 @@ let authDB
console.clear()
CreateDB()
// authDB.backup(usersDBPath)
// .then(() => {
// logger.Log('backup complete!')
// })
// .catch((err) => {
// logger.Log('backup failed!', logger.GetColor('redbg'))
// console.log(err)
// })
authDB.close()
function CreateDB () {
@ -44,7 +35,7 @@ function CreateDB () {
pw: uuidv4(),
oldCID: cid,
avaiblePWRequests: 4,
created: new Date().toString()
created: utils.GetDateString()
})
} catch (e) {
logger.Log('Error during inserting', logger.GetColor('redbg'))
@ -55,7 +46,7 @@ function CreateDB () {
console.error(e)
}
const dir = `./dbSetupResult/${GetDateString().replace(/ /g, '_')}`
const dir = `./dbSetupResult/${utils.GetDateString().replace(/ /g, '_')}`
utils.CreatePath(dir)
Object.keys(dbStruct).forEach((key) => {
const path = `${dir}/${key}.json`
@ -68,14 +59,3 @@ function CreateDB () {
logger.Log('Done')
}
function GetDateString () {
const m = new Date()
const d = m.getFullYear() + '-' +
('0' + (m.getMonth() + 1)).slice(-2) + '-' +
('0' + m.getDate()).slice(-2) + ' ' +
('0' + m.getHours()).slice(-2) + ':' +
('0' + m.getMinutes()).slice(-2) + ':' +
('0' + m.getSeconds()).slice(-2)
return d
}

View file

@ -64,12 +64,7 @@ let noLogips = []
function GetDateString () {
const m = new Date()
const d = m.getFullYear() + '/' +
('0' + (m.getMonth() + 1)).slice(-2) + '/' +
('0' + m.getDate()).slice(-2) + ' ' +
('0' + m.getHours()).slice(-2) + ':' +
('0' + m.getMinutes()).slice(-2) + ':' +
('0' + m.getSeconds()).slice(-2)
const d = utils.GetDateString()
return GetRandomColor(m.getHours().toString()) + d + C()
}

View file

@ -10,7 +10,8 @@ module.exports = {
CreatePath: CreatePath,
WatchFile: WatchFile,
ReadDir: ReadDir,
CopyFile: CopyFile
CopyFile: CopyFile,
GetDateString: GetDateString
}
var fs = require('fs')
@ -19,6 +20,16 @@ var logger = require('../utils/logger.js')
const dataFile = './public/data.json'
function GetDateString () {
const m = new Date()
return m.getFullYear() + '-' +
('0' + (m.getMonth() + 1)).slice(-2) + '-' +
('0' + m.getDate()).slice(-2) + ' ' +
('0' + m.getHours()).slice(-2) + ':' +
('0' + m.getMinutes()).slice(-2) + ':' +
('0' + m.getSeconds()).slice(-2)
}
function CopyFile (from, to) {
CreatePath(to)
fs.copyFileSync(from, to)