2018-04-16 04:42:06 -04:00
|
|
|
import { ajax } from 'discourse/lib/ajax';
|
|
|
|
import computed from 'ember-addons/ember-computed-decorators';
|
|
|
|
import loadScript from 'discourse/lib/load-script';
|
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
2018-04-16 06:00:49 -04:00
|
|
|
classNames: ["dashboard-mini-chart"],
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-04-16 10:01:29 -04:00
|
|
|
classNameBindings: ["trend", "oneDataPoint", "isLoading"],
|
2018-04-16 04:42:06 -04:00
|
|
|
|
|
|
|
isLoading: false,
|
|
|
|
total: null,
|
|
|
|
trend: null,
|
|
|
|
title: null,
|
|
|
|
chartData: null,
|
|
|
|
oneDataPoint: false,
|
|
|
|
backgroundColor: "rgba(200,220,240,0.3)",
|
|
|
|
borderColor: "#08C",
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
loadScript("/javascripts/Chart.min.js").then(() => {
|
|
|
|
this.fetchReport.apply(this);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
didUpdateAttrs() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
this.fetchReport.apply(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("dataSourceName")
|
|
|
|
dataSource(dataSourceName) {
|
|
|
|
return `/admin/reports/${dataSourceName}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("trend")
|
|
|
|
trendIcon(trend) {
|
|
|
|
if (trend === "stable") {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return `angle-${trend}`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchReport() {
|
2018-04-16 10:01:29 -04:00
|
|
|
this.set("isLoading", true);
|
|
|
|
|
2018-04-16 04:42:06 -04:00
|
|
|
let payload = {data: {}};
|
|
|
|
|
|
|
|
if (this.get("startDate")) {
|
|
|
|
payload.data.start_date = this.get("startDate").toISOString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("endDate")) {
|
|
|
|
payload.data.end_date = this.get("endDate").toISOString();
|
|
|
|
}
|
|
|
|
|
|
|
|
ajax(this.get("dataSource"), payload)
|
|
|
|
.then((response) => {
|
|
|
|
const report = response.report;
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
oneDataPoint: (this.get("startDate") && this.get("endDate")) &&
|
|
|
|
this.get("startDate").isSame(this.get("endDate"), 'day'),
|
|
|
|
total: report.total,
|
|
|
|
title: report.title,
|
|
|
|
trend: this._computeTrend(report.total, report.prev30Days),
|
|
|
|
chartData: report.data
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
|
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
if (!this.get("oneDataPoint")) {
|
|
|
|
this.drawChart();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
drawChart() {
|
2018-04-16 10:01:29 -04:00
|
|
|
const context = this.$(".chart-canvas")[0].getContext("2d");
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-04-16 10:01:29 -04:00
|
|
|
const data = {
|
2018-04-16 04:42:06 -04:00
|
|
|
labels: this.get("chartData").map(r => r.x),
|
|
|
|
datasets: [{
|
|
|
|
data: this.get("chartData").map(r => r.y),
|
|
|
|
backgroundColor: this.get("backgroundColor"),
|
|
|
|
borderColor: this.get("borderColor")
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2018-04-16 10:01:29 -04:00
|
|
|
this._chart = new window.Chart(context, this._buildChartConfig(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
_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: {
|
|
|
|
legend: { display: false },
|
|
|
|
responsive: true,
|
2018-04-16 10:01:29 -04:00
|
|
|
layout: { padding: { left: 0, top: 0, right: 0, bottom: 0 } },
|
2018-04-16 04:42:06 -04:00
|
|
|
scales: {
|
2018-04-16 10:01:29 -04:00
|
|
|
yAxes: [{ display: true, ticks: { suggestedMin: 0 } }],
|
2018-04-16 04:42:06 -04:00
|
|
|
xAxes: [{ display: true }],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2018-04-16 10:01:29 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_computeTrend(total, prevTotal) {
|
|
|
|
const percentChange = ((total - prevTotal) / prevTotal) * 100;
|
2018-04-16 04:42:06 -04:00
|
|
|
|
2018-04-16 10:01:29 -04:00
|
|
|
if (percentChange > 50) return "double-up";
|
|
|
|
if (percentChange > 0) return "up";
|
|
|
|
if (percentChange === 0) return "stable";
|
|
|
|
if (percentChange < 50) return "double-down";
|
|
|
|
if (percentChange < 0) return "down";
|
|
|
|
},
|
2018-04-16 04:42:06 -04:00
|
|
|
});
|