diff --git a/src/analytics.js b/src/analytics.js index 49254954bc..8ac91877c3 100644 --- a/src/analytics.js +++ b/src/analytics.js @@ -66,4 +66,23 @@ var cronJob = require('cron').CronJob, Analytics.getUnwrittenPageviews = function() { return pageViews; }; + + Analytics.getMonthlyPageViews = function(callback) { + var thisMonth = new Date(); + 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); + + 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}); + }); + }; + }(exports)); \ No newline at end of file diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index a9d75b3f89..00fea0c730 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -233,7 +233,7 @@ SocketAdmin.analytics.get = function(socket, data, callback) { getHourlyStatsForSet('analytics:pageviews', data.amount, next); }, monthlyPageViews: function(next) { - getMonthlyPageViews(next); + analytics.getMonthlyPageViews(next); } }, function(err, data) { data.pastDay = data.pageviews.reduce(function(a, b) {return parseInt(a, 10) + parseInt(b, 10);}); @@ -286,24 +286,6 @@ function getHourlyStatsForSet(set, hours, callback) { }); } -function getMonthlyPageViews(callback) { - var thisMonth = new Date(); - 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); - - 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}); - }); -} - SocketAdmin.getMoreEvents = function(socket, next, callback) { var start = parseInt(next, 10); if (start < 0) {