2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-11-02 01:06:50 +08:00
|
|
|
import { makeArray } from "discourse-common/lib/helpers";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { alias, or, and, reads, equal, notEmpty } from "@ember/object/computed";
|
2019-10-30 03:23:50 +08:00
|
|
|
import EmberObject from "@ember/object";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { next } from "@ember/runloop";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2018-08-24 21:28:01 +08:00
|
|
|
import ReportLoader from "discourse/lib/reports-loader";
|
2018-07-20 02:33:11 +08:00
|
|
|
import { exportEntity } from "discourse/lib/export-csv";
|
|
|
|
import { outputExportResult } from "discourse/lib/export-result";
|
2019-04-26 18:17:10 +08:00
|
|
|
import { isNumeric } from "discourse/lib/utilities";
|
2018-08-01 05:35:13 +08:00
|
|
|
import { SCHEMA_VERSION, default as Report } from "admin/models/report";
|
2019-11-08 01:20:35 +08:00
|
|
|
import ENV from "discourse-common/config/environment";
|
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;
|
|
|
|
}
|
2019-01-21 22:17:04 +08:00
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
return aggregate;
|
|
|
|
}
|
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default 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,
|
|
|
|
showTrend: false,
|
|
|
|
showHeader: true,
|
|
|
|
showTitle: true,
|
|
|
|
showFilteringUI: false,
|
2019-10-31 04:28:29 +08:00
|
|
|
showDatesOptions: alias("model.dates_filtering"),
|
2019-10-31 07:18:29 +08:00
|
|
|
showRefresh: or("showDatesOptions", "model.available_filters.length"),
|
2019-10-31 04:28:29 +08:00
|
|
|
shouldDisplayTrend: and("showTrend", "model.prev_period"),
|
2018-07-20 02:33:11 +08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this._reports = [];
|
|
|
|
},
|
|
|
|
|
2019-10-31 04:28:29 +08:00
|
|
|
startDate: reads("filters.startDate"),
|
|
|
|
endDate: reads("filters.endDate"),
|
2019-04-26 18:17:10 +08:00
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.report) {
|
2019-05-27 16:42:53 +08:00
|
|
|
this._renderReport(this.report, this.forcedModes, this.currentMode);
|
2019-05-27 16:15:39 +08:00
|
|
|
} else if (this.dataSourceName) {
|
2018-08-24 21:28:01 +08:00
|
|
|
this._fetchReport();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-31 07:18:29 +08:00
|
|
|
showError: or("showTimeoutError", "showExceptionError", "showNotFoundError"),
|
2019-10-31 04:28:29 +08:00
|
|
|
showNotFoundError: equal("model.error", "not_found"),
|
|
|
|
showTimeoutError: equal("model.error", "timeout"),
|
|
|
|
showExceptionError: equal("model.error", "exception"),
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2019-10-31 04:28:29 +08:00
|
|
|
hasData: notEmpty("model.data"),
|
2018-07-26 02:28:41 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("dataSourceName", "model.type")
|
2018-07-20 02:33:11 +08:00
|
|
|
dasherizedDataSourceName(dataSourceName, type) {
|
|
|
|
return (dataSourceName || type || "undefined").replace(/_/g, "-");
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("dataSourceName", "model.type")
|
2018-07-20 02:33:11 +08:00
|
|
|
dataSource(dataSourceName, type) {
|
|
|
|
dataSourceName = dataSourceName || type;
|
|
|
|
return `/admin/reports/${dataSourceName}`;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("displayedModes.length")
|
2018-07-20 02:33:11 +08:00
|
|
|
showModes(displayedModesLength) {
|
|
|
|
return displayedModesLength > 1;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("currentMode", "model.modes", "forcedModes")
|
2018-07-20 02:33:11 +08:00
|
|
|
displayedModes(currentMode, reportModes, forcedModes) {
|
|
|
|
const modes = forcedModes ? forcedModes.split(",") : reportModes;
|
|
|
|
|
2019-11-01 01:55:01 +08:00
|
|
|
return 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"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("currentMode")
|
2018-07-20 02:33:11 +08:00
|
|
|
modeComponent(currentMode) {
|
|
|
|
return `admin-report-${currentMode}`;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("startDate")
|
2018-07-20 07:30:13 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("endDate")
|
2018-07-20 07:30:13 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2018-07-20 07:30:13 +08:00
|
|
|
"dataSourceName",
|
|
|
|
"normalizedStartDate",
|
2019-04-26 18:17:10 +08:00
|
|
|
"normalizedEndDate",
|
|
|
|
"filters.customFilters"
|
2018-07-20 07:30:13 +08:00
|
|
|
)
|
2019-04-26 18:17:10 +08:00
|
|
|
reportKey(dataSourceName, startDate, endDate, customFilters) {
|
2018-07-20 07:30:13 +08:00
|
|
|
if (!dataSourceName || !startDate || !endDate) return null;
|
|
|
|
|
2018-08-01 05:35:13 +08:00
|
|
|
let reportKey = "reports:";
|
|
|
|
reportKey += [
|
|
|
|
dataSourceName,
|
2019-11-08 01:20:35 +08:00
|
|
|
ENV.environment === "test" ? "start" : startDate.replace(/-/g, ""),
|
|
|
|
ENV.environment === "test" ? "end" : endDate.replace(/-/g, ""),
|
2018-08-01 05:35:13 +08:00
|
|
|
"[:prev_period]",
|
|
|
|
this.get("reportOptions.table.limit"),
|
2019-04-26 18:17:10 +08:00
|
|
|
customFilters
|
2019-06-28 15:36:38 +08:00
|
|
|
? JSON.stringify(customFilters, (key, value) =>
|
|
|
|
isNumeric(value) ? value.toString() : value
|
2019-04-26 18:17:10 +08:00
|
|
|
)
|
|
|
|
: null,
|
2018-08-01 05:35:13 +08:00
|
|
|
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: {
|
2019-08-06 14:41:21 +08:00
|
|
|
onChangeEndDate(date) {
|
|
|
|
const startDate = moment(this.startDate);
|
|
|
|
const newEndDate = moment(date).endOf("day");
|
|
|
|
|
|
|
|
if (newEndDate.isSameOrAfter(startDate)) {
|
|
|
|
this.set("endDate", newEndDate.format("YYYY-MM-DD"));
|
|
|
|
} else {
|
|
|
|
this.set("endDate", startDate.endOf("day").format("YYYY-MM-DD"));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.send("refreshReport");
|
|
|
|
},
|
|
|
|
|
|
|
|
onChangeStartDate(date) {
|
|
|
|
const endDate = moment(this.endDate);
|
|
|
|
const newStartDate = moment(date).startOf("day");
|
|
|
|
|
|
|
|
if (newStartDate.isSameOrBefore(endDate)) {
|
|
|
|
this.set("startDate", newStartDate.format("YYYY-MM-DD"));
|
|
|
|
} else {
|
|
|
|
this.set("startDate", endDate.startOf("day").format("YYYY-MM-DD"));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.send("refreshReport");
|
|
|
|
},
|
|
|
|
|
2019-04-26 18:17:10 +08:00
|
|
|
applyFilter(id, value) {
|
|
|
|
let customFilters = this.get("filters.customFilters") || {};
|
|
|
|
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
delete customFilters[id];
|
|
|
|
} else {
|
|
|
|
customFilters[id] = value;
|
FEATURE: Exposing a way to add a generic report filter (#6816)
* FEATURE: Exposing a way to add a generic report filter
## Why do we need this change?
Part of the work discussed [here](https://meta.discourse.org/t/gain-understanding-of-file-uploads-usage/104994), and implemented a first spike [here](https://github.com/discourse/discourse/pull/6809), I am trying to expose a single generic filter selector per report.
## How does this work?
We basically expose a simple, single generic filter that is computed and displayed based on backend values passed into the report.
This would be a simple contract between the frontend and the backend.
**Backend changes:** we simply need to return a list of dropdown / select options, and enable the report's newly introduced `custom_filtering` property.
For example, for our [Top Uploads](https://github.com/discourse/discourse/pull/6809/files#diff-3f97cbb8726f3310e0b0c386dbe89e22R1423) report, it can look like this on the backend:
```ruby
report.custom_filtering = true
report.custom_filter_options = [{ id: "any", name: "Any" }, { id: "jpg", name: "JPEG" } ]
```
In our javascript report HTTP call, it will look like:
```js
{
"custom_filtering": true,
"custom_filter_options": [
{
"id": "any",
"name": "Any"
},
{
"id": "jpg",
"name": "JPG"
}
]
}
```
**Frontend changes:** We introduced a generic `filter` param and a `combo-box` which hooks up into the existing framework for fetching a report.
This works alright, with the limitation of being a single custom filter per report. If we wanted to add, for an instance a `filesize filter`, this will not work for us. _I went through with this approach because it is hard to predict and build abstractions for requirements or problems we don't have yet, or might not have._
## How does it look like?
![a1ktg1odde](https://user-images.githubusercontent.com/45508821/50485875-f17edb80-09ee-11e9-92dd-1454ab041fbb.gif)
## More on the bigger picture
The major concern here I have is the solution I introduced might serve the `think small` version of the reporting work, but I don't think it serves the `think big`, I will try to shed some light into why.
Within the current design, It is hard to maintain QueryParams for dynamically generated params (based on the idea of introducing more than one custom filter per report).
To allow ourselves to have more than one generic filter, we will need to:
a. Use the Route's model to retrieve the report's payload (we are now dependent on changes of the QueryParams via computed properties)
b. After retrieving the payload, we can use the `setupController` to define our dynamic QueryParams based on the custom filters definitions we received from the backend
c. Load a custom filter specific Ember component based on the definitions we received from the backend
2019-03-15 20:15:38 +08:00
|
|
|
}
|
|
|
|
|
2019-04-26 18:17:10 +08:00
|
|
|
this.attrs.onRefresh({
|
|
|
|
type: this.get("model.type"),
|
2019-05-27 16:15:39 +08:00
|
|
|
startDate: this.startDate,
|
|
|
|
endDate: this.endDate,
|
2019-04-26 18:17:10 +08:00
|
|
|
filters: customFilters
|
FEATURE: Exposing a way to add a generic report filter (#6816)
* FEATURE: Exposing a way to add a generic report filter
## Why do we need this change?
Part of the work discussed [here](https://meta.discourse.org/t/gain-understanding-of-file-uploads-usage/104994), and implemented a first spike [here](https://github.com/discourse/discourse/pull/6809), I am trying to expose a single generic filter selector per report.
## How does this work?
We basically expose a simple, single generic filter that is computed and displayed based on backend values passed into the report.
This would be a simple contract between the frontend and the backend.
**Backend changes:** we simply need to return a list of dropdown / select options, and enable the report's newly introduced `custom_filtering` property.
For example, for our [Top Uploads](https://github.com/discourse/discourse/pull/6809/files#diff-3f97cbb8726f3310e0b0c386dbe89e22R1423) report, it can look like this on the backend:
```ruby
report.custom_filtering = true
report.custom_filter_options = [{ id: "any", name: "Any" }, { id: "jpg", name: "JPEG" } ]
```
In our javascript report HTTP call, it will look like:
```js
{
"custom_filtering": true,
"custom_filter_options": [
{
"id": "any",
"name": "Any"
},
{
"id": "jpg",
"name": "JPG"
}
]
}
```
**Frontend changes:** We introduced a generic `filter` param and a `combo-box` which hooks up into the existing framework for fetching a report.
This works alright, with the limitation of being a single custom filter per report. If we wanted to add, for an instance a `filesize filter`, this will not work for us. _I went through with this approach because it is hard to predict and build abstractions for requirements or problems we don't have yet, or might not have._
## How does it look like?
![a1ktg1odde](https://user-images.githubusercontent.com/45508821/50485875-f17edb80-09ee-11e9-92dd-1454ab041fbb.gif)
## More on the bigger picture
The major concern here I have is the solution I introduced might serve the `think small` version of the reporting work, but I don't think it serves the `think big`, I will try to shed some light into why.
Within the current design, It is hard to maintain QueryParams for dynamically generated params (based on the idea of introducing more than one custom filter per report).
To allow ourselves to have more than one generic filter, we will need to:
a. Use the Route's model to retrieve the report's payload (we are now dependent on changes of the QueryParams via computed properties)
b. After retrieving the payload, we can use the `setupController` to define our dynamic QueryParams based on the custom filters definitions we received from the backend
c. Load a custom filter specific Ember component based on the definitions we received from the backend
2019-03-15 20:15:38 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-26 01:16:35 +08:00
|
|
|
refreshReport() {
|
|
|
|
this.attrs.onRefresh({
|
2019-05-02 16:03:12 +08:00
|
|
|
type: this.get("model.type"),
|
2019-05-27 16:15:39 +08:00
|
|
|
startDate: this.startDate,
|
|
|
|
endDate: this.endDate,
|
2019-04-26 18:17:10 +08:00
|
|
|
filters: this.get("filters.customFilters")
|
2018-07-26 01:16:35 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
exportCsv() {
|
2019-05-01 12:57:19 +08:00
|
|
|
const customFilters = this.get("filters.customFilters") || {};
|
2019-04-26 18:17:10 +08:00
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
exportEntity("report", {
|
|
|
|
name: this.get("model.type"),
|
2019-05-27 16:15:39 +08:00
|
|
|
start_date: this.startDate,
|
|
|
|
end_date: this.endDate,
|
2019-04-26 18:17:10 +08:00
|
|
|
category_id: customFilters.category,
|
|
|
|
group_id: customFilters.group
|
2018-07-20 02:33:11 +08:00
|
|
|
}).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) {
|
2019-05-27 16:15:39 +08:00
|
|
|
return r.findBy("type", this.dataSourceName);
|
2018-07-20 02:33:11 +08:00
|
|
|
} else {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.startDate || !this.endDate) {
|
2018-07-20 02:33:11 +08:00
|
|
|
report = sort(filteredReports)[0];
|
|
|
|
} else {
|
2019-05-27 16:15:39 +08:00
|
|
|
const reportKey = this.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
|
|
|
}
|
|
|
|
|
2018-11-12 20:47:24 +08:00
|
|
|
if (report.error === "not_found") {
|
|
|
|
this.set("showFilteringUI", false);
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
this._renderReport(report, this.forcedModes, this.currentMode);
|
2018-07-20 02:33:11 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_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() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2018-08-22 17:25:12 +08:00
|
|
|
this.setProperties({ isLoading: true, rateLimitationString: null });
|
2018-07-20 02:33:11 +08:00
|
|
|
|
2019-10-30 21:48:24 +08:00
|
|
|
next(() => {
|
2018-08-24 21:28:01 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
ReportLoader.enqueue(this.dataSourceName, payload.data, callback);
|
2018-08-24 21:28:01 +08:00
|
|
|
});
|
2018-07-20 02:33:11 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildPayload(facets) {
|
|
|
|
let payload = { data: { cache: true, facets } };
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.startDate) {
|
2018-08-17 22:19:25 +08:00
|
|
|
payload.data.start_date = moment
|
2019-05-27 16:15:39 +08:00
|
|
|
.utc(this.startDate, "YYYY-MM-DD")
|
2018-08-17 22:19:25 +08:00
|
|
|
.toISOString();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.endDate) {
|
2018-08-17 22:19:25 +08:00
|
|
|
payload.data.end_date = moment
|
2019-05-27 16:15:39 +08:00
|
|
|
.utc(this.endDate, "YYYY-MM-DD")
|
2018-08-17 22:19:25 +08:00
|
|
|
.toISOString();
|
2018-07-20 02:33:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("reportOptions.table.limit")) {
|
|
|
|
payload.data.limit = this.get("reportOptions.table.limit");
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:17:10 +08:00
|
|
|
if (this.get("filters.customFilters")) {
|
|
|
|
payload.data.filters = this.get("filters.customFilters");
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:33:11 +08:00
|
|
|
return payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
_buildOptions(mode) {
|
|
|
|
if (mode === "table") {
|
|
|
|
const tableOptions = JSON.parse(JSON.stringify(TABLE_OPTIONS));
|
2019-10-30 03:23:50 +08:00
|
|
|
return EmberObject.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));
|
2019-10-30 03:23:50 +08:00
|
|
|
return EmberObject.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" });
|
|
|
|
|
2019-01-21 22:17:04 +08:00
|
|
|
if (jsonReport.chartData && jsonReport.modes[0] === "stacked_chart") {
|
|
|
|
jsonReport.chartData = jsonReport.chartData.map(chartData => {
|
|
|
|
if (chartData.length > 40) {
|
|
|
|
return {
|
|
|
|
data: collapseWeekly(chartData.data),
|
|
|
|
req: chartData.req,
|
2019-01-22 00:10:10 +08:00
|
|
|
label: chartData.label,
|
2019-01-21 22:17:04 +08:00
|
|
|
color: chartData.color
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return chartData;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (jsonReport.chartData && jsonReport.chartData.length > 40) {
|
2018-07-20 02:33:11 +08:00
|
|
|
jsonReport.chartData = collapseWeekly(
|
|
|
|
jsonReport.chartData,
|
|
|
|
jsonReport.average
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jsonReport.prev_data) {
|
|
|
|
Report.fillMissingDates(jsonReport, {
|
|
|
|
filledField: "prevChartData",
|
|
|
|
dataField: "prev_data",
|
2019-04-26 18:17:10 +08:00
|
|
|
starDate: jsonReport.prev_startDate,
|
|
|
|
endDate: jsonReport.prev_endDate
|
2018-07-20 02:33:11 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (jsonReport.prevChartData && jsonReport.prevChartData.length > 40) {
|
|
|
|
jsonReport.prevChartData = collapseWeekly(
|
|
|
|
jsonReport.prevChartData,
|
|
|
|
jsonReport.average
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Report.create(jsonReport);
|
|
|
|
}
|
|
|
|
});
|