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-05-16 14:44:13 +08:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2018-05-16 22:45:21 +08:00
|
|
|
import { registerTooltip, unregisterTooltip } from "discourse/lib/tooltip";
|
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-05-16 22:45:21 +08:00
|
|
|
classNames: ["chart", "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 22:45:21 +08:00
|
|
|
didRender() {
|
|
|
|
this._super();
|
|
|
|
registerTooltip($(this.element).find("[data-tooltip]"));
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super();
|
|
|
|
unregisterTooltip($(this.element).find("[data-tooltip]"));
|
|
|
|
},
|
|
|
|
|
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-16 14:05:03 +08:00
|
|
|
data: { cache: 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
|
|
|
return Ember.RSVP.Promise.all(this.get("dataSources").map(dataSource => {
|
|
|
|
return ajax(dataSource, payload)
|
|
|
|
.then(response => {
|
2018-05-16 22:45:21 +08:00
|
|
|
this.get("reports").pushObject(this.loadReport(response.report));
|
2018-05-16 02:12:03 +08:00
|
|
|
});
|
|
|
|
}));
|
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 22:45:21 +08:00
|
|
|
const reportsForPeriod = this.get("reportsForPeriod");
|
2018-05-16 02:12:03 +08:00
|
|
|
|
2018-05-16 22:45:21 +08:00
|
|
|
const labels = Ember.makeArray(reportsForPeriod.get("firstObject.data")).map(d => d.x);
|
2018-05-16 02:12:03 +08:00
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
const data = {
|
2018-05-16 02:12:03 +08:00
|
|
|
labels,
|
2018-05-16 22:45:21 +08:00
|
|
|
datasets: reportsForPeriod.map(report => {
|
2018-05-16 02:12:03 +08:00
|
|
|
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-16 15:34:45 +08:00
|
|
|
this._chart = null;
|
2018-05-11 11:30:21 +08:00
|
|
|
}
|
2018-05-16 14:44:13 +08:00
|
|
|
|
|
|
|
loadScript("/javascripts/Chart.min.js").then(() => {
|
2018-05-16 15:34:45 +08:00
|
|
|
if (this._chart) {
|
|
|
|
this._chart.destroy();
|
|
|
|
}
|
2018-05-16 14:44:13 +08:00
|
|
|
this._chart = new window.Chart(context, this._buildChartConfig(data));
|
|
|
|
});
|
2018-05-03 21:41:41 +08:00
|
|
|
});
|
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-05-16 22:45:21 +08:00
|
|
|
tooltips: {
|
|
|
|
callbacks: {
|
|
|
|
title: (context) => moment(context[0].xLabel, "YYYY-MM-DD").format("LL")
|
|
|
|
}
|
|
|
|
},
|
2018-04-26 20:49:41 +08:00
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
2018-04-16 16:42:06 +08:00
|
|
|
responsive: true,
|
2018-05-17 08:25:10 +08:00
|
|
|
maintainAspectRatio: false,
|
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
|
|
|
});
|