2018-04-26 20:49:41 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2018-05-03 21:41:41 +08:00
|
|
|
import AsyncReport from "admin/mixins/async-report";
|
2018-04-26 20:49:41 +08:00
|
|
|
import Report from "admin/models/report";
|
2018-05-03 21:41:41 +08:00
|
|
|
import { number } from 'discourse/lib/formatter';
|
2018-04-16 16:42:06 +08:00
|
|
|
|
2018-05-11 11:30:21 +08: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 21:41:41 +08:00
|
|
|
export default Ember.Component.extend(AsyncReport, {
|
2018-04-16 18:00:49 +08:00
|
|
|
classNames: ["dashboard-mini-chart"],
|
2018-05-11 11:30:21 +08:00
|
|
|
total: 0,
|
2018-04-16 16:42:06 +08:00
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
init() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
this._colorsPool = ["rgb(0,136,204)", "rgb(235,83,148)"];
|
2018-04-16 16:42:06 +08:00
|
|
|
},
|
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
pickColorAtIndex(index) {
|
|
|
|
return this._colorsPool[index] || this._colorsPool[0];
|
2018-04-16 16:42:06 +08:00
|
|
|
},
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
fetchReport() {
|
2018-05-16 02:12:03 +08:00
|
|
|
this._super();
|
2018-04-16 22:01:29 +08:00
|
|
|
|
2018-04-26 20:49:41 +08:00
|
|
|
let payload = {
|
2018-05-11 11:30:21 +08:00
|
|
|
data: { async: true, facets: ["prev_period"] }
|
2018-04-26 20:49:41 +08:00
|
|
|
};
|
2018-04-16 16:42:06 +08:00
|
|
|
|
|
|
|
if (this.get("startDate")) {
|
2018-05-14 14:31:50 +08:00
|
|
|
payload.data.start_date = this.get("startDate").locale('en').format('YYYY-MM-DD[T]HH:mm:ss.SSSZZ');
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("endDate")) {
|
2018-05-14 14:31:50 +08:00
|
|
|
payload.data.end_date = this.get("endDate").locale('en').format('YYYY-MM-DD[T]HH:mm:ss.SSSZZ');
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
this._chart = null;
|
|
|
|
}
|
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
this.set("reports", Ember.Object.create());
|
|
|
|
this.set("reportKeys", []);
|
2018-04-16 16:42:06 +08:00
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
return Ember.RSVP.Promise.all(this.get("dataSources").map(dataSource => {
|
|
|
|
return ajax(dataSource, payload)
|
|
|
|
.then(response => {
|
|
|
|
this.set(`reports.${response.report.report_key}`, this.loadReport(response.report));
|
|
|
|
this.get("reportKeys").pushObject(response.report.report_key);
|
|
|
|
});
|
|
|
|
}));
|
2018-04-16 16:42:06 +08:00
|
|
|
},
|
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
loadReport(report, previousReport) {
|
|
|
|
Report.fillMissingDates(report);
|
2018-05-11 11:30:21 +08:00
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
if (report.data && report.data.length > 40) {
|
|
|
|
report.data = collapseWeekly(report.data, report.average);
|
|
|
|
}
|
2018-05-11 11:30:21 +08:00
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
if (previousReport && previousReport.color.length) {
|
|
|
|
report.color = previousReport.color;
|
|
|
|
} else {
|
|
|
|
const dataSourceNameIndex = this.get("dataSourceNames").split(",").indexOf(report.type);
|
|
|
|
report.color = this.pickColorAtIndex(dataSourceNameIndex);
|
2018-05-11 11:30:21 +08:00
|
|
|
}
|
2018-05-16 02:12:03 +08:00
|
|
|
|
|
|
|
return Report.create(report);
|
2018-05-11 11:30:21 +08:00
|
|
|
},
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
renderReport() {
|
2018-05-16 02:12:03 +08:00
|
|
|
this._super();
|
2018-04-20 00:19:21 +08:00
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
const $chartCanvas = this.$(".chart-canvas");
|
|
|
|
if (!$chartCanvas.length) return;
|
|
|
|
const context = $chartCanvas[0].getContext("2d");
|
2018-04-16 16:42:06 +08:00
|
|
|
|
2018-05-16 02:12:03 +08:00
|
|
|
const reports = _.values(this.get("reports"));
|
|
|
|
|
|
|
|
const labels = Ember.makeArray(reports.get("firstObject.data")).map(d => d.x);
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
const data = {
|
2018-05-16 02:12:03 +08:00
|
|
|
labels,
|
|
|
|
datasets: reports.map(report => {
|
|
|
|
return {
|
|
|
|
data: Ember.makeArray(report.data).map(d => d.y),
|
|
|
|
backgroundColor: "rgba(200,220,240,0.3)",
|
|
|
|
borderColor: report.color
|
|
|
|
};
|
|
|
|
})
|
2018-05-03 21:41:41 +08:00
|
|
|
};
|
|
|
|
|
2018-05-11 11:30:21 +08:00
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
}
|
2018-05-03 21:41:41 +08:00
|
|
|
this._chart = new window.Chart(context, this._buildChartConfig(data));
|
|
|
|
});
|
2018-04-16 22:01:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildChartConfig(data) {
|
|
|
|
return {
|
2018-04-16 16:42:06 +08:00
|
|
|
type: "line",
|
2018-04-16 22:01:29 +08:00
|
|
|
data,
|
2018-04-16 16:42:06 +08:00
|
|
|
options: {
|
2018-04-26 20:49:41 +08:00
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
responsive: true,
|
2018-04-26 20:49:41 +08:00
|
|
|
layout: {
|
|
|
|
padding: {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0
|
|
|
|
}
|
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
scales: {
|
2018-04-26 20:49:41 +08:00
|
|
|
yAxes: [{
|
|
|
|
display: true,
|
2018-05-03 21:41:41 +08:00
|
|
|
ticks: { callback: (label) => number(label) }
|
2018-04-26 20:49:41 +08:00
|
|
|
}],
|
|
|
|
xAxes: [{
|
|
|
|
display: true,
|
2018-05-16 02:12:03 +08:00
|
|
|
gridLines: { display: false },
|
2018-04-26 20:49:41 +08:00
|
|
|
type: "time",
|
|
|
|
time: {
|
|
|
|
parser: "YYYY-MM-DD"
|
2018-04-17 17:01:06 +08:00
|
|
|
}
|
2018-04-26 20:49:41 +08:00
|
|
|
}],
|
2018-04-16 16:42:06 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2018-04-26 20:49:41 +08:00
|
|
|
}
|
2018-04-16 16:42:06 +08:00
|
|
|
});
|