Logging changes

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-09 17:42:56 +01:00
parent 11a6150d3a
commit c2f3f24e51
3 changed files with 41 additions and 3 deletions

View file

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

38
stat.js
View file

@ -19,7 +19,7 @@
------------------------------------------------------------------------- */
module.exports = {
Inc: Inc,
LogStat: LogStat,
Load: Load
};
@ -27,9 +27,11 @@ var utils = require('./utils.js');
var logger = require('./logger.js');
const statFile = "stats/stats";
const vStatFile = "stats/vstats";
const writeInterval = 10;
var data = {};
var vData = [];
var writes = 0;
function Load() {
@ -40,6 +42,19 @@ function Load() {
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
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);
}
function Inc(value) {
@ -51,16 +66,35 @@ function Inc(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() {
writes++;
if (writes == writeInterval) {
try {
utils.WriteFile(JSON.stringify(data), statFile);
writes = 0;
// logger.Log("[STAT] Stats wrote.");
} catch (e) {
logger.Log("[STAT]: Error at writing logs!", logger.GetColor("redbg"));
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;
}
}

4
todos
View file

@ -1,2 +1,6 @@
make init function that
creates directory and file structure
redirects, for more stats (ex. to greasyfork: /script
like - dislike button
static public links above textboxes
public questions allow question deletion