FEATURE: Exposing a way to add a generic report filter (#6816)

approved

#1

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, and implemented a first spike here, 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 report, it can look like this on the backend:

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:

{
  "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

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 7f75af9..28a7e3d 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 541d77e..d1c186e 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 e04b16f..8c77318 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 53e9d90..aff881d 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 14191e9..c06e8b1 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 @@
           </div>
         {{/if}}
 
+        {{#each filterOptions as |filterOption|}}
+          <div class="control">
+            <div class="input">
+              {{combo-box content=filterOption.choices
+                          filterable=true
+                          allowAny=true
+                          value=filterOption.selected
+                          onSelect=(action "filter" filterOption.id)}}
+            </div>
+          </div>
+        {{/each}}
+
         {{#if showExport}}
           <div class="control">
             <div class="input">
diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rb
index 1ec347f..fa5a6e3 100644
--- a/app/controllers/admin/reports_controller.rb
+++ b/app/controllers/admin/reports_controller.rb
@@ -113,11 +113,17 @@ class Admin::ReportsController < Admin::AdminController
       limit = report_params[:limit].to_i
     end
 
+    filter = nil
+    if report_params.has_key?(:filter)
+      filter = report_params[:filter]
+    end
+
     {
       start_date: start_date,
       end_date: end_date,
       category_id: category_id,
       group_id: group_id,
+      filter: filter,
       facets: facets,

[... diff too long, it was truncated ...]

GitHub sha: f8480ed9


Approved #2