diff --git a/app/assets/javascripts/admin/models/report.js b/app/assets/javascripts/admin/models/report.js
index ed6bc868c21..9730d917d0a 100644
--- a/app/assets/javascripts/admin/models/report.js
+++ b/app/assets/javascripts/admin/models/report.js
@@ -1,7 +1,69 @@
Discourse.Report = Discourse.Model.extend({
reportUrl: function() {
return("/admin/reports/" + this.get('type'));
- }.property('type')
+ }.property('type'),
+
+ valueAt: function(numDaysAgo) {
+ if (this.data) {
+ var wantedDate = Date.create(numDaysAgo + ' days ago', 'en').format('{yyyy}-{MM}-{dd}');
+ var item = this.data.find( function(d, i, arr) { return d.x === wantedDate; } );
+ if (item) {
+ return item.y;
+ }
+ }
+ return 0;
+ },
+
+ sumDays: function(startDaysAgo, endDaysAgo) {
+ if (this.data) {
+ var earliestDate = Date.create(endDaysAgo + ' days ago', 'en');
+ var latestDate = Date.create(startDaysAgo + ' days ago', 'en');
+ var d, sum = 0;
+ this.data.each(function(datum){
+ d = Date.create(datum.x);
+ if(d >= earliestDate && d <= latestDate) {
+ sum += datum.y;
+ }
+ });
+ return sum;
+ }
+ },
+
+ yesterdayTrend: function() {
+ var yesterdayVal = this.valueAt(1);
+ var twoDaysAgoVal = this.valueAt(2);
+ if ( yesterdayVal > twoDaysAgoVal ) {
+ return 'trending-up';
+ } else if ( yesterdayVal < twoDaysAgoVal ) {
+ return 'trending-down';
+ } else {
+ return 'no-change';
+ }
+ }.property('data'),
+
+ sevenDayTrend: function() {
+ var currentPeriod = this.sumDays(1,7);
+ var prevPeriod = this.sumDays(8,14);
+ if ( currentPeriod > prevPeriod ) {
+ return 'trending-up';
+ } else if ( currentPeriod < prevPeriod ) {
+ return 'trending-down';
+ } else {
+ return 'no-change';
+ }
+ }.property('data'),
+
+ thirtyDayTrend: function() {
+ if( this.get('prev30Days') ) {
+ var currentPeriod = this.sumDays(1,30);
+ if( currentPeriod > this.get('prev30Days') ) {
+ return 'trending-up';
+ } else if ( currentPeriod < this.get('prev30Days') ) {
+ return 'trending-down';
+ }
+ }
+ return 'no-change';
+ }.property('data', 'prev30Days')
});
Discourse.Report.reopenClass({
diff --git a/app/assets/javascripts/admin/templates/reports/summed_counts_report.js.handlebars b/app/assets/javascripts/admin/templates/reports/summed_counts_report.js.handlebars
index 1a24f4d5df2..5dc9001c511 100644
--- a/app/assets/javascripts/admin/templates/reports/summed_counts_report.js.handlebars
+++ b/app/assets/javascripts/admin/templates/reports/summed_counts_report.js.handlebars
@@ -1,8 +1,8 @@
{{title}} |
{{valueAtDaysAgo data 0}} |
- {{valueAtDaysAgo data 1}} |
- {{sumLast data 7}} |
- {{sumLast data 30}} |
+ {{valueAtDaysAgo data 1}} |
+ {{sumLast data 7}} |
+ {{sumLast data 30}} |
{{total}} |
\ No newline at end of file
diff --git a/app/assets/stylesheets/admin/admin_base.scss b/app/assets/stylesheets/admin/admin_base.scss
index c7428b4b8b3..3b881773cc5 100644
--- a/app/assets/stylesheets/admin/admin_base.scss
+++ b/app/assets/stylesheets/admin/admin_base.scss
@@ -364,6 +364,29 @@ table {
td.value {
font-weight: bold;
text-align: center;
+
+ i {
+ display: none;
+ }
+
+ &.trending-up {
+ color: green;
+ i.up {
+ display: inline;
+ }
+ }
+ &.trending-down {
+ color: red;
+ i.down {
+ display: inline;
+ }
+ }
+ &.no-change {
+ i.down {
+ display: inline;
+ visibility: hidden;
+ }
+ }
}
}
diff --git a/app/models/report.rb b/app/models/report.rb
index 8973a08bf0f..a18aba769ca 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -1,11 +1,12 @@
class Report
- attr_accessor :type, :data, :total
+ attr_accessor :type, :data, :total, :prev30Days
def initialize(type)
@type = type
@data = nil
@total = nil
+ @prev30Days = nil
end
def as_json
@@ -15,7 +16,8 @@ class Report
xaxis: I18n.t("reports.#{self.type}.xaxis"),
yaxis: I18n.t("reports.#{self.type}.yaxis"),
data: self.data,
- total: self.total
+ total: self.total,
+ prev30Days: self.prev30Days
}
end
@@ -42,6 +44,7 @@ class Report
report.data << {x: date, y: count}
end
report.total = User.count
+ report.prev30Days = User.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
def self.report_topics(report)
@@ -50,6 +53,7 @@ class Report
report.data << {x: date, y: count}
end
report.total = Topic.count
+ report.prev30Days = Topic.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
def self.report_posts(report)
@@ -58,6 +62,7 @@ class Report
report.data << {x: date, y: count}
end
report.total = Post.count
+ report.prev30Days = Post.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
def self.report_flags(report)
@@ -67,7 +72,9 @@ class Report
report.data << {x: i.days.ago.to_date.to_s, y: count}
end
end
- report.total = PostAction.where(post_action_type_id: PostActionType.flag_types.values).count
+ flagsQuery = PostAction.where(post_action_type_id: PostActionType.flag_types.values)
+ report.total = flagsQuery.count
+ report.prev30Days = flagsQuery.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
def self.report_users_by_trust_level(report)
@@ -82,7 +89,9 @@ class Report
PostAction.count_likes_per_day(30.days.ago).each do |date, count|
report.data << {x: date, y: count}
end
- report.total = PostAction.where(post_action_type_id: PostActionType.types[:like]).count
+ likesQuery = PostAction.where(post_action_type_id: PostActionType.types[:like])
+ report.total = likesQuery.count
+ report.prev30Days = likesQuery.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
def self.report_emails(report)
@@ -91,6 +100,7 @@ class Report
report.data << {x: date, y: count}
end
report.total = EmailLog.count
+ report.prev30Days = EmailLog.where('created_at > ? and created_at < ?', 60.days.ago, 30.days.ago).count
end
end