mistercrunch closed pull request #3285: [Add] add optional selection for filter 
order
URL: https://github.com/apache/incubator-superset/pull/3285
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/assets/backendSync.json b/superset/assets/backendSync.json
index 71e7130328..5b9a538e68 100644
--- a/superset/assets/backendSync.json
+++ b/superset/assets/backendSync.json
@@ -2006,6 +2006,13 @@
       "default": false,
       "description": "Whether to include a time filter"
     },
+    "filter_order": {
+      "type": "SelectControl",
+      "multi": true,
+      "label": "Ordering by optional value",
+      "default": [],
+      "description": "How to order the list of values for the filter. When 
does't select for one filter, it will order the optional value by metric."
+    },
     "show_datatable": {
       "type": "CheckboxControl",
       "label": "Data Table",
@@ -2371,4 +2378,4 @@
       "description": "The number of seconds before expiring the cache"
     }
   }
-}
\ No newline at end of file
+}
diff --git a/superset/assets/javascripts/explore/stores/controls.jsx 
b/superset/assets/javascripts/explore/stores/controls.jsx
index 3d33873c3c..bcc3c34367 100644
--- a/superset/assets/javascripts/explore/stores/controls.jsx
+++ b/superset/assets/javascripts/explore/stores/controls.jsx
@@ -955,6 +955,18 @@ export const controls = {
     description: 'Whether to include a time filter',
   },
 
+  filter_order: {
+    type: 'SelectControl',
+    multi: true,
+    label: 'Ordering by optional value',
+    default: [],
+    mapStateToProps: state => ({
+      choices: (state.datasource) ? state.datasource.order_by_choices : [],
+    }),
+    description: 'How to order the list of values for the filter. ' +
+    "When does't select for one filter, it will order the optional value by 
metric.",
+  },
+
   show_datatable: {
     type: 'CheckboxControl',
     label: 'Data Table',
diff --git a/superset/assets/javascripts/explore/stores/visTypes.js 
b/superset/assets/javascripts/explore/stores/visTypes.js
index e937b036ab..7df3f8ffb4 100644
--- a/superset/assets/javascripts/explore/stores/visTypes.js
+++ b/superset/assets/javascripts/explore/stores/visTypes.js
@@ -745,6 +745,7 @@ export const visTypes = {
           ['date_filter', 'instant_filtering'],
           ['groupby'],
           ['metric'],
+          ['filter_order'],
         ],
       },
     ],
diff --git a/superset/connectors/sqla/models.py 
b/superset/connectors/sqla/models.py
index 6dccdf874a..97738fa53b 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -510,12 +510,12 @@ def get_sqla_query(  # sqla
         else:
             qry = qry.where(and_(*where_clause_and))
         qry = qry.having(and_(*having_clause_and))
-        if groupby:
-            qry = qry.order_by(desc(main_metric_expr))
-        elif orderby:
+        if orderby:
             for col, ascending in orderby:
                 direction = asc if ascending else desc
                 qry = qry.order_by(direction(col))
+        elif groupby:
+            qry = qry.order_by(desc(main_metric_expr))
 
         if row_limit:
             qry = qry.limit(row_limit)
diff --git a/superset/viz.py b/superset/viz.py
index 100d8bbd6f..26718f8cf6 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -1384,8 +1384,11 @@ def get_data(self, df):
         qry = self.query_obj()
         filters = [g for g in self.form_data['groupby']]
         d = {}
+        filter_order = self.form_data.get('filter_order') or []
+        order = dict(json.loads(o) for o in filter_order)
         for flt in filters:
             qry['groupby'] = [flt]
+            qry['orderby'] = [(flt, order[flt])] if flt in order else []
             df = super(FilterBoxViz, self).get_df(qry)
             d[flt] = [{
                 'id': row[0],


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to