From 94cf29d42c6dffff549af379153a2cf2fd206029 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 20 Dec 2014 20:05:59 -0500 Subject: [PATCH] show monthly page views --- public/less/admin/general/dashboard.less | 14 +++++--- public/src/admin/general/dashboard.js | 5 +++ src/analytics.js | 4 +++ src/socket.io/admin.js | 43 ++++++++++++++++-------- src/views/admin/general/dashboard.tpl | 9 +++++ 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/public/less/admin/general/dashboard.less b/public/less/admin/general/dashboard.less index 225bad0062..1001893ed7 100644 --- a/public/less/admin/general/dashboard.less +++ b/public/less/admin/general/dashboard.less @@ -1,6 +1,6 @@ .dashboard { max-width: 1680px; - + .panel { max-width: 100% !important; } @@ -14,7 +14,7 @@ padding-right: 0px; padding-left: 50px; min-height: 180px; - + .graph-legend { top: -10px; left: 0px; @@ -28,7 +28,7 @@ &.legend-down { padding-left: 0px; padding-top: 0px; - + canvas { margin-bottom: 25px; } @@ -60,7 +60,7 @@ list-style-type: none; padding: 0; - li { + li { div { border: 1px solid; width: 12px; @@ -105,6 +105,8 @@ } } } + + } .version-check { @@ -123,6 +125,10 @@ } } + .monthly-pageviews { + width:50%; + } + .motd textarea { width: 100%; } diff --git a/public/src/admin/general/dashboard.js b/public/src/admin/general/dashboard.js index 76316eb091..20bba7d22e 100644 --- a/public/src/admin/general/dashboard.js +++ b/public/src/admin/general/dashboard.js @@ -331,6 +331,11 @@ define('admin/general/dashboard', ['semver'], function(semver) { graphs.traffic.scale.xLabels = getHoursArray(); graphs.traffic.update(); + + $('#pageViewsThisMonth').html(data.monthlyPageViews.thisMonth); + $('#pageViewsLastMonth').html(data.monthlyPageViews.lastMonth); + utils.addCommasToNumbers($('#pageViewsThisMonth')); + utils.addCommasToNumbers($('#pageViewsLastMonth')); }); } diff --git a/src/analytics.js b/src/analytics.js index fd9031d9a7..38d6ec53b7 100644 --- a/src/analytics.js +++ b/src/analytics.js @@ -45,6 +45,10 @@ var cronJob = require('cron').CronJob, if (pageViews > 0) { db.sortedSetIncrBy('analytics:pageviews', pageViews, today.getTime()); + var month = new Date(); + month.setMonth(month.getMonth(), 1); + month.setHours(0, 0, 0, 0); + db.sortedSetIncrBy('analytics:pageviews:month', pageViews, month.getTime()); pageViews = 0; } diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 54e220920e..fecd4213c4 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -199,6 +199,9 @@ SocketAdmin.analytics.get = function(socket, data, callback) { }, pageviews: function(next) { getHourlyStatsForSet('analytics:pageviews', data.amount, next); + }, + monthlyPageViews: function(next) { + getMonthlyPageViews(next); } }, callback); } @@ -227,20 +230,15 @@ function getHourlyStatsForSet(set, hours, callback) { hour.setHours(hour.getHours() - 1, 0, 0, 0); } - async.each(hoursArr, function(term, next) { - if (set.indexOf('analytics') !== -1) { - db.sortedSetScore(set, term, function(err, count) { - terms[term] = count || 0; - next(err); - }); - } else { - db.sortedSetCount(set, term, Date.now(), function(err, count) { - terms[term] = count || 0; - next(err); - }); + db.sortedSetScores(set, hoursArr, function(err, counts) { + if (err) { + return callback(err); } - }, function(err) { + hoursArr.forEach(function(term, index) { + terms[term] = counts[index] || 0; + }); + var termsArr = []; hoursArr.reverse(); @@ -248,7 +246,25 @@ function getHourlyStatsForSet(set, hours, callback) { termsArr.push(terms[hour]); }); - callback(err, termsArr); + callback(null, termsArr); + }); +} + +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}); }); } @@ -259,7 +275,6 @@ SocketAdmin.getMoreEvents = function(socket, next, callback) { events.getLog(next, 5000, callback); }; - SocketAdmin.dismissFlag = function(socket, pid, callback) { if (!pid) { return callback('[[error:invalid-data]]'); diff --git a/src/views/admin/general/dashboard.tpl b/src/views/admin/general/dashboard.tpl index 72b5153be2..d8d0105a68 100644 --- a/src/views/admin/general/dashboard.tpl +++ b/src/views/admin/general/dashboard.tpl @@ -10,6 +10,15 @@ + +
+
+
Page views Last Month
+
+
+
+
Page views This Month
+