Stat.js merge with logger.js, added ids.js

This commit is contained in:
MrFry 2019-10-28 14:29:18 +01:00
parent 892bfff54e
commit 5dfcb7d8ae
4 changed files with 112 additions and 35 deletions

View file

@ -23,7 +23,9 @@ module.exports = {
Log: Log,
GetColor: GetColor,
LogReq: LogReq,
setNoLogReadInterval: setNoLogReadInterval
LogStat: LogStat,
Load: Load,
logHashed: logHashed
}
const DELIM = C('green') + '|' + C()
@ -32,6 +34,14 @@ const utils = require('../utils/utils.js')
const locLogFile = './stats/logs'
const logFile = '/nlogs/nlogs'
const allLogFile = '/nlogs/log'
const statFile = 'stats/stats'
const vStatFile = 'stats/vstats'
const writeInterval = 10
let data = {}
let vData = {}
let writes = 0
const colors = [
'green',
@ -44,13 +54,6 @@ const colors = [
let noLogips = []
function setNoLogReadInterval () {
noLogips = utils.ReadFile('./nolog').split('\n')
setInterval(() => {
noLogips = utils.ReadFile('./nolog').split('\n')
}, 1000 * 60 * 30)
}
function GetDateString () {
const m = new Date()
const d = m.getFullYear() + '/' +
@ -109,6 +112,78 @@ function LogReq (req, toFile, sc) {
}
}
function setNoLogReadInterval () {
noLogips = utils.ReadFile('./nolog').split('\n')
setInterval(() => {
noLogips = utils.ReadFile('./nolog').split('\n')
}, 1000 * 60 * 30)
}
function Load () {
try {
var prevData = utils.ReadFile(statFile)
data = JSON.parse(prevData)
} catch (e) {
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
console.log(e)
}
try {
var prevVData = utils.ReadFile(vStatFile)
vData = JSON.parse(prevVData)
} catch (e) {
Log('Error at loading visit logs! (@ first run its normal)', GetColor('redbg'))
console.log(e)
}
setNoLogReadInterval()
}
function LogStat (url) {
url = url.split('?')[0]
Inc(url)
AddVisitStat(url)
Save()
}
function Inc (value) {
if (value.startsWith('/?')) { value = '/' }
if (data[value] === undefined) { data[value] = 0 }
data[value]++
}
function AddVisitStat (name) {
var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2)
if (vData[now] === undefined) { vData[now] = {} }
if (vData[now][name] === undefined) { vData[now][name] = 0 }
vData[now][name]++
}
function Save () {
writes++
if (writes === writeInterval) {
try {
utils.WriteFile(JSON.stringify(data), statFile)
// Log("Stats wrote.");
} catch (e) {
Log('Error at writing logs!', GetColor('redbg'))
console.log(e)
}
try {
utils.WriteFile(JSON.stringify(vData), vStatFile)
// Log("Stats wrote.");
} catch (e) {
Log('Error at writing visit logs!', GetColor('redbg'))
console.log(e)
}
writes = 0
}
}
function logHashed (x) {
return GetRandomColor(x.toString()) + x + C()
}
function GetRandomColor (ip) {
if (!ip) {
return 'red'