From c2f3f24e51ad6f4235a085ff5edd75d87b7e3ccc Mon Sep 17 00:00:00 2001 From: YourFriendlyNeighborhoodDealer <3167982-YourFriendlyNeighborhoodDealer@users.noreply.gitlab.com> Date: Sun, 9 Dec 2018 17:42:56 +0100 Subject: [PATCH] Logging changes --- server.js | 2 +- stat.js | 38 ++++++++++++++++++++++++++++++++++++-- todos | 4 ++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 2c1fd0a..ec26b4f 100755 --- a/server.js +++ b/server.js @@ -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')); diff --git a/stat.js b/stat.js index 1ddf926..ad774d7 100755 --- a/stat.js +++ b/stat.js @@ -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; } } diff --git a/todos b/todos index 7d3e8f5..58c7804 100644 --- a/todos +++ b/todos @@ -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