mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 22:45:46 +01:00
analytics tests, reduce bcrypt rouds for tests
This commit is contained in:
326
src/analytics.js
326
src/analytics.js
@@ -6,192 +6,194 @@ var winston = require('winston');
|
|||||||
|
|
||||||
var db = require('./database');
|
var db = require('./database');
|
||||||
|
|
||||||
(function (Analytics) {
|
var Analytics = module.exports;
|
||||||
var counters = {};
|
|
||||||
|
|
||||||
var pageViews = 0;
|
var counters = {};
|
||||||
var uniqueIPCount = 0;
|
|
||||||
var uniquevisitors = 0;
|
|
||||||
|
|
||||||
var isCategory = /^(?:\/api)?\/category\/(\d+)/;
|
var pageViews = 0;
|
||||||
|
var uniqueIPCount = 0;
|
||||||
|
var uniquevisitors = 0;
|
||||||
|
|
||||||
new cronJob('*/10 * * * *', function () {
|
var isCategory = /^(?:\/api)?\/category\/(\d+)/;
|
||||||
Analytics.writeData();
|
|
||||||
}, null, true);
|
|
||||||
|
|
||||||
Analytics.increment = function (keys) {
|
new cronJob('*/10 * * * *', function () {
|
||||||
keys = Array.isArray(keys) ? keys : [keys];
|
Analytics.writeData();
|
||||||
|
}, null, true);
|
||||||
|
|
||||||
keys.forEach(function (key) {
|
Analytics.increment = function (keys) {
|
||||||
counters[key] = counters[key] || 0;
|
keys = Array.isArray(keys) ? keys : [keys];
|
||||||
++counters[key];
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Analytics.pageView = function (payload) {
|
keys.forEach(function (key) {
|
||||||
++pageViews;
|
counters[key] = counters[key] || 0;
|
||||||
|
++counters[key];
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (payload.ip) {
|
Analytics.pageView = function (payload) {
|
||||||
db.sortedSetScore('ip:recent', payload.ip, function (err, score) {
|
++pageViews;
|
||||||
if (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!score) {
|
|
||||||
++uniqueIPCount;
|
|
||||||
}
|
|
||||||
var today = new Date();
|
|
||||||
today.setHours(today.getHours(), 0, 0, 0);
|
|
||||||
if (!score || score < today.getTime()) {
|
|
||||||
++uniquevisitors;
|
|
||||||
db.sortedSetAdd('ip:recent', Date.now(), payload.ip);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (payload.path) {
|
if (payload.ip) {
|
||||||
var categoryMatch = payload.path.match(isCategory),
|
db.sortedSetScore('ip:recent', payload.ip, function (err, score) {
|
||||||
cid = categoryMatch ? parseInt(categoryMatch[1], 10) : null;
|
|
||||||
|
|
||||||
if (cid) {
|
|
||||||
Analytics.increment(['pageviews:byCid:' + cid]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Analytics.writeData = function () {
|
|
||||||
var today = new Date();
|
|
||||||
var month = new Date();
|
|
||||||
var dbQueue = [];
|
|
||||||
|
|
||||||
today.setHours(today.getHours(), 0, 0, 0);
|
|
||||||
month.setMonth(month.getMonth(), 1);
|
|
||||||
month.setHours(0, 0, 0, 0);
|
|
||||||
|
|
||||||
if (pageViews > 0) {
|
|
||||||
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:pageviews', pageViews, today.getTime()));
|
|
||||||
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:pageviews:month', pageViews, month.getTime()));
|
|
||||||
pageViews = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uniquevisitors > 0) {
|
|
||||||
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:uniquevisitors', uniquevisitors, today.getTime()));
|
|
||||||
uniquevisitors = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uniqueIPCount > 0) {
|
|
||||||
dbQueue.push(async.apply(db.incrObjectFieldBy, 'global', 'uniqueIPCount', uniqueIPCount));
|
|
||||||
uniqueIPCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(counters).length > 0) {
|
|
||||||
for(var key in counters) {
|
|
||||||
if (counters.hasOwnProperty(key)) {
|
|
||||||
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:' + key, counters[key], today.getTime()));
|
|
||||||
delete counters[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async.parallel(dbQueue, function (err) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('[analytics] Encountered error while writing analytics to data store: ' + err.message);
|
return;
|
||||||
|
}
|
||||||
|
if (!score) {
|
||||||
|
++uniqueIPCount;
|
||||||
|
}
|
||||||
|
var today = new Date();
|
||||||
|
today.setHours(today.getHours(), 0, 0, 0);
|
||||||
|
if (!score || score < today.getTime()) {
|
||||||
|
++uniquevisitors;
|
||||||
|
db.sortedSetAdd('ip:recent', Date.now(), payload.ip);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
Analytics.getHourlyStatsForSet = function (set, hour, numHours, callback) {
|
if (payload.path) {
|
||||||
var terms = {},
|
var categoryMatch = payload.path.match(isCategory),
|
||||||
hoursArr = [];
|
cid = categoryMatch ? parseInt(categoryMatch[1], 10) : null;
|
||||||
|
|
||||||
hour = new Date(hour);
|
if (cid) {
|
||||||
hour.setHours(hour.getHours(), 0, 0, 0);
|
Analytics.increment(['pageviews:byCid:' + cid]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (var i = 0, ii = numHours; i < ii; i++) {
|
Analytics.writeData = function (callback) {
|
||||||
hoursArr.push(hour.getTime());
|
callback = callback || function () {};
|
||||||
hour.setHours(hour.getHours() - 1, 0, 0, 0);
|
var today = new Date();
|
||||||
|
var month = new Date();
|
||||||
|
var dbQueue = [];
|
||||||
|
|
||||||
|
today.setHours(today.getHours(), 0, 0, 0);
|
||||||
|
month.setMonth(month.getMonth(), 1);
|
||||||
|
month.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
if (pageViews > 0) {
|
||||||
|
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:pageviews', pageViews, today.getTime()));
|
||||||
|
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:pageviews:month', pageViews, month.getTime()));
|
||||||
|
pageViews = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uniquevisitors > 0) {
|
||||||
|
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:uniquevisitors', uniquevisitors, today.getTime()));
|
||||||
|
uniquevisitors = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uniqueIPCount > 0) {
|
||||||
|
dbQueue.push(async.apply(db.incrObjectFieldBy, 'global', 'uniqueIPCount', uniqueIPCount));
|
||||||
|
uniqueIPCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(counters).length > 0) {
|
||||||
|
for(var key in counters) {
|
||||||
|
if (counters.hasOwnProperty(key)) {
|
||||||
|
dbQueue.push(async.apply(db.sortedSetIncrBy, 'analytics:' + key, counters[key], today.getTime()));
|
||||||
|
delete counters[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async.parallel(dbQueue, function (err) {
|
||||||
|
if (err) {
|
||||||
|
winston.error('[analytics] Encountered error while writing analytics to data store: ' + err.message);
|
||||||
|
}
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Analytics.getHourlyStatsForSet = function (set, hour, numHours, callback) {
|
||||||
|
var terms = {},
|
||||||
|
hoursArr = [];
|
||||||
|
|
||||||
|
hour = new Date(hour);
|
||||||
|
hour.setHours(hour.getHours(), 0, 0, 0);
|
||||||
|
|
||||||
|
for (var i = 0, ii = numHours; i < ii; i++) {
|
||||||
|
hoursArr.push(hour.getTime());
|
||||||
|
hour.setHours(hour.getHours() - 1, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.sortedSetScores(set, hoursArr, function (err, counts) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.sortedSetScores(set, hoursArr, function (err, counts) {
|
hoursArr.forEach(function (term, index) {
|
||||||
|
terms[term] = parseInt(counts[index], 10) || 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
var termsArr = [];
|
||||||
|
|
||||||
|
hoursArr.reverse();
|
||||||
|
hoursArr.forEach(function (hour) {
|
||||||
|
termsArr.push(terms[hour]);
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(null, termsArr);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Analytics.getDailyStatsForSet = function (set, day, numDays, callback) {
|
||||||
|
var daysArr = [];
|
||||||
|
|
||||||
|
day = new Date(day);
|
||||||
|
day.setDate(day.getDate() + 1); // set the date to tomorrow, because getHourlyStatsForSet steps *backwards* 24 hours to sum up the values
|
||||||
|
day.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
async.whilst(function () {
|
||||||
|
return numDays--;
|
||||||
|
}, function (next) {
|
||||||
|
Analytics.getHourlyStatsForSet(set, day.getTime() - (1000 * 60 * 60 * 24 * numDays), 24, function (err, day) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
hoursArr.forEach(function (term, index) {
|
daysArr.push(day.reduce(function (cur, next) {
|
||||||
terms[term] = parseInt(counts[index], 10) || 0;
|
return cur + next;
|
||||||
});
|
}));
|
||||||
|
next();
|
||||||
var termsArr = [];
|
|
||||||
|
|
||||||
hoursArr.reverse();
|
|
||||||
hoursArr.forEach(function (hour) {
|
|
||||||
termsArr.push(terms[hour]);
|
|
||||||
});
|
|
||||||
|
|
||||||
callback(null, termsArr);
|
|
||||||
});
|
});
|
||||||
};
|
}, function (err) {
|
||||||
|
callback(err, daysArr);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
Analytics.getDailyStatsForSet = function (set, day, numDays, callback) {
|
Analytics.getUnwrittenPageviews = function () {
|
||||||
var daysArr = [];
|
return pageViews;
|
||||||
|
};
|
||||||
|
|
||||||
day = new Date(day);
|
Analytics.getMonthlyPageViews = function (callback) {
|
||||||
day.setDate(day.getDate() + 1); // set the date to tomorrow, because getHourlyStatsForSet steps *backwards* 24 hours to sum up the values
|
var thisMonth = new Date();
|
||||||
day.setHours(0, 0, 0, 0);
|
var lastMonth = new Date();
|
||||||
|
thisMonth.setMonth(thisMonth.getMonth(), 1);
|
||||||
|
thisMonth.setHours(0, 0, 0, 0);
|
||||||
|
lastMonth.setMonth(thisMonth.getMonth() - 1, 1);
|
||||||
|
lastMonth.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
async.whilst(function () {
|
var values = [thisMonth.getTime(), lastMonth.getTime()];
|
||||||
return numDays--;
|
|
||||||
}, function (next) {
|
|
||||||
Analytics.getHourlyStatsForSet(set, day.getTime() - (1000 * 60 * 60 * 24 * numDays), 24, function (err, day) {
|
|
||||||
if (err) {
|
|
||||||
return next(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
daysArr.push(day.reduce(function (cur, next) {
|
db.sortedSetScores('analytics:pageviews:month', values, function (err, scores) {
|
||||||
return cur + next;
|
if (err) {
|
||||||
}));
|
return callback(err);
|
||||||
next();
|
}
|
||||||
});
|
callback(null, {thisMonth: scores[0] || 0, lastMonth: scores[1] || 0});
|
||||||
}, function (err) {
|
});
|
||||||
callback(err, daysArr);
|
};
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Analytics.getUnwrittenPageviews = function () {
|
Analytics.getCategoryAnalytics = function (cid, callback) {
|
||||||
return pageViews;
|
async.parallel({
|
||||||
};
|
'pageviews:hourly': async.apply(Analytics.getHourlyStatsForSet, 'analytics:pageviews:byCid:' + cid, Date.now(), 24),
|
||||||
|
'pageviews:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:pageviews:byCid:' + cid, Date.now(), 30),
|
||||||
|
'topics:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:topics:byCid:' + cid, Date.now(), 7),
|
||||||
|
'posts:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:posts:byCid:' + cid, Date.now(), 7),
|
||||||
|
}, callback);
|
||||||
|
};
|
||||||
|
|
||||||
Analytics.getMonthlyPageViews = function (callback) {
|
Analytics.getErrorAnalytics = function (callback) {
|
||||||
var thisMonth = new Date();
|
async.parallel({
|
||||||
var lastMonth = new Date();
|
'not-found': async.apply(Analytics.getDailyStatsForSet, 'analytics:errors:404', Date.now(), 7),
|
||||||
thisMonth.setMonth(thisMonth.getMonth(), 1);
|
'toobusy': async.apply(Analytics.getDailyStatsForSet, 'analytics:errors:503', Date.now(), 7)
|
||||||
thisMonth.setHours(0, 0, 0, 0);
|
}, callback);
|
||||||
lastMonth.setMonth(thisMonth.getMonth() - 1, 1);
|
};
|
||||||
lastMonth.setHours(0, 0, 0, 0);
|
|
||||||
|
|
||||||
var values = [thisMonth.getTime(), lastMonth.getTime()];
|
|
||||||
|
|
||||||
db.sortedSetScores('analytics:pageviews:month', values, function (err, scores) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {thisMonth: scores[0] || 0, lastMonth: scores[1] || 0});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Analytics.getCategoryAnalytics = function (cid, callback) {
|
|
||||||
async.parallel({
|
|
||||||
'pageviews:hourly': async.apply(Analytics.getHourlyStatsForSet, 'analytics:pageviews:byCid:' + cid, Date.now(), 24),
|
|
||||||
'pageviews:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:pageviews:byCid:' + cid, Date.now(), 30),
|
|
||||||
'topics:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:topics:byCid:' + cid, Date.now(), 7),
|
|
||||||
'posts:daily': async.apply(Analytics.getDailyStatsForSet, 'analytics:posts:byCid:' + cid, Date.now(), 7),
|
|
||||||
}, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
Analytics.getErrorAnalytics = function (callback) {
|
|
||||||
async.parallel({
|
|
||||||
'not-found': async.apply(Analytics.getDailyStatsForSet, 'analytics:errors:404', Date.now(), 7),
|
|
||||||
'toobusy': async.apply(Analytics.getDailyStatsForSet, 'analytics:errors:503', Date.now(), 7)
|
|
||||||
}, callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
}(exports));
|
|
||||||
@@ -45,6 +45,8 @@ describe('Controllers', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
it('should load default home route', function (done) {
|
it('should load default home route', function (done) {
|
||||||
request(nconf.get('url'), function (err, res, body) {
|
request(nconf.get('url'), function (err, res, body) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
@@ -615,6 +617,10 @@ describe('Controllers', function () {
|
|||||||
|
|
||||||
|
|
||||||
after(function (done) {
|
after(function (done) {
|
||||||
db.emptydb(done);
|
var analytics = require('../src/analytics');
|
||||||
|
analytics.writeData(function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
db.emptydb(done);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -129,6 +129,7 @@
|
|||||||
nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
|
nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
|
||||||
nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
|
nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
|
||||||
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
|
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
|
||||||
|
nconf.set('bcrypt_rounds', 6);
|
||||||
|
|
||||||
require('../../build').buildTargets(['js', 'clientCSS', 'acpCSS', 'tpl'], next);
|
require('../../build').buildTargets(['js', 'clientCSS', 'acpCSS', 'tpl'], next);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -322,6 +322,24 @@ describe('socket.io', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should get daily analytics', function (done) {
|
||||||
|
io.emit('admin.analytics.get', {graph: 'traffic', units: 'days'}, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert(data);
|
||||||
|
assert(data.monthlyPageViews);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get hourly analytics', function (done) {
|
||||||
|
io.emit('admin.analytics.get', {graph: 'traffic', units: 'hours'}, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert(data);
|
||||||
|
assert(data.monthlyPageViews);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
after(function (done) {
|
after(function (done) {
|
||||||
db.emptydb(done);
|
db.emptydb(done);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user