mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
closes #6539
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
var cronJob = require('cron').CronJob;
|
var cronJob = require('cron').CronJob;
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
|
var nconf = require('nconf');
|
||||||
|
var crypto = require('crypto');
|
||||||
|
|
||||||
var db = require('./database');
|
var db = require('./database');
|
||||||
|
|
||||||
@@ -14,6 +16,18 @@ var pageViews = 0;
|
|||||||
var uniqueIPCount = 0;
|
var uniqueIPCount = 0;
|
||||||
var uniquevisitors = 0;
|
var uniquevisitors = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: allow the cache's max value to be configurable. On high-traffic installs,
|
||||||
|
* the cache could be exhausted continuously if there are more than 500 concurrently
|
||||||
|
* active users
|
||||||
|
*/
|
||||||
|
var LRU = require('lru-cache');
|
||||||
|
var ipCache = LRU({
|
||||||
|
max: 500,
|
||||||
|
length: function () { return 1; },
|
||||||
|
maxAge: 0,
|
||||||
|
});
|
||||||
|
|
||||||
new cronJob('*/10 * * * * *', function () {
|
new cronJob('*/10 * * * * *', function () {
|
||||||
Analytics.writeData();
|
Analytics.writeData();
|
||||||
}, null, true);
|
}, null, true);
|
||||||
@@ -35,7 +49,14 @@ Analytics.pageView = function (payload) {
|
|||||||
pageViews += 1;
|
pageViews += 1;
|
||||||
|
|
||||||
if (payload.ip) {
|
if (payload.ip) {
|
||||||
db.sortedSetScore('ip:recent', payload.ip, function (err, score) {
|
// Retrieve hash or calculate if not present
|
||||||
|
let hash = ipCache.get(payload.ip + nconf.get('secret'));
|
||||||
|
if (!hash) {
|
||||||
|
hash = crypto.createHash('sha1').update(payload.ip + nconf.get('secret')).digest('hex');
|
||||||
|
ipCache.set(payload.ip + nconf.get('secret'), hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.sortedSetScore('ip:recent', hash, function (err, score) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -46,7 +67,7 @@ Analytics.pageView = function (payload) {
|
|||||||
today.setHours(today.getHours(), 0, 0, 0);
|
today.setHours(today.getHours(), 0, 0, 0);
|
||||||
if (!score || score < today.getTime()) {
|
if (!score || score < today.getTime()) {
|
||||||
uniquevisitors += 1;
|
uniquevisitors += 1;
|
||||||
db.sortedSetAdd('ip:recent', Date.now(), payload.ip);
|
db.sortedSetAdd('ip:recent', Date.now(), hash);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user