discourse/app/assets/javascripts/admin/helpers/report_helpers.js

39 lines
998 B
JavaScript
Raw Normal View History

2013-03-08 00:07:59 +08:00
/**
Get the y value of a report data point by its index. Negative indexes start from the end.
@method reportValueY
@for Handlebars
**/
Handlebars.registerHelper('valueAtDaysAgo', function(property, i) {
var data = Ember.Handlebars.get(this, property);
if( data ) {
var wantedDate = Date.create(i + ' days ago', 'en').format('{yyyy}-{MM}-{dd}');
2013-03-10 04:53:30 +08:00
var item = data.find( function(d, i, arr) { return d.x === wantedDate; } );
2013-03-08 00:07:59 +08:00
if( item ) {
return item.y;
} else {
return 0;
}
}
});
/**
Sum the given number of data points from the report, starting at the most recent.
@method sumLast
@for Handlebars
**/
Handlebars.registerHelper('sumLast', function(property, numDays) {
var data = Ember.Handlebars.get(this, property);
if( data ) {
var earliestDate = Date.create(numDays + ' days ago', 'en');
2013-03-08 00:07:59 +08:00
var sum = 0;
data.each(function(d){
if(Date.create(d.x) >= earliestDate) {
sum += d.y;
}
});
return sum;
}
});