Added some bit advanced statistic logging

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-11-29 16:43:43 +01:00
parent 06dc1078bb
commit 02c513c090
2 changed files with 38 additions and 4 deletions

View file

@ -69,7 +69,7 @@ app.use(function(req, res, next) {
res.on('finish', function() { res.on('finish', function() {
Log(req, true); Log(req, true);
}); });
stat.Inc(req.url); stat.LogStat(req.url);
next(); next();
}); });
app.use(express.static('public')); app.use(express.static('public'));

40
stat.js
View file

@ -19,7 +19,7 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
module.exports = { module.exports = {
Inc: Inc, LogStat: LogStat,
Load: Load Load: Load
}; };
@ -27,9 +27,11 @@ var utils = require('./utils.js');
var logger = require('./logger.js'); var logger = require('./logger.js');
const statFile = "stats/stats"; const statFile = "stats/stats";
const vStatFile = "stats/vstats";
const writeInterval = 10; const writeInterval = 10;
var data = {}; var data = {};
var vData = {};
var writes = 0; var writes = 0;
function Load() { function Load() {
@ -40,6 +42,20 @@ function Load() {
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg")); logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
console.log(e); console.log(e);
} }
try {
var prevVData = utils.ReadFile(vStatFile);
vData = JSON.parse(prevVData);
} catch (e) {
logger.Log("[STAT]: Error at loading visit logs!", logger.GetColor("redbg"));
console.log(e);
}
}
function LogStat(url) {
Inc(url);
AddVisitStat(url);
Save();
} }
function Inc(value) { function Inc(value) {
@ -48,7 +64,18 @@ function Inc(value) {
if (data[value] == undefined) if (data[value] == undefined)
data[value] = 0; data[value] = 0;
data[value]++; data[value]++;
Save(); }
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() { function Save() {
@ -56,11 +83,18 @@ function Save() {
if (writes == writeInterval) { if (writes == writeInterval) {
try { try {
utils.WriteFile(JSON.stringify(data), statFile); utils.WriteFile(JSON.stringify(data), statFile);
writes = 0;
// logger.Log("[STAT] Stats wrote."); // logger.Log("[STAT] Stats wrote.");
} catch (e) { } catch (e) {
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg")); logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
console.log(e); console.log(e);
} }
try {
utils.WriteFile(JSON.stringify(vData), vStatFile);
// logger.Log("[STAT] Stats wrote.");
} catch (e) {
logger.Log("[STAT]: Error at writing visit logs!", logger.GetColor("redbg"));
console.log(e);
}
writes = 0;
} }
} }