From f8480ed911bb41ada4fc1f09d3b7cbbeb3a0833e Mon Sep 17 00:00:00 2001 From: Tarek Khalil <45508821+khalilovcmded@users.noreply.github.com> Date: Fri, 15 Mar 2019 12:15:38 +0000 Subject: [PATCH] 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?  ## 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 --- .../components/admin-report-table.js.es6 | 4 +- .../admin/components/admin-report.js.es6 | 47 ++++++++++++++++++- .../controllers/admin-reports-show.js.es6 | 8 ++-- .../javascripts/admin/models/report.js.es6 | 8 +++- .../templates/components/admin-report.hbs | 12 +++++ app/controllers/admin/reports_controller.rb | 6 +++ app/models/report.rb | 42 +++++++++++++---- config/locales/client.en.yml | 1 + 8 files changed, 111 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/admin/components/admin-report-table.js.es6 b/app/assets/javascripts/admin/components/admin-report-table.js.es6 index 7f75af90273..28a7e3d84d3 100644 --- a/app/assets/javascripts/admin/components/admin-report-table.js.es6 +++ b/app/assets/javascripts/admin/components/admin-report-table.js.es6 @@ -79,8 +79,8 @@ export default Ember.Component.extend({ if (sortLabel) { const compare = (label, direction) => { return (a, b) => { - let aValue = label.compute(a).value; - let bValue = label.compute(b).value; + const aValue = label.compute(a, { useSortProperty: true }).value; + const bValue = label.compute(b, { useSortProperty: true }).value; const result = aValue < bValue ? -1 : aValue > bValue ? 1 : 0; return result * direction; }; diff --git a/app/assets/javascripts/admin/components/admin-report.js.es6 b/app/assets/javascripts/admin/components/admin-report.js.es6 index 541d77eb681..d1c186effec 100644 --- a/app/assets/javascripts/admin/components/admin-report.js.es6 +++ b/app/assets/javascripts/admin/components/admin-report.js.es6 @@ -56,6 +56,7 @@ export default Ember.Component.extend({ endDate: null, category: null, groupId: null, + filter: null, showTrend: false, showHeader: true, showTitle: true, @@ -85,6 +86,7 @@ export default Ember.Component.extend({ this.setProperties({ category: Category.findById(state.categoryId), groupId: state.groupId, + filter: state.filter, startDate: state.startDate, endDate: state.endDate }); @@ -174,6 +176,18 @@ export default Ember.Component.extend({ return `admin-report-${currentMode}`; }, + @computed("model.filter_options") + filterOptions(options) { + if (options) { + return options.map(option => { + if (option.allowAny) { + option.choices.unshift(I18n.t("admin.dashboard.report_filter_any")); + } + return option; + }); + } + }, + @computed("startDate") normalizedStartDate(startDate) { return startDate && typeof startDate.isValid === "function" @@ -202,10 +216,11 @@ export default Ember.Component.extend({ "dataSourceName", "categoryId", "groupId", + "filter", "normalizedStartDate", "normalizedEndDate" ) - reportKey(dataSourceName, categoryId, groupId, startDate, endDate) { + reportKey(dataSourceName, categoryId, groupId, filter, startDate, endDate) { if (!dataSourceName || !startDate || !endDate) return null; let reportKey = "reports:"; @@ -215,6 +230,7 @@ export default Ember.Component.extend({ startDate.replace(/-/g, ""), endDate.replace(/-/g, ""), groupId, + filter, "[:prev_period]", this.get("reportOptions.table.limit"), SCHEMA_VERSION @@ -227,10 +243,35 @@ export default Ember.Component.extend({ }, actions: { + filter(filterOptionId, value) { + let params = []; + let paramPairs = {}; + let newParams = []; + + if (this.get("filter")) { + const filter = this.get("filter").slice(1, -1); + params = filter.split("&") || []; + params.map(p => { + const pair = p.split("="); + paramPairs[pair[0]] = pair[1]; + }); + } + + paramPairs[filterOptionId] = value; + Object.keys(paramPairs).forEach(key => { + if (paramPairs[key] !== I18n.t("admin.dashboard.report_filter_any")) { + newParams.push(`${key}=${paramPairs[key]}`); + } + }); + + this.set("filter", `[${newParams.join("&")}]`); + }, + refreshReport() { this.attrs.onRefresh({ categoryId: this.get("categoryId"), groupId: this.get("groupId"), + filter: this.get("filter"), startDate: this.get("startDate"), endDate: this.get("endDate") }); @@ -366,6 +407,10 @@ export default Ember.Component.extend({ payload.data.category_id = this.get("categoryId"); } + if (this.get("filter") && this.get("filter") !== "all") { + payload.data.filter = this.get("filter"); + } + if (this.get("reportOptions.table.limit")) { payload.data.limit = this.get("reportOptions.table.limit"); } diff --git a/app/assets/javascripts/admin/controllers/admin-reports-show.js.es6 b/app/assets/javascripts/admin/controllers/admin-reports-show.js.es6 index e04b16f6a75..8c773181ed9 100644 --- a/app/assets/javascripts/admin/controllers/admin-reports-show.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-reports-show.js.es6 @@ -1,7 +1,7 @@ import computed from "ember-addons/ember-computed-decorators"; export default Ember.Controller.extend({ - queryParams: ["start_date", "end_date", "category_id", "group_id"], + queryParams: ["start_date", "end_date", "category_id", "group_id", "filter"], @computed("model.type") reportOptions(type) { @@ -14,11 +14,12 @@ export default Ember.Controller.extend({ return options; }, - @computed("category_id", "group_id", "start_date", "end_date") - filters(categoryId, groupId, startDate, endDate) { + @computed("category_id", "group_id", "start_date", "end_date", "filter") + filters(categoryId, groupId, startDate, endDate, filter) { return { categoryId, groupId, + filter, startDate, endDate }; @@ -28,6 +29,7 @@ export default Ember.Controller.extend({ onParamsChange(params) { this.setProperties({ start_date: params.startDate, + filter: params.filter, category_id: params.categoryId, group_id: params.groupId, end_date: params.endDate diff --git a/app/assets/javascripts/admin/models/report.js.es6 b/app/assets/javascripts/admin/models/report.js.es6 index 53e9d90a207..aff881dd2d9 100644 --- a/app/assets/javascripts/admin/models/report.js.es6 +++ b/app/assets/javascripts/admin/models/report.js.es6 @@ -264,7 +264,13 @@ const Report = Discourse.Model.extend({ mainProperty, type, compute: (row, opts = {}) => { - const value = row[mainProperty]; + let value = null; + + if (opts.useSortProperty) { + value = row[label.sort_property || mainProperty]; + } else { + value = row[mainProperty]; + } if (type === "user") return this._userLabel(label.properties, row); if (type === "post") return this._postLabel(label.properties, row); diff --git a/app/assets/javascripts/admin/templates/components/admin-report.hbs b/app/assets/javascripts/admin/templates/components/admin-report.hbs index 14191e90234..c06e8b11aba 100644 --- a/app/assets/javascripts/admin/templates/components/admin-report.hbs +++ b/app/assets/javascripts/admin/templates/components/admin-report.hbs @@ -173,6 +173,18 @@ {{/if}} + {{#each filterOptions as |filterOption|}} +