2013-03-19 12:04:40 -04:00
|
|
|
Discourse.Report = Discourse.Model.extend({
|
|
|
|
reportUrl: function() {
|
|
|
|
return("/admin/reports/" + this.get('type'));
|
2013-03-30 14:07:25 -04:00
|
|
|
}.property('type'),
|
|
|
|
|
|
|
|
valueAt: function(numDaysAgo) {
|
|
|
|
if (this.data) {
|
2014-08-20 16:22:58 -04:00
|
|
|
var wantedDate = moment().subtract(numDaysAgo, 'days').format('YYYY-MM-DD');
|
2013-12-30 13:29:52 -05:00
|
|
|
var item = this.data.find( function(d) { return d.x === wantedDate; } );
|
2013-03-30 14:07:25 -04:00
|
|
|
if (item) {
|
|
|
|
return item.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
sumDays: function(startDaysAgo, endDaysAgo) {
|
|
|
|
if (this.data) {
|
2014-08-20 16:22:58 -04:00
|
|
|
var earliestDate = moment().subtract(endDaysAgo, 'days').startOf('day');
|
|
|
|
var latestDate = moment().subtract(startDaysAgo, 'days').startOf('day');
|
2013-03-30 14:07:25 -04:00
|
|
|
var d, sum = 0;
|
2013-06-10 16:48:50 -04:00
|
|
|
_.each(this.data,function(datum){
|
|
|
|
d = moment(datum.x);
|
2013-03-30 14:07:25 -04:00
|
|
|
if(d >= earliestDate && d <= latestDate) {
|
|
|
|
sum += datum.y;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-02 14:40:00 -04:00
|
|
|
todayCount: function() {
|
|
|
|
return this.valueAt(0);
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
yesterdayCount: function() {
|
|
|
|
return this.valueAt(1);
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
lastSevenDaysCount: function() {
|
|
|
|
return this.sumDays(1,7);
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
lastThirtyDaysCount: function() {
|
|
|
|
return this.sumDays(1,30);
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
sevenDaysAgoCount: function() {
|
|
|
|
return this.valueAt(7);
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
thirtyDaysAgoCount: function() {
|
|
|
|
return this.valueAt(30);
|
|
|
|
}.property('data'),
|
|
|
|
|
2013-03-30 14:07:25 -04:00
|
|
|
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';
|
2013-04-16 18:37:35 -04:00
|
|
|
}.property('data', 'prev30Days'),
|
|
|
|
|
|
|
|
icon: function() {
|
|
|
|
switch( this.get('type') ) {
|
|
|
|
case 'flags':
|
2015-02-24 13:47:46 -05:00
|
|
|
return 'flag';
|
2013-04-16 18:37:35 -04:00
|
|
|
case 'likes':
|
2015-02-24 13:47:46 -05:00
|
|
|
return 'heart';
|
2013-04-16 18:37:35 -04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2013-04-26 17:13:20 -04:00
|
|
|
}.property('type'),
|
|
|
|
|
|
|
|
percentChangeString: function(val1, val2) {
|
|
|
|
var val = ((val1 - val2) / val2) * 100;
|
|
|
|
if( isNaN(val) || !isFinite(val) ) {
|
|
|
|
return null;
|
|
|
|
} else if( val > 0 ) {
|
|
|
|
return '+' + val.toFixed(0) + '%';
|
|
|
|
} else {
|
|
|
|
return val.toFixed(0) + '%';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
changeTitle: function(val1, val2, prevPeriodString) {
|
|
|
|
var title = '';
|
|
|
|
var percentChange = this.percentChangeString(val1, val2);
|
|
|
|
if( percentChange ) {
|
|
|
|
title += percentChange + ' change. ';
|
|
|
|
}
|
|
|
|
title += 'Was ' + val2 + ' ' + prevPeriodString + '.';
|
|
|
|
return title;
|
|
|
|
},
|
|
|
|
|
|
|
|
yesterdayCountTitle: function() {
|
|
|
|
return this.changeTitle( this.valueAt(1), this.valueAt(2),'two days ago');
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
sevenDayCountTitle: function() {
|
|
|
|
return this.changeTitle( this.sumDays(1,7), this.sumDays(8,14), 'two weeks ago');
|
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
thirtyDayCountTitle: function() {
|
|
|
|
return this.changeTitle( this.sumDays(1,30), this.get('prev30Days'), 'in the previous 30 day period');
|
2014-06-06 17:08:35 -04:00
|
|
|
}.property('data'),
|
|
|
|
|
|
|
|
dataReversed: function() {
|
|
|
|
return this.get('data').toArray().reverse();
|
2013-04-26 17:13:20 -04:00
|
|
|
}.property('data')
|
|
|
|
|
2013-03-19 12:04:40 -04:00
|
|
|
});
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
|
|
Discourse.Report.reopenClass({
|
2014-11-05 14:46:27 -05:00
|
|
|
find: function(type, startDate, endDate) {
|
|
|
|
|
|
|
|
return Discourse.ajax("/admin/reports/" + type, {data: {
|
|
|
|
start_date: startDate,
|
|
|
|
end_date: endDate
|
|
|
|
}}).then(function (json) {
|
2013-04-03 16:06:55 -04:00
|
|
|
// Add a percent field to each tuple
|
|
|
|
var maxY = 0;
|
|
|
|
json.report.data.forEach(function (row) {
|
|
|
|
if (row.y > maxY) maxY = row.y;
|
|
|
|
});
|
|
|
|
if (maxY > 0) {
|
2013-03-17 15:02:36 -04:00
|
|
|
json.report.data.forEach(function (row) {
|
2013-04-03 16:06:55 -04:00
|
|
|
row.percentage = Math.round((row.y / maxY) * 100);
|
2013-03-17 15:02:36 -04:00
|
|
|
});
|
2013-02-27 22:39:42 -05:00
|
|
|
}
|
2014-11-05 14:46:27 -05:00
|
|
|
var model = Discourse.Report.create({type: type});
|
2014-07-21 13:39:23 -04:00
|
|
|
model.setProperties(json.report);
|
2014-11-05 14:46:27 -05:00
|
|
|
return model;
|
2013-02-27 22:39:42 -05:00
|
|
|
});
|
|
|
|
}
|
2013-03-14 08:01:52 -04:00
|
|
|
});
|