discourse/app/assets/javascripts/admin/models/report.js.es6

185 lines
5.2 KiB
Plaintext
Raw Normal View History

2016-07-01 01:55:44 +08:00
import { ajax } from 'discourse/lib/ajax';
import round from "discourse/lib/round";
2015-08-08 03:08:27 +08:00
import { fmt } from 'discourse/lib/computed';
import { fillMissingDates } from 'discourse/lib/utilities';
import computed from 'ember-addons/ember-computed-decorators';
const Report = Discourse.Model.extend({
2015-08-08 03:08:27 +08:00
reportUrl: fmt("type", "/admin/reports/%@"),
2013-03-31 02:07:25 +08:00
valueAt(numDaysAgo) {
2013-03-31 02:07:25 +08:00
if (this.data) {
2015-08-05 00:23:56 +08:00
const wantedDate = moment().subtract(numDaysAgo, "days").format("YYYY-MM-DD");
const item = this.data.find(d => d.x === wantedDate);
2013-03-31 02:07:25 +08:00
if (item) {
return item.y;
}
}
return 0;
},
2015-08-05 00:23:56 +08:00
valueFor(startDaysAgo, endDaysAgo) {
2013-03-31 02:07:25 +08:00
if (this.data) {
2015-08-05 00:23:56 +08:00
const earliestDate = moment().subtract(endDaysAgo, "days").startOf("day");
const latestDate = moment().subtract(startDaysAgo, "days").startOf("day");
var d, sum = 0, count = 0;
_.each(this.data, datum => {
d = moment(datum.x);
2015-08-05 00:23:56 +08:00
if (d >= earliestDate && d <= latestDate) {
2013-03-31 02:07:25 +08:00
sum += datum.y;
2015-08-05 00:23:56 +08:00
count++;
2013-03-31 02:07:25 +08:00
}
});
if (this.get("method") === "average" && count > 0) { sum /= count; }
return round(sum, -2);
2013-03-31 02:07:25 +08:00
}
},
2015-08-05 00:23:56 +08:00
todayCount: function() { return this.valueAt(0); }.property("data"),
yesterdayCount: function() { return this.valueAt(1); }.property("data"),
sevenDaysAgoCount: function() { return this.valueAt(7); }.property("data"),
thirtyDaysAgoCount: function() { return this.valueAt(30); }.property("data"),
2015-08-05 00:23:56 +08:00
lastSevenDaysCount: function() { return this.valueFor(1, 7); }.property("data"),
lastThirtyDaysCount: function() { return this.valueFor(1, 30); }.property("data"),
@computed('data')
yesterdayTrend() {
2015-08-05 00:23:56 +08:00
const yesterdayVal = this.valueAt(1);
const twoDaysAgoVal = this.valueAt(2);
if (yesterdayVal > twoDaysAgoVal) {
return "trending-up";
} else if (yesterdayVal < twoDaysAgoVal) {
return "trending-down";
2013-03-31 02:07:25 +08:00
} else {
2015-08-05 00:23:56 +08:00
return "no-change";
2013-03-31 02:07:25 +08:00
}
},
2013-03-31 02:07:25 +08:00
@computed('data')
sevenDayTrend() {
2015-08-05 00:23:56 +08:00
const currentPeriod = this.valueFor(1, 7);
const prevPeriod = this.valueFor(8, 14);
if (currentPeriod > prevPeriod) {
return "trending-up";
} else if (currentPeriod < prevPeriod) {
return "trending-down";
2013-03-31 02:07:25 +08:00
} else {
2015-08-05 00:23:56 +08:00
return "no-change";
2013-03-31 02:07:25 +08:00
}
},
2013-03-31 02:07:25 +08:00
@computed('prev30Days', 'data')
thirtyDayTrend(prev30Days) {
if (prev30Days) {
2015-08-05 00:23:56 +08:00
const currentPeriod = this.valueFor(1, 30);
if (currentPeriod > this.get("prev30Days")) {
return "trending-up";
} else if (currentPeriod < prev30Days) {
2015-08-05 00:23:56 +08:00
return "trending-down";
2013-03-31 02:07:25 +08:00
}
}
2015-08-05 00:23:56 +08:00
return "no-change";
},
2013-04-17 06:37:35 +08:00
@computed('type')
icon(type) {
switch (type) {
2015-08-05 00:23:56 +08:00
case "flags": return "flag";
case "likes": return "heart";
2017-01-05 09:07:38 +08:00
case "bookmarks": return "bookmark";
default: return null;
2013-04-17 06:37:35 +08:00
}
},
2015-08-05 00:23:56 +08:00
@computed('type')
method(type) {
if (type === "time_to_first_response") {
2015-08-05 00:23:56 +08:00
return "average";
} else {
return "sum";
}
},
percentChangeString(val1, val2) {
2015-08-05 00:23:56 +08:00
const val = ((val1 - val2) / val2) * 100;
if (isNaN(val) || !isFinite(val)) {
return null;
2015-08-05 00:23:56 +08:00
} else if (val > 0) {
return "+" + val.toFixed(0) + "%";
} else {
2015-08-05 00:23:56 +08:00
return val.toFixed(0) + "%";
}
},
changeTitle(val1, val2, prevPeriodString) {
2015-08-05 00:23:56 +08:00
const percentChange = this.percentChangeString(val1, val2);
var title = "";
if (percentChange) { title += percentChange + " change. "; }
title += "Was " + val2 + " " + prevPeriodString + ".";
return title;
},
@computed('data')
yesterdayCountTitle() {
2015-08-05 00:23:56 +08:00
return this.changeTitle(this.valueAt(1), this.valueAt(2), "two days ago");
},
@computed('data')
sevenDayCountTitle() {
2015-08-05 00:23:56 +08:00
return this.changeTitle(this.valueFor(1, 7), this.valueFor(8, 14), "two weeks ago");
},
@computed('prev30Days', 'data')
thirtyDayCountTitle(prev30Days) {
return this.changeTitle(this.valueFor(1, 30), prev30Days, "in the previous 30 day period");
},
@computed('data')
sortedData(data) {
2018-03-28 01:53:47 +08:00
return this.get('xAxisIsDate') ? data.toArray().reverse() : data.toArray();
},
@computed('data')
2018-03-28 01:53:47 +08:00
xAxisIsDate() {
if (!this.data[0]) return false;
return this.data && this.data[0].x.match(/\d{4}-\d{1,2}-\d{1,2}/);
}
});
Report.reopenClass({
find(type, startDate, endDate, categoryId, groupId) {
2016-07-01 01:55:44 +08:00
return ajax("/admin/reports/" + type, {
data: {
start_date: startDate,
end_date: endDate,
category_id: categoryId,
group_id: groupId
}
}).then(json => {
// Add zero values for missing dates
if (json.report.data.length > 0) {
const startDateFormatted = moment(json.report.start_date).utc().format('YYYY-MM-DD');
const endDateFormatted = moment(json.report.end_date).utc().format('YYYY-MM-DD');
json.report.data = fillMissingDates(json.report.data, startDateFormatted, endDateFormatted);
}
2015-12-02 07:31:30 +08:00
const model = Report.create({ type: type });
model.setProperties(json.report);
if (json.report.related_report) {
// TODO: fillMissingDates if xaxis is date
const related = Report.create({ type: json.report.related_report.type });
related.setProperties(json.report.related_report);
model.set('relatedReport', related);
}
return model;
});
}
});
export default Report;