2018-07-19 14:33:11 -04:00
|
|
|
|
import { escapeExpression } from "discourse/lib/utilities";
|
2018-05-17 16:44:33 -04:00
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2015-06-22 13:46:51 -04:00
|
|
|
|
import round from "discourse/lib/round";
|
2018-08-01 18:40:59 -04:00
|
|
|
|
import { fillMissingDates, formatUsername } from "discourse/lib/utilities";
|
2018-05-17 16:44:33 -04:00
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2018-07-19 14:33:11 -04:00
|
|
|
|
import { number, durationTiny } from "discourse/lib/formatter";
|
2018-07-31 17:35:13 -04:00
|
|
|
|
import { renderAvatar } from "discourse/helpers/user-avatar";
|
|
|
|
|
|
|
|
|
|
// Change this line each time report format change
|
|
|
|
|
// and you want to ensure cache is reset
|
2018-08-30 08:56:11 -04:00
|
|
|
|
export const SCHEMA_VERSION = 3;
|
2015-06-22 13:46:51 -04:00
|
|
|
|
|
|
|
|
|
const Report = Discourse.Model.extend({
|
2018-05-03 09:41:41 -04:00
|
|
|
|
average: false,
|
2018-05-15 14:12:03 -04:00
|
|
|
|
percent: false,
|
2018-05-17 16:44:33 -04:00
|
|
|
|
higher_is_better: true,
|
2018-05-03 09:41:41 -04:00
|
|
|
|
|
2018-07-19 14:33:11 -04:00
|
|
|
|
@computed("modes")
|
|
|
|
|
onlyTable(modes) {
|
|
|
|
|
return modes.length === 1 && modes[0] === "table";
|
|
|
|
|
},
|
|
|
|
|
|
2018-05-13 21:33:36 -04:00
|
|
|
|
@computed("type", "start_date", "end_date")
|
|
|
|
|
reportUrl(type, start_date, end_date) {
|
2018-07-19 14:33:11 -04:00
|
|
|
|
start_date = moment
|
|
|
|
|
.utc(start_date)
|
2018-06-15 11:03:24 -04:00
|
|
|
|
.locale("en")
|
|
|
|
|
.format("YYYY-MM-DD");
|
2018-07-19 14:33:11 -04:00
|
|
|
|
|
|
|
|
|
end_date = moment
|
|
|
|
|
.utc(end_date)
|
2018-06-15 11:03:24 -04:00
|
|
|
|
.locale("en")
|
|
|
|
|
.format("YYYY-MM-DD");
|
2018-07-19 14:33:11 -04:00
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return Discourse.getURL(
|
|
|
|
|
`/admin/reports/${type}?start_date=${start_date}&end_date=${end_date}`
|
|
|
|
|
);
|
2018-05-13 21:33:36 -04:00
|
|
|
|
},
|
2013-03-30 14:07:25 -04:00
|
|
|
|
|
2015-06-22 13:46:51 -04:00
|
|
|
|
valueAt(numDaysAgo) {
|
2013-03-30 14:07:25 -04:00
|
|
|
|
if (this.data) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
const wantedDate = moment()
|
|
|
|
|
.subtract(numDaysAgo, "days")
|
|
|
|
|
.locale("en")
|
|
|
|
|
.format("YYYY-MM-DD");
|
2015-08-04 12:23:56 -04:00
|
|
|
|
const item = this.data.find(d => d.x === wantedDate);
|
2013-03-30 14:07:25 -04:00
|
|
|
|
if (item) {
|
|
|
|
|
return item.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
},
|
|
|
|
|
|
2015-08-04 12:23:56 -04:00
|
|
|
|
valueFor(startDaysAgo, endDaysAgo) {
|
2013-03-30 14:07:25 -04:00
|
|
|
|
if (this.data) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
const earliestDate = moment()
|
|
|
|
|
.subtract(endDaysAgo, "days")
|
|
|
|
|
.startOf("day");
|
|
|
|
|
const latestDate = moment()
|
|
|
|
|
.subtract(startDaysAgo, "days")
|
|
|
|
|
.startOf("day");
|
|
|
|
|
let d,
|
|
|
|
|
sum = 0,
|
|
|
|
|
count = 0;
|
2018-11-20 08:46:58 -05:00
|
|
|
|
this.data.forEach(datum => {
|
2013-06-10 16:48:50 -04:00
|
|
|
|
d = moment(datum.x);
|
2015-08-04 12:23:56 -04:00
|
|
|
|
if (d >= earliestDate && d <= latestDate) {
|
2013-03-30 14:07:25 -04:00
|
|
|
|
sum += datum.y;
|
2015-08-04 12:23:56 -04:00
|
|
|
|
count++;
|
2013-03-30 14:07:25 -04:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-06-15 11:03:24 -04:00
|
|
|
|
if (this.get("method") === "average" && count > 0) {
|
|
|
|
|
sum /= count;
|
|
|
|
|
}
|
2015-06-22 13:46:51 -04:00
|
|
|
|
return round(sum, -2);
|
2013-03-30 14:07:25 -04:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
|
todayCount: function() {
|
|
|
|
|
return this.valueAt(0);
|
|
|
|
|
}.property("data", "average"),
|
|
|
|
|
yesterdayCount: function() {
|
|
|
|
|
return this.valueAt(1);
|
|
|
|
|
}.property("data", "average"),
|
|
|
|
|
sevenDaysAgoCount: function() {
|
|
|
|
|
return this.valueAt(7);
|
|
|
|
|
}.property("data", "average"),
|
|
|
|
|
thirtyDaysAgoCount: function() {
|
|
|
|
|
return this.valueAt(30);
|
|
|
|
|
}.property("data", "average"),
|
2018-05-17 16:44:33 -04:00
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
|
lastSevenDaysCount: function() {
|
2018-05-03 09:41:41 -04:00
|
|
|
|
return this.averageCount(7, this.valueFor(1, 7));
|
|
|
|
|
}.property("data", "average"),
|
|
|
|
|
lastThirtyDaysCount: function() {
|
|
|
|
|
return this.averageCount(30, this.valueFor(1, 30));
|
|
|
|
|
}.property("data", "average"),
|
|
|
|
|
|
|
|
|
|
averageCount(count, value) {
|
|
|
|
|
return this.get("average") ? value / count : value;
|
|
|
|
|
},
|
2013-04-02 14:40:00 -04:00
|
|
|
|
|
2018-05-28 04:55:42 -04:00
|
|
|
|
@computed("yesterdayCount", "higher_is_better")
|
|
|
|
|
yesterdayTrend(yesterdayCount, higherIsBetter) {
|
|
|
|
|
return this._computeTrend(this.valueAt(2), yesterdayCount, higherIsBetter);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-03-30 14:07:25 -04:00
|
|
|
|
|
2018-05-28 04:55:42 -04:00
|
|
|
|
@computed("lastSevenDaysCount", "higher_is_better")
|
|
|
|
|
sevenDaysTrend(lastSevenDaysCount, higherIsBetter) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return this._computeTrend(
|
|
|
|
|
this.valueFor(8, 14),
|
|
|
|
|
lastSevenDaysCount,
|
|
|
|
|
higherIsBetter
|
|
|
|
|
);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-03-30 14:07:25 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("data")
|
2018-06-15 11:03:24 -04:00
|
|
|
|
currentTotal(data) {
|
2018-11-19 07:04:57 -05:00
|
|
|
|
return data.reduce((cur, pair) => cur + pair.y, 0);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("data", "currentTotal")
|
2018-05-10 23:30:21 -04:00
|
|
|
|
currentAverage(data, total) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return Ember.makeArray(data).length === 0
|
|
|
|
|
? 0
|
|
|
|
|
: parseFloat((total / parseFloat(data.length)).toFixed(1));
|
2018-05-15 14:12:03 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-25 06:09:30 -04:00
|
|
|
|
@computed("trend", "higher_is_better")
|
|
|
|
|
trendIcon(trend, higherIsBetter) {
|
|
|
|
|
return this._iconForTrend(trend, higherIsBetter);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
@computed("sevenDaysTrend", "higher_is_better")
|
|
|
|
|
sevenDaysTrendIcon(sevenDaysTrend, higherIsBetter) {
|
|
|
|
|
return this._iconForTrend(sevenDaysTrend, higherIsBetter);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
@computed("thirtyDaysTrend", "higher_is_better")
|
|
|
|
|
thirtyDaysTrendIcon(thirtyDaysTrend, higherIsBetter) {
|
|
|
|
|
return this._iconForTrend(thirtyDaysTrend, higherIsBetter);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
@computed("yesterdayTrend", "higher_is_better")
|
|
|
|
|
yesterdayTrendIcon(yesterdayTrend, higherIsBetter) {
|
|
|
|
|
return this._iconForTrend(yesterdayTrend, higherIsBetter);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-28 04:55:42 -04:00
|
|
|
|
@computed("prev_period", "currentTotal", "currentAverage", "higher_is_better")
|
|
|
|
|
trend(prev, currentTotal, currentAverage, higherIsBetter) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
const total = this.get("average") ? currentAverage : currentTotal;
|
2018-05-28 04:55:42 -04:00
|
|
|
|
return this._computeTrend(prev, total, higherIsBetter);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-28 04:55:42 -04:00
|
|
|
|
@computed("prev30Days", "lastThirtyDaysCount", "higher_is_better")
|
|
|
|
|
thirtyDaysTrend(prev30Days, lastThirtyDaysCount, higherIsBetter) {
|
|
|
|
|
return this._computeTrend(prev30Days, lastThirtyDaysCount, higherIsBetter);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-04-16 18:37:35 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("type")
|
2018-03-15 17:10:45 -04:00
|
|
|
|
method(type) {
|
|
|
|
|
if (type === "time_to_first_response") {
|
2015-08-04 12:23:56 -04:00
|
|
|
|
return "average";
|
|
|
|
|
} else {
|
|
|
|
|
return "sum";
|
|
|
|
|
}
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-04-26 17:13:20 -04:00
|
|
|
|
|
2015-06-22 13:46:51 -04:00
|
|
|
|
percentChangeString(val1, val2) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
const change = this._computeChange(val1, val2);
|
|
|
|
|
|
|
|
|
|
if (isNaN(change) || !isFinite(change)) {
|
2013-04-26 17:13:20 -04:00
|
|
|
|
return null;
|
2018-05-17 16:44:33 -04:00
|
|
|
|
} else if (change > 0) {
|
|
|
|
|
return "+" + change.toFixed(0) + "%";
|
2013-04-26 17:13:20 -04:00
|
|
|
|
} else {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
return change.toFixed(0) + "%";
|
2013-04-26 17:13:20 -04:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("prev_period", "currentTotal", "currentAverage")
|
2018-05-10 23:30:21 -04:00
|
|
|
|
trendTitle(prev, currentTotal, currentAverage) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
let current = this.get("average") ? currentAverage : currentTotal;
|
|
|
|
|
let percent = this.percentChangeString(prev, current);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
if (this.get("average")) {
|
2018-05-13 21:12:52 -04:00
|
|
|
|
prev = prev ? prev.toFixed(1) : "0";
|
2018-05-17 16:44:33 -04:00
|
|
|
|
if (this.get("percent")) {
|
|
|
|
|
current += "%";
|
|
|
|
|
prev += "%";
|
2018-05-14 20:17:17 -04:00
|
|
|
|
}
|
2018-05-17 16:44:33 -04:00
|
|
|
|
} else {
|
|
|
|
|
prev = number(prev);
|
|
|
|
|
current = number(current);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return I18n.t("admin.dashboard.reports.trend_title", {
|
|
|
|
|
percent,
|
|
|
|
|
prev,
|
|
|
|
|
current
|
|
|
|
|
});
|
2018-05-10 23:30:21 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
changeTitle(valAtT1, valAtT2, prevPeriodString) {
|
|
|
|
|
const change = this.percentChangeString(valAtT1, valAtT2);
|
|
|
|
|
let title = "";
|
2018-06-15 11:03:24 -04:00
|
|
|
|
if (change) {
|
|
|
|
|
title += `${change} change. `;
|
|
|
|
|
}
|
2018-05-28 14:35:22 -04:00
|
|
|
|
title += `Was ${number(valAtT1)} ${prevPeriodString}.`;
|
2013-04-26 17:13:20 -04:00
|
|
|
|
return title;
|
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("yesterdayCount")
|
2018-05-03 09:41:41 -04:00
|
|
|
|
yesterdayCountTitle(yesterdayCount) {
|
2018-05-28 14:35:22 -04:00
|
|
|
|
return this.changeTitle(this.valueAt(2), yesterdayCount, "two days ago");
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-04-26 17:13:20 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("lastSevenDaysCount")
|
|
|
|
|
sevenDaysCountTitle(lastSevenDaysCount) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return this.changeTitle(
|
|
|
|
|
this.valueFor(8, 14),
|
|
|
|
|
lastSevenDaysCount,
|
|
|
|
|
"two weeks ago"
|
|
|
|
|
);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2013-04-26 17:13:20 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("prev30Days", "lastThirtyDaysCount")
|
|
|
|
|
thirtyDaysCountTitle(prev30Days, lastThirtyDaysCount) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
return this.changeTitle(
|
|
|
|
|
prev30Days,
|
|
|
|
|
lastThirtyDaysCount,
|
|
|
|
|
"in the previous 30 day period"
|
|
|
|
|
);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("data")
|
2018-03-15 17:10:45 -04:00
|
|
|
|
sortedData(data) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
return this.get("xAxisIsDate") ? data.toArray().reverse() : data.toArray();
|
2018-03-15 17:10:45 -04:00
|
|
|
|
},
|
2014-06-06 17:08:35 -04:00
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
@computed("data")
|
2018-03-27 13:53:47 -04:00
|
|
|
|
xAxisIsDate() {
|
2018-03-15 17:10:45 -04:00
|
|
|
|
if (!this.data[0]) return false;
|
2018-03-27 14:10:39 -04:00
|
|
|
|
return this.data && this.data[0].x.match(/\d{4}-\d{1,2}-\d{1,2}/);
|
2018-05-17 16:44:33 -04:00
|
|
|
|
},
|
2013-04-26 17:13:20 -04:00
|
|
|
|
|
2018-07-31 17:35:13 -04:00
|
|
|
|
@computed("labels")
|
|
|
|
|
computedLabels(labels) {
|
|
|
|
|
return labels.map(label => {
|
2018-08-01 18:40:59 -04:00
|
|
|
|
const type = label.type || "string";
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
|
|
|
|
let mainProperty;
|
|
|
|
|
if (label.property) mainProperty = label.property;
|
|
|
|
|
else if (type === "user") mainProperty = label.properties["username"];
|
|
|
|
|
else if (type === "topic") mainProperty = label.properties["title"];
|
|
|
|
|
else if (type === "post")
|
|
|
|
|
mainProperty = label.properties["truncated_raw"];
|
|
|
|
|
else mainProperty = label.properties[0];
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: label.title,
|
|
|
|
|
sortProperty: label.sort_property || mainProperty,
|
|
|
|
|
mainProperty,
|
2018-08-01 18:40:59 -04:00
|
|
|
|
type,
|
|
|
|
|
compute: (row, opts = {}) => {
|
2018-07-31 17:35:13 -04:00
|
|
|
|
const value = row[mainProperty];
|
|
|
|
|
|
|
|
|
|
if (type === "user") return this._userLabel(label.properties, row);
|
|
|
|
|
if (type === "post") return this._postLabel(label.properties, row);
|
|
|
|
|
if (type === "topic") return this._topicLabel(label.properties, row);
|
2018-08-01 18:40:59 -04:00
|
|
|
|
if (type === "seconds") return this._secondsLabel(value);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
if (type === "link") return this._linkLabel(label.properties, row);
|
2018-08-01 18:40:59 -04:00
|
|
|
|
if (type === "percent") return this._percentLabel(value);
|
|
|
|
|
if (type === "number") {
|
|
|
|
|
return this._numberLabel(value, opts);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
}
|
|
|
|
|
if (type === "date") {
|
|
|
|
|
const date = moment(value, "YYYY-MM-DD");
|
2018-08-01 18:40:59 -04:00
|
|
|
|
if (date.isValid()) return this._dateLabel(value, date);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
}
|
2018-08-01 18:40:59 -04:00
|
|
|
|
if (type === "text") return this._textLabel(value);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-01 18:40:59 -04:00
|
|
|
|
type,
|
|
|
|
|
property: mainProperty,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? escapeExpression(value) : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_userLabel(properties, row) {
|
|
|
|
|
const username = row[properties.username];
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
const formatedValue = () => {
|
|
|
|
|
const userId = row[properties.id];
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
const user = Ember.Object.create({
|
|
|
|
|
username,
|
|
|
|
|
name: formatUsername(username),
|
|
|
|
|
avatar_template: row[properties.avatar]
|
|
|
|
|
});
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
2018-10-26 06:32:02 -04:00
|
|
|
|
const href = Discourse.getURL(`/admin/users/${userId}/${username}`);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
const avatarImg = renderAvatar(user, {
|
|
|
|
|
imageSize: "tiny",
|
|
|
|
|
ignoreTitle: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return `<a href='${href}'>${avatarImg}<span class='username'>${
|
|
|
|
|
user.name
|
|
|
|
|
}</span></a>`;
|
|
|
|
|
};
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
value: username,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: username ? formatedValue(username) : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_topicLabel(properties, row) {
|
|
|
|
|
const topicTitle = row[properties.title];
|
2018-08-02 08:01:17 -04:00
|
|
|
|
|
|
|
|
|
const formatedValue = () => {
|
|
|
|
|
const topicId = row[properties.id];
|
2018-10-26 06:32:02 -04:00
|
|
|
|
const href = Discourse.getURL(`/t/-/${topicId}`);
|
2018-08-02 08:01:17 -04:00
|
|
|
|
return `<a href='${href}'>${topicTitle}</a>`;
|
|
|
|
|
};
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
value: topicTitle,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: topicTitle ? formatedValue() : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_postLabel(properties, row) {
|
|
|
|
|
const postTitle = row[properties.truncated_raw];
|
|
|
|
|
const postNumber = row[properties.number];
|
|
|
|
|
const topicId = row[properties.topic_id];
|
2018-10-26 06:32:02 -04:00
|
|
|
|
const href = Discourse.getURL(`/t/-/${topicId}/${postNumber}`);
|
2018-07-31 17:35:13 -04:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
property: properties.title,
|
|
|
|
|
value: postTitle,
|
|
|
|
|
formatedValue: `<a href='${href}'>${postTitle}</a>`
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
_secondsLabel(value) {
|
2018-07-31 17:35:13 -04:00
|
|
|
|
return {
|
|
|
|
|
value,
|
|
|
|
|
formatedValue: durationTiny(value)
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
_percentLabel(value) {
|
2018-07-31 17:35:13 -04:00
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? `${value}%` : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
_numberLabel(value, options = {}) {
|
|
|
|
|
const formatNumbers = Ember.isEmpty(options.formatNumbers)
|
|
|
|
|
? true
|
|
|
|
|
: options.formatNumbers;
|
|
|
|
|
|
|
|
|
|
const formatedValue = () => (formatNumbers ? number(value) : value);
|
|
|
|
|
|
2018-07-31 17:35:13 -04:00
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? formatedValue() : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
_dateLabel(value, date) {
|
2018-07-31 17:35:13 -04:00
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? date.format("LL") : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-08-01 18:40:59 -04:00
|
|
|
|
_textLabel(value) {
|
2018-07-31 17:35:13 -04:00
|
|
|
|
const escaped = escapeExpression(value);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? escaped : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_linkLabel(properties, row) {
|
|
|
|
|
const property = properties[0];
|
2018-10-26 06:32:02 -04:00
|
|
|
|
const value = Discourse.getURL(row[property]);
|
2018-08-01 18:40:59 -04:00
|
|
|
|
const formatedValue = (href, anchor) => {
|
|
|
|
|
return `<a href="${escapeExpression(href)}">${escapeExpression(
|
|
|
|
|
anchor
|
|
|
|
|
)}</a>`;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 17:35:13 -04:00
|
|
|
|
return {
|
|
|
|
|
value,
|
2018-08-17 10:19:25 -04:00
|
|
|
|
formatedValue: value ? formatedValue(value, row[properties[1]]) : "—"
|
2018-07-31 17:35:13 -04:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2018-05-17 16:44:33 -04:00
|
|
|
|
_computeChange(valAtT1, valAtT2) {
|
|
|
|
|
return ((valAtT2 - valAtT1) / valAtT1) * 100;
|
|
|
|
|
},
|
|
|
|
|
|
2018-05-28 04:55:42 -04:00
|
|
|
|
_computeTrend(valAtT1, valAtT2, higherIsBetter) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
const change = this._computeChange(valAtT1, valAtT2);
|
|
|
|
|
|
|
|
|
|
if (change > 50) {
|
|
|
|
|
return higherIsBetter ? "high-trending-up" : "high-trending-down";
|
2018-05-28 04:55:42 -04:00
|
|
|
|
} else if (change > 2) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
return higherIsBetter ? "trending-up" : "trending-down";
|
2018-05-28 04:55:42 -04:00
|
|
|
|
} else if (change <= 2 && change >= -2) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
return "no-change";
|
|
|
|
|
} else if (change < -50) {
|
|
|
|
|
return higherIsBetter ? "high-trending-down" : "high-trending-up";
|
2018-05-28 04:55:42 -04:00
|
|
|
|
} else if (change < -2) {
|
2018-05-17 16:44:33 -04:00
|
|
|
|
return higherIsBetter ? "trending-down" : "trending-up";
|
|
|
|
|
}
|
2018-05-25 06:09:30 -04:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_iconForTrend(trend, higherIsBetter) {
|
|
|
|
|
switch (trend) {
|
|
|
|
|
case "trending-up":
|
|
|
|
|
return higherIsBetter ? "angle-up" : "angle-down";
|
|
|
|
|
case "trending-down":
|
|
|
|
|
return higherIsBetter ? "angle-down" : "angle-up";
|
|
|
|
|
case "high-trending-up":
|
|
|
|
|
return higherIsBetter ? "angle-double-up" : "angle-double-down";
|
|
|
|
|
case "high-trending-down":
|
|
|
|
|
return higherIsBetter ? "angle-double-down" : "angle-double-up";
|
|
|
|
|
default:
|
2018-08-22 12:00:11 -04:00
|
|
|
|
return "minus";
|
2018-05-25 06:09:30 -04:00
|
|
|
|
}
|
2018-05-17 16:44:33 -04:00
|
|
|
|
}
|
2013-03-19 12:04:40 -04:00
|
|
|
|
});
|
2013-02-27 22:39:42 -05:00
|
|
|
|
|
2015-06-22 13:46:51 -04:00
|
|
|
|
Report.reopenClass({
|
2018-07-19 14:33:11 -04:00
|
|
|
|
fillMissingDates(report, options = {}) {
|
|
|
|
|
const dataField = options.dataField || "data";
|
|
|
|
|
const filledField = options.filledField || "data";
|
|
|
|
|
const startDate = options.startDate || "start_date";
|
|
|
|
|
const endDate = options.endDate || "end_date";
|
|
|
|
|
|
|
|
|
|
if (_.isArray(report[dataField])) {
|
2018-06-15 11:03:24 -04:00
|
|
|
|
const startDateFormatted = moment
|
2018-07-19 14:33:11 -04:00
|
|
|
|
.utc(report[startDate])
|
2018-06-15 11:03:24 -04:00
|
|
|
|
.locale("en")
|
|
|
|
|
.format("YYYY-MM-DD");
|
|
|
|
|
const endDateFormatted = moment
|
2018-07-19 14:33:11 -04:00
|
|
|
|
.utc(report[endDate])
|
2018-06-15 11:03:24 -04:00
|
|
|
|
.locale("en")
|
|
|
|
|
.format("YYYY-MM-DD");
|
2018-07-19 14:33:11 -04:00
|
|
|
|
report[filledField] = fillMissingDates(
|
|
|
|
|
JSON.parse(JSON.stringify(report[dataField])),
|
2018-06-15 11:03:24 -04:00
|
|
|
|
startDateFormatted,
|
|
|
|
|
endDateFormatted
|
|
|
|
|
);
|
2018-05-10 23:30:21 -04:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2016-02-02 21:29:51 -05:00
|
|
|
|
find(type, startDate, endDate, categoryId, groupId) {
|
2016-06-30 13:55:44 -04:00
|
|
|
|
return ajax("/admin/reports/" + type, {
|
2015-06-24 09:19:39 -04:00
|
|
|
|
data: {
|
|
|
|
|
start_date: startDate,
|
|
|
|
|
end_date: endDate,
|
2016-02-02 21:29:51 -05:00
|
|
|
|
category_id: categoryId,
|
|
|
|
|
group_id: groupId
|
2015-06-24 09:19:39 -04:00
|
|
|
|
}
|
|
|
|
|
}).then(json => {
|
2018-07-19 14:33:11 -04:00
|
|
|
|
// don’t fill for large multi column tables
|
|
|
|
|
// which are not date based
|
|
|
|
|
const modes = json.report.modes;
|
|
|
|
|
if (modes.length !== 1 && modes[0] !== "table") {
|
|
|
|
|
Report.fillMissingDates(json.report);
|
|
|
|
|
}
|
2018-01-12 21:43:49 -05:00
|
|
|
|
|
2015-12-01 18:31:30 -05:00
|
|
|
|
const model = Report.create({ type: type });
|
2014-07-21 13:39:23 -04:00
|
|
|
|
model.setProperties(json.report);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
|
|
|
|
|
if (json.report.related_report) {
|
|
|
|
|
// TODO: fillMissingDates if xaxis is date
|
2018-06-15 11:03:24 -04:00
|
|
|
|
const related = Report.create({
|
|
|
|
|
type: json.report.related_report.type
|
|
|
|
|
});
|
2018-03-15 17:10:45 -04:00
|
|
|
|
related.setProperties(json.report.related_report);
|
2018-05-17 16:44:33 -04:00
|
|
|
|
model.set("relatedReport", related);
|
2018-03-15 17:10:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
});
|
2015-06-22 13:46:51 -04:00
|
|
|
|
|
|
|
|
|
export default Report;
|