From 3c0e85e2c074755eb2883dc75b92aad83a3f0261 Mon Sep 17 00:00:00 2001 From: fabianmenges Date: Tue, 12 Sep 2017 12:48:43 -0400 Subject: [PATCH] Adding order_desc flag to explore endpoint (#3439) --- superset/connectors/druid/models.py | 13 +++++++------ superset/connectors/sqla/models.py | 9 ++++++--- superset/viz.py | 4 ++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/superset/connectors/druid/models.py b/superset/connectors/druid/models.py index 6340331ba785a..4aadf06e4d19a 100644 --- a/superset/connectors/druid/models.py +++ b/superset/connectors/druid/models.py @@ -812,7 +812,8 @@ def run_query( # noqa / druid orderby=None, extras=None, # noqa select=None, # noqa - columns=None, phase=2, client=None, form_data=None): + columns=None, phase=2, client=None, form_data=None, + order_desc=True): """Runs a query against Druid and returns a dataframe. """ # TODO refactor into using a TBD Query object @@ -882,12 +883,12 @@ def run_query( # noqa / druid having_filters = self.get_having_filters(extras.get('having_druid')) if having_filters: qry['having'] = having_filters - + order_direction = "descending" if order_desc else "ascending" orig_filters = filters if len(groupby) == 0 and not having_filters: del qry['dimensions'] client.timeseries(**qry) - if not having_filters and len(groupby) == 1: + if not having_filters and len(groupby) == 1 and order_desc: qry['threshold'] = timeseries_limit or 1000 if row_limit and granularity == 'all': qry['threshold'] = row_limit @@ -895,7 +896,7 @@ def run_query( # noqa / druid del qry['dimensions'] qry['metric'] = list(qry['aggregations'].keys())[0] client.topn(**qry) - elif len(groupby) > 1 or having_filters: + elif len(groupby) > 1 or having_filters or not order_desc: # If grouping on multiple fields or using a having filter # we have to force a groupby query if timeseries_limit and is_timeseries: @@ -913,7 +914,7 @@ def run_query( # noqa / druid inner_to_dttm.isoformat()), "columns": [{ "dimension": order_by, - "direction": "descending", + "direction": order_direction, }], } client.groupby(**pre_qry) @@ -956,7 +957,7 @@ def run_query( # noqa / druid "columns": [{ "dimension": ( metrics[0] if metrics else self.metrics[0]), - "direction": "descending", + "direction": order_direction, }], } client.groupby(**qry) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 96ef575986bfd..03c8c54a6c92f 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -364,7 +364,8 @@ def get_sqla_query( # sqla orderby=None, extras=None, columns=None, - form_data=None): + form_data=None, + order_desc=True): """Querying any sqla table from this common interface""" template_kwargs = { 'from_dttm': from_dttm, @@ -512,7 +513,8 @@ def get_sqla_query( # sqla qry = qry.where(and_(*where_clause_and)) qry = qry.having(and_(*having_clause_and)) if groupby: - qry = qry.order_by(desc(main_metric_expr)) + direction = desc if order_desc else asc + qry = qry.order_by(direction(main_metric_expr)) elif orderby: for col, ascending in orderby: direction = asc if ascending else desc @@ -539,7 +541,8 @@ def get_sqla_query( # sqla ob = inner_main_metric_expr if timeseries_limit_metric_expr is not None: ob = timeseries_limit_metric_expr - subq = subq.order_by(desc(ob)) + direction = desc if order_desc else asc + subq = subq.order_by(direction(ob)) subq = subq.limit(timeseries_limit) on_clause = [] for i, gb in enumerate(groupby): diff --git a/superset/viz.py b/superset/viz.py index 3f973c7ed9bb5..7a2b22ac7f2e7 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -137,6 +137,9 @@ def query_obj(self): row_limit = int( form_data.get("row_limit") or config.get("ROW_LIMIT")) + # default order direction + order_desc = form_data.get("order_desc", True) + # __form and __to are special extra_filters that target time # boundaries. The rest of extra_filters are simple # [column_name in list_of_values]. `__` prefix is there to avoid @@ -194,6 +197,7 @@ def query_obj(self): 'extras': extras, 'timeseries_limit_metric': timeseries_limit_metric, 'form_data': form_data, + 'order_desc': order_desc } return d