converted pageviews tracking to sorted set, and using pre-built getStatsForSet function

thanks @barisusakli
v1.18.x
psychobunny 10 years ago
parent 80ca6bb0a2
commit 144992426d

@ -38,9 +38,6 @@ adminController.home = function(req, res, next) {
stats: function(next) { stats: function(next) {
getStats(next); getStats(next);
}, },
pageviews: function(next) {
getPageviews(next);
},
notices: function(next) { notices: function(next) {
var notices = [ var notices = [
{done: !meta.restartRequired, doneText: 'Restart not required', notDoneText:'Restart required'}, {done: !meta.restartRequired, doneText: 'Restart not required', notDoneText:'Restart required'},
@ -56,24 +53,11 @@ adminController.home = function(req, res, next) {
res.render('admin/index', { res.render('admin/index', {
version: pkg.version, version: pkg.version,
notices: results.notices, notices: results.notices,
stats: results.stats, stats: results.stats
pageviews: results.pageviews
}); });
}); });
}; };
function getPageviews(callback) {
async.parallel({
monthly: function(next) {
db.get('pageviews:monthly', next);
},
daily: function(next) {
db.get('pageviews:daily', next);
}
}, function(err, results) {
callback(null, results);
});
}
function getStats(callback) { function getStats(callback) {
async.parallel([ async.parallel([
function(next) { function(next) {
@ -87,6 +71,9 @@ function getStats(callback) {
}, },
function(next) { function(next) {
getStatsForSet('topics:tid', next); getStatsForSet('topics:tid', next);
},
function(next) {
getStatsForSet('analytics:pageviews', next);
} }
], function(err, results) { ], function(err, results) {
if (err) { if (err) {
@ -96,6 +83,7 @@ function getStats(callback) {
results[1].name = 'Users'; results[1].name = 'Users';
results[2].name = 'Posts'; results[2].name = 'Posts';
results[3].name = 'Topics'; results[3].name = 'Topics';
results[4].name = 'Page Views';
callback(null, results); callback(null, results);
}); });

@ -49,29 +49,13 @@ middleware.updateLastOnlineTime = function(req, res, next) {
}; };
middleware.incrementPageViews = function(req, res, next) { middleware.incrementPageViews = function(req, res, next) {
var nextMonth = new Date(), var today = new Date();
nextDay = new Date();
nextMonth.setMonth(nextMonth.getMonth() + 1, 1); today.setDate(today.getDate());
nextMonth.setHours(0, 0, 0, 0); today.setHours(today.getHours(), 0, 0, 0);
nextDay.setDate(nextDay.getDate() + 1);
nextDay.setHours(0, 0, 0, 0);
db.increment('pageviews:monthly', function(err) {
if (err) {
return;
}
db.pexpireAt('pageviews:monthly', nextMonth.getTime());
});
db.increment('pageviews:daily', function(err) {
if (err) {
return;
}
db.pexpireAt('pageviews:daily', nextDay.getTime());
});
db.sortedSetIncrBy('analytics:pageviews', 1, today.getTime());
next(); next();
}; };

@ -23,18 +23,6 @@
</div> </div>
</div> </div>
<div class="panel panel-default">
<div class="panel-heading">Pageviews</div>
<div class="panel-body">
<div class="col-xs-6 text-center">
<h3>Monthly<br /><small>{pageviews.monthly}</small></h3>
</div>
<div class="col-xs-6 text-center">
<h3>Daily<br /><small>{pageviews.daily}</small></h3>
</div>
</div>
</div>
<!-- BEGIN stats --> <!-- BEGIN stats -->
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">{stats.name}</div> <div class="panel-heading">{stats.name}</div>

Loading…
Cancel
Save