2018-08-24 21:28:01 +08:00
|
|
|
import ReportLoader from "discourse/lib/reports-loader";
|
2018-07-26 01:16:35 +08:00
|
|
|
import Category from "discourse/models/category";
|
2018-07-20 02:33:11 +08:00
|
|
|
import { exportEntity } from "discourse/lib/export-csv";
|
|
|
|
import { outputExportResult } from "discourse/lib/export-result";
|
2018-08-01 05:35:13 +08:00
|
|
|
import { SCHEMA_VERSION, default as Report } from "admin/models/report";
|
2018-07-20 02:33:11 +08:00
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2018-08-17 22:19:25 +08:00
|
|
|
import {
|
|
|
|
registerHoverTooltip,
|
|
|
|
unregisterHoverTooltip
|
|
|
|
} from "discourse/lib/tooltip";
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
const TABLE_OPTIONS = {
|
|
|
|
perPage: 8,
|
|
|
|
total: true,
|
2018-08-02 06:40:59 +08:00
|
|
|
limit: 20,
|
|
|
|
formatNumbers: true
|
2018-07-20 02:33:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const CHART_OPTIONS = {};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
2018-08-17 22:19:25 +08:00
|
|
|
classNameBindings: ["isEnabled", "isLoading", "dasherizedDataSourceName"],
|
2018-07-20 02:33:11 +08:00
|
|
|
classNames: ["admin-report"],
|
|
|
|
isEnabled: true,
|
2018-11-08 00:59:42 +08:00
|
|
|
disabledLabel: I18n.t("admin.dashboard.disabled"),
|
2018-07-20 02:33:11 +08:00
|
|
|
isLoading: false,
|
2018-08-22 17:25:12 +08:00
|
|
|
rateLimitationString: null,
|
2018-07-20 02:33:11 +08:00
|
|
|
dataSourceName: null,
|
|
|
|
report: null,
|
|
|
|
model: null,
|
|
|
|
reportOptions: null,
|
|
|
|
forcedModes: null,
|
|
|
|
showAllReportsLink: false,
|
2018-07-27 13:22:00 +08:00
|
|
|
filters: null,
|
2018-07-20 02:33:11 +08:00
|
|
|
startDate: null,
|
|
|
|
endDate: null,
|
2018-07-27 07:08:06 +08:00
|
|
|
category: null,
|
2018-07-26 02:02:21 +08:00
|
|
|
groupId: null,
|
2018-07-20 02:33:11 +08:00
|
|
|
showTrend: false,
|
|
|
|
showHeader: true,
|
|
|
|
showTitle: true,
|
|
|
|
showFilteringUI: false,
|
|
|
|
showCategoryOptions: Ember.computed.alias("model.category_filtering"),
|
|
|
|
showDatesOptions: Ember.computed.alias("model.dates_filtering"),
|
|
|
|
showGroupOptions: Ember.computed.alias("model.group_filtering"),
|
|
|
|
showExport: Ember.computed.not("model.onlyTable"),
|
2018-07-26 01:16:35 +08:00
|
|
|
showRefresh: Ember.computed.or(
|
2018-07-20 02:33:11 +08:00
|
|
|
"showCategoryOptions",
|
|
|
|
"showDatesOptions",
|
2018-07-26 01:16:35 +08:00
|
|
|
"showGroupOptions"
|
2018-07-20 02:33:11 +08:00
|
|
|
),
|
2018-08-17 22:19:25 +08:00
|
|
|
shouldDisplayTrend: Ember.computed.and("showTrend", "model.prev_period"),
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._reports = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2018-07-27 13:22:00 +08:00
|
|
|
const state = this.get("filters") || {};
|
2018-08-17 22:19:25 +08:00
|
|
|
|
2018-07-26 01:16:35 +08:00
|
|
|
this.setProperties({
|
|
|
|
category: Category.findById(state.categoryId),
|
|
|
|
groupId: state.groupId,
|
|
|
|
startDate: state.startDate,
|
|
|
|
endDate: state.endDate
|
|
|
|
});
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
if (this.get("report")) {
|
|
|
|
this._renderReport(
|
|
|
|
this.get("report"),
|
|
|
|
this.get("forcedModes"),
|
|
|
|
this.get("currentMode")
|
|
|
|
);
|
|
|
|
} else if (this.get("dataSourceName")) {
|
2018-08-24 21:28:01 +08:00
|
|
|
this._fetchReport();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didRender() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2018-08-17 22:19:25 +08:00
|
|
|
registerHoverTooltip($(".info[data-tooltip]"));
|
2018-07-20 02:33:11 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2018-08-17 22:19:25 +08:00
|
|
|
unregisterHoverTooltip($(".info[data-tooltip]"));
|
2018-07-20 02:33:11 +08:00
|
|
|
},
|
|
|
|
|
2018-08-01 09:23:28 +08:00
|
|
|
showError: Ember.computed.or("showTimeoutError", "showExceptionError"),
|
|
|
|
showTimeoutError: Ember.computed.equal("model.error", "timeout"),
|
|
|
|
showExceptionError: Ember.computed.equal("model.error", "exception"),
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2018-07-26 02:28:41 +08:00
|
|
|
hasData: Ember.computed.notEmpty("model.data"),
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
@computed("dataSourceName", "model.type")
|
|
|
|
dasherizedDataSourceName(dataSourceName, type) {
|
|
|
|
return (dataSourceName || type || "undefined").replace(/_/g, "-");
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("dataSourceName", "model.type")
|
|
|
|
dataSource(dataSourceName, type) {
|
|
|
|
dataSourceName = dataSourceName || type;
|
|
|
|
return `/admin/reports/${dataSourceName}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("displayedModes.length")
|
|
|
|
showModes(displayedModesLength) {
|
|
|
|
return displayedModesLength > 1;
|
|
|
|
},
|
|
|
|
|
2018-07-27 07:08:06 +08:00
|
|
|
categoryId: Ember.computed.alias("category.id"),
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
@computed("currentMode", "model.modes", "forcedModes")
|
|
|
|
displayedModes(currentMode, reportModes, forcedModes) {
|
|
|
|
const modes = forcedModes ? forcedModes.split(",") : reportModes;
|
|
|
|
|
|
|
|
return Ember.makeArray(modes).map(mode => {
|
2018-10-25 04:09:36 +08:00
|
|
|
const base = `btn-default mode-btn ${mode}`;
|
2018-08-17 22:19:25 +08:00
|
|
|
const cssClass = currentMode === mode ? `${base} is-current` : base;
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
mode,
|
|
|
|
cssClass,
|
|
|
|
icon: mode === "table" ? "table" : "signal"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed()
|
|
|
|
groupOptions() {
|
|
|
|
const arr = [
|
|
|
|
{ name: I18n.t("admin.dashboard.reports.groups"), value: "all" }
|
|
|
|
];
|
|
|
|
return arr.concat(
|
2018-08-17 22:19:25 +08:00
|
|
|
(this.site.groups || []).map(i => {
|
2018-07-20 02:33:11 +08:00
|
|
|
return { name: i["name"], value: i["id"] };
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("currentMode")
|
|
|
|
modeComponent(currentMode) {
|
|
|
|
return `admin-report-${currentMode}`;
|
|
|
|
},
|
|
|
|
|
2018-07-20 07:30:13 +08:00
|
|
|
@computed("startDate")
|
|
|
|
normalizedStartDate(startDate) {
|
|
|
|
return startDate && typeof startDate.isValid === "function"
|
2018-08-17 22:19:25 +08:00
|
|
|
? moment
|
|
|
|
.utc(startDate.toISOString())
|
|
|
|
.locale("en")
|
|
|
|
.format("YYYYMMDD")
|
|
|
|
: moment(startDate)
|
|
|
|
.locale("en")
|
|
|
|
.format("YYYYMMDD");
|
2018-07-20 07:30:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("endDate")
|
|
|
|
normalizedEndDate(endDate) {
|
|
|
|
return endDate && typeof endDate.isValid === "function"
|
2018-08-17 22:19:25 +08:00
|
|
|
? moment
|
|
|
|
.utc(endDate.toISOString())
|
|
|
|
.locale("en")
|
|
|
|
.format("YYYYMMDD")
|
|
|
|
: moment(endDate)
|
|
|
|
.locale("en")
|
|
|
|
.format("YYYYMMDD");
|
2018-07-20 07:30:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed(
|
|
|
|
"dataSourceName",
|
|
|
|
"categoryId",
|
|
|
|
"groupId",
|
|
|
|
"normalizedStartDate",
|
|
|
|
"normalizedEndDate"
|
|
|
|
)
|
|
|
|
reportKey(dataSourceName, categoryId, groupId, startDate, endDate) {
|
|
|
|
if (!dataSourceName || !startDate || !endDate) return null;
|
|
|
|
|
2018-08-01 05:35:13 +08:00
|
|
|
let reportKey = "reports:";
|
|
|
|
reportKey += [
|
|
|
|
dataSourceName,
|
|
|
|
categoryId,
|
|
|
|
startDate.replace(/-/g, ""),
|
|
|
|
endDate.replace(/-/g, ""),
|
|
|
|
groupId,
|
|
|
|
"[:prev_period]",
|
|
|
|
this.get("reportOptions.table.limit"),
|
|
|
|
SCHEMA_VERSION
|
|
|
|
]
|
|
|
|
.filter(x => x)
|
|
|
|
.map(x => x.toString())
|
|
|
|
.join(":");
|
2018-07-20 07:30:13 +08:00
|
|
|
|
|
|
|
return reportKey;
|
|
|
|
},
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
actions: {
|
2018-07-26 01:16:35 +08:00
|
|
|
refreshReport() {
|
|
|
|
this.attrs.onRefresh({
|
2018-07-27 07:08:06 +08:00
|
|
|
categoryId: this.get("categoryId"),
|
2018-07-26 01:16:35 +08:00
|
|
|
groupId: this.get("groupId"),
|
|
|
|
startDate: this.get("startDate"),
|
|
|
|
endDate: this.get("endDate")
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
exportCsv() {
|
|
|
|
exportEntity("report", {
|
|
|
|
name: this.get("model.type"),
|
|
|
|
start_date: this.get("startDate"),
|
|
|
|
end_date: this.get("endDate"),
|
|
|
|
category_id:
|
|
|
|
this.get("categoryId") === "all" ? undefined : this.get("categoryId"),
|
|
|
|
group_id:
|
|
|
|
this.get("groupId") === "all" ? undefined : this.get("groupId")
|
|
|
|
}).then(outputExportResult);
|
|
|
|
},
|
|
|
|
|
|
|
|
changeMode(mode) {
|
|
|
|
this.set("currentMode", mode);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_computeReport() {
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._reports || !this._reports.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// on a slow network _fetchReport could be called multiple times between
|
|
|
|
// T and T+x, and all the ajax responses would occur after T+(x+y)
|
|
|
|
// to avoid any inconsistencies we filter by period and make sure
|
|
|
|
// the array contains only unique values
|
|
|
|
let filteredReports = this._reports.uniqBy("report_key");
|
|
|
|
let report;
|
|
|
|
|
|
|
|
const sort = r => {
|
|
|
|
if (r.length > 1) {
|
|
|
|
return r.findBy("type", this.get("dataSourceName"));
|
|
|
|
} else {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-20 07:30:13 +08:00
|
|
|
if (!this.get("startDate") || !this.get("endDate")) {
|
2018-07-20 02:33:11 +08:00
|
|
|
report = sort(filteredReports)[0];
|
|
|
|
} else {
|
2018-07-20 07:30:13 +08:00
|
|
|
let reportKey = this.get("reportKey");
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
report = sort(
|
|
|
|
filteredReports.filter(r => r.report_key.includes(reportKey))
|
|
|
|
)[0];
|
|
|
|
|
2018-07-20 20:16:29 +08:00
|
|
|
if (!report) return;
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._renderReport(
|
|
|
|
report,
|
|
|
|
this.get("forcedModes"),
|
|
|
|
this.get("currentMode")
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
_renderReport(report, forcedModes, currentMode) {
|
|
|
|
const modes = forcedModes ? forcedModes.split(",") : report.modes;
|
2018-07-26 02:02:21 +08:00
|
|
|
currentMode = currentMode || (modes ? modes[0] : null);
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
model: report,
|
|
|
|
currentMode,
|
|
|
|
options: this._buildOptions(currentMode)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_fetchReport() {
|
|
|
|
this._super();
|
|
|
|
|
2018-08-22 17:25:12 +08:00
|
|
|
this.setProperties({ isLoading: true, rateLimitationString: null });
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2018-08-24 21:28:01 +08:00
|
|
|
Ember.run.next(() => {
|
|
|
|
let payload = this._buildPayload(["prev_period"]);
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2018-08-24 21:28:01 +08:00
|
|
|
const callback = response => {
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
2018-08-24 21:28:01 +08:00
|
|
|
|
|
|
|
this.set("isLoading", false);
|
|
|
|
|
|
|
|
if (response === 429) {
|
2018-08-23 00:13:29 +08:00
|
|
|
this.set(
|
|
|
|
"rateLimitationString",
|
|
|
|
I18n.t("admin.dashboard.too_many_requests")
|
|
|
|
);
|
2018-08-24 21:28:01 +08:00
|
|
|
} else if (response === 500) {
|
|
|
|
this.set("model.error", "exception");
|
|
|
|
} else if (response) {
|
|
|
|
this._reports.push(this._loadReport(response));
|
|
|
|
this._computeReport();
|
2018-08-22 17:25:12 +08:00
|
|
|
}
|
2018-08-24 21:28:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ReportLoader.enqueue(this.get("dataSourceName"), payload.data, callback);
|
|
|
|
});
|
2018-07-20 02:33:11 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildPayload(facets) {
|
|
|
|
let payload = { data: { cache: true, facets } };
|
|
|
|
|
|
|
|
if (this.get("startDate")) {
|
2018-08-17 22:19:25 +08:00
|
|
|
payload.data.start_date = moment
|
|
|
|
.utc(this.get("startDate"), "YYYY-MM-DD")
|
|
|
|
.toISOString();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("endDate")) {
|
2018-08-17 22:19:25 +08:00
|
|
|
payload.data.end_date = moment
|
|
|
|
.utc(this.get("endDate"), "YYYY-MM-DD")
|
|
|
|
.toISOString();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("groupId") && this.get("groupId") !== "all") {
|
|
|
|
payload.data.group_id = this.get("groupId");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("categoryId") && this.get("categoryId") !== "all") {
|
|
|
|
payload.data.category_id = this.get("categoryId");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("reportOptions.table.limit")) {
|
|
|
|
payload.data.limit = this.get("reportOptions.table.limit");
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildOptions(mode) {
|
|
|
|
if (mode === "table") {
|
|
|
|
const tableOptions = JSON.parse(JSON.stringify(TABLE_OPTIONS));
|
|
|
|
return Ember.Object.create(
|
2018-08-02 06:40:59 +08:00
|
|
|
Object.assign(tableOptions, this.get("reportOptions.table") || {})
|
2018-07-20 02:33:11 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const chartOptions = JSON.parse(JSON.stringify(CHART_OPTIONS));
|
|
|
|
return Ember.Object.create(
|
2018-08-02 06:40:59 +08:00
|
|
|
Object.assign(chartOptions, this.get("reportOptions.chart") || {})
|
2018-07-20 02:33:11 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_loadReport(jsonReport) {
|
|
|
|
Report.fillMissingDates(jsonReport, { filledField: "chartData" });
|
|
|
|
|
|
|
|
if (jsonReport.chartData && jsonReport.chartData.length > 40) {
|
|
|
|
jsonReport.chartData = collapseWeekly(
|
|
|
|
jsonReport.chartData,
|
|
|
|
jsonReport.average
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonReport.prev_data) {
|
|
|
|
Report.fillMissingDates(jsonReport, {
|
|
|
|
filledField: "prevChartData",
|
|
|
|
dataField: "prev_data",
|
|
|
|
starDate: jsonReport.prev_start_date,
|
|
|
|
endDate: jsonReport.prev_end_date
|
|
|
|
});
|
|
|
|
|
|
|
|
if (jsonReport.prevChartData && jsonReport.prevChartData.length > 40) {
|
|
|
|
jsonReport.prevChartData = collapseWeekly(
|
|
|
|
jsonReport.prevChartData,
|
|
|
|
jsonReport.average
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Report.create(jsonReport);
|
|
|
|
}
|
|
|
|
});
|