2018-04-26 08:49:41 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2018-05-03 09:41:41 -04:00
|
|
|
import AsyncReport from "admin/mixins/async-report";
|
2018-04-26 08:49:41 -04:00
|
|
|
import Report from "admin/models/report";
|
2018-05-03 09:41:41 -04:00
|
|
|
import { number } from 'discourse/lib/formatter';
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
function collapseWeekly(data, average) {
|
|
|
|
let aggregate = [];
|
|
|
|
let bucket, i;
|
|
|
|
let offset = data.length % 7;
|
|
|
|
for(i = offset; i < data.length; i++) {
|
|
|
|
|
|
|
|
if (bucket && (i % 7 === offset)) {
|
|
|
|
if (average) {
|
|
|
|
bucket.y = parseFloat((bucket.y / 7.0).toFixed(2));
|
|
|
|
}
|
|
|
|
aggregate.push(bucket);
|
|
|
|
bucket = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket = bucket || { x: data[i].x, y: 0 };
|
|
|
|
bucket.y += data[i].y;
|
|
|
|
}
|
|
|
|
return aggregate;
|
|
|
|
}
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
export default Ember.Component.extend(AsyncReport, {
|
2018-04-16 06:00:49 -04:00
|
|
|
classNames: ["dashboard-mini-chart"],
|
2018-05-10 23:30:21 -04:00
|
|
|
classNameBindings: ["trend", "oneDataPoint"],
|
2018-05-03 09:41:41 -04:00
|
|
|
isLoading: true,
|
2018-05-10 23:30:21 -04:00
|
|
|
trend: Ember.computed.alias("report.trend"),
|
2018-04-16 04:42:06 -04:00
|
|
|
oneDataPoint: false,
|
|
|
|
backgroundColor: "rgba(200,220,240,0.3)",
|
|
|
|
borderColor: "#08C",
|
2018-05-03 09:41:41 -04:00
|
|
|
average: false,
|
2018-05-10 23:30:21 -04:00
|
|
|
total: 0,
|
2018-04-16 04:42:06 -04:00
|
|
|
|
|
|
|
@computed("dataSourceName")
|
|
|
|
dataSource(dataSourceName) {
|
2018-04-18 15:30:41 -04:00
|
|
|
if (dataSourceName) {
|
|
|
|
return `/admin/reports/${dataSourceName}`;
|
|
|
|
}
|
2018-04-16 04:42:06 -04:00
|
|
|
},
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
@computed("trend")
|
|
|
|
trendIcon(trend) {
|
|
|
|
switch (trend) {
|
2018-04-26 08:49:41 -04:00
|
|
|
case "trending-up":
|
|
|
|
return "angle-up";
|
|
|
|
case "trending-down":
|
|
|
|
return "angle-down";
|
|
|
|
case "high-trending-up":
|
|
|
|
return "angle-double-up";
|
|
|
|
case "high-trending-down":
|
|
|
|
return "angle-double-down";
|
|
|
|
default:
|
|
|
|
return null;
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
fetchReport() {
|
2018-04-16 10:01:29 -04:00
|
|
|
this.set("isLoading", true);
|
|
|
|
|
2018-04-26 08:49:41 -04:00
|
|
|
let payload = {
|
2018-05-10 23:30:21 -04:00
|
|
|
data: { async: true, facets: ["prev_period"] }
|
2018-04-26 08:49:41 -04:00
|
|
|
};
|
2018-04-16 04:42:06 -04:00
|
|
|
|
|
|
|
if (this.get("startDate")) {
|
2018-05-03 09:41:41 -04:00
|
|
|
payload.data.start_date = this.get("startDate").format('YYYY-MM-DD[T]HH:mm:ss.SSSZZ');
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("endDate")) {
|
2018-05-03 09:41:41 -04:00
|
|
|
payload.data.end_date = this.get("endDate").format('YYYY-MM-DD[T]HH:mm:ss.SSSZZ');
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
this._chart = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("report", null);
|
|
|
|
|
2018-04-16 04:42:06 -04:00
|
|
|
ajax(this.get("dataSource"), payload)
|
|
|
|
.then((response) => {
|
2018-05-10 23:30:21 -04:00
|
|
|
this.set('reportKey', response.report.report_key);
|
|
|
|
|
|
|
|
this.loadReport(response.report);
|
2018-04-16 04:42:06 -04:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2018-05-03 09:41:41 -04:00
|
|
|
if (this.get("oneDataPoint")) {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
if (!Ember.isEmpty(this.get("report.data"))) {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
this.renderReport();
|
|
|
|
}
|
2018-04-16 04:42:06 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
loadReport(report) {
|
|
|
|
if (report.data) {
|
|
|
|
Report.fillMissingDates(report);
|
|
|
|
|
|
|
|
if (report.data && report.data.length > 40) {
|
|
|
|
report.data = collapseWeekly(report.data, this.get("average"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = Report.create(report);
|
|
|
|
this._setPropertiesFromReport(model);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
renderReport() {
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) { return; }
|
|
|
|
if (this.get("oneDataPoint")) return;
|
2018-04-19 12:19:21 -04:00
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
const $chartCanvas = this.$(".chart-canvas");
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
if (!$chartCanvas.length) return;
|
|
|
|
const context = $chartCanvas[0].getContext("2d");
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
const data = {
|
|
|
|
labels: this.get("labels"),
|
|
|
|
datasets: [{
|
|
|
|
data: Ember.makeArray(this.get("values")),
|
|
|
|
backgroundColor: this.get("backgroundColor"),
|
|
|
|
borderColor: this.get("borderColor")
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2018-05-10 23:30:21 -04:00
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
}
|
2018-05-03 09:41:41 -04:00
|
|
|
this._chart = new window.Chart(context, this._buildChartConfig(data));
|
|
|
|
});
|
2018-04-16 10:01:29 -04:00
|
|
|
},
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
_setPropertiesFromReport(report) {
|
2018-04-26 08:49:41 -04:00
|
|
|
const oneDataPoint = (this.get("startDate") && this.get("endDate")) &&
|
|
|
|
this.get("startDate").isSame(this.get("endDate"), "day");
|
|
|
|
|
2018-05-03 09:41:41 -04:00
|
|
|
report.set("average", this.get("average"));
|
|
|
|
this.setProperties({ oneDataPoint, report });
|
2018-04-18 15:30:41 -04:00
|
|
|
},
|
|
|
|
|
2018-04-16 10:01:29 -04:00
|
|
|
_buildChartConfig(data) {
|
|
|
|
return {
|
2018-04-16 04:42:06 -04:00
|
|
|
type: "line",
|
2018-04-16 10:01:29 -04:00
|
|
|
data,
|
2018-04-16 04:42:06 -04:00
|
|
|
options: {
|
2018-04-26 08:49:41 -04:00
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
2018-04-16 04:42:06 -04:00
|
|
|
responsive: true,
|
2018-04-26 08:49:41 -04:00
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0
|
|
|
|
}
|
|
|
|
},
|
2018-04-16 04:42:06 -04:00
|
|
|
scales: {
|
2018-04-26 08:49:41 -04:00
|
|
|
yAxes: [{
|
|
|
|
display: true,
|
2018-05-03 09:41:41 -04:00
|
|
|
ticks: { callback: (label) => number(label) }
|
2018-04-26 08:49:41 -04:00
|
|
|
}],
|
|
|
|
xAxes: [{
|
|
|
|
display: true,
|
|
|
|
type: "time",
|
|
|
|
time: {
|
|
|
|
parser: "YYYY-MM-DD"
|
2018-04-17 05:01:06 -04:00
|
|
|
}
|
2018-04-26 08:49:41 -04:00
|
|
|
}],
|
2018-04-16 04:42:06 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2018-04-26 08:49:41 -04:00
|
|
|
}
|
2018-04-16 04:42:06 -04:00
|
|
|
});
|