You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2018/06/14 17:38:34 UTC

[GitHub] timifasubaa closed pull request #5118: Adding the MetricsControl to the timeseries_limit_metric field

timifasubaa closed pull request #5118: Adding the MetricsControl to the timeseries_limit_metric field
URL: https://github.com/apache/incubator-superset/pull/5118
 
 
   

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/src/explore/controls.jsx b/superset/assets/src/explore/controls.jsx
index b7327ddd58..ff719c320c 100644
--- a/superset/assets/src/explore/controls.jsx
+++ b/superset/assets/src/explore/controls.jsx
@@ -959,12 +959,14 @@ export const controls = {
   },
 
   timeseries_limit_metric: {
-    type: 'SelectControl',
+    type: 'MetricsControl',
     label: t('Sort By'),
     default: null,
     description: t('Metric used to define the top series'),
     mapStateToProps: state => ({
-      choices: (state.datasource) ? state.datasource.metrics_combo : [],
+      columns: state.datasource ? state.datasource.columns : [],
+      savedMetrics: state.datasource ? state.datasource.metrics : [],
+      datasourceType: state.datasource && state.datasource.type,
     }),
   },
 
diff --git a/superset/assets/src/visualizations/table.js b/superset/assets/src/visualizations/table.js
index 6b8deec2d8..72a326ac65 100644
--- a/superset/assets/src/visualizations/table.js
+++ b/superset/assets/src/visualizations/table.js
@@ -17,7 +17,7 @@ function tableVis(slice, payload) {
   const data = payload.data;
   const fd = slice.formData;
 
-  let metrics = fd.metrics || [];
+  let metrics = fd.metrics.map(m => m.label || m);
   // Add percent metrics
   metrics = metrics.concat((fd.percent_metrics || []).map(m => '%' + m));
   // Removing metrics (aggregates) that are strings
@@ -187,7 +187,7 @@ function tableVis(slice, payload) {
   let sortBy;
   if (fd.timeseries_limit_metric) {
     // Sort by as specified
-    sortBy = fd.timeseries_limit_metric;
+    sortBy = fd.timeseries_limit_metric.label || fd.timeseries_limit_metric;
   } else if (metrics.length > 0) {
     // If not specified, use the first metric from the list
     sortBy = metrics[0];
@@ -195,7 +195,7 @@ function tableVis(slice, payload) {
   if (sortBy) {
     datatable.column(data.columns.indexOf(sortBy)).order(fd.order_desc ? 'desc' : 'asc');
   }
-  if (fd.timeseries_limit_metric && metrics.indexOf(fd.timeseries_limit_metric) < 0) {
+  if (sortBy && metrics.indexOf(sortBy) < 0) {
     // Hiding the sortBy column if not in the metrics list
     datatable.column(data.columns.indexOf(sortBy)).visible(false);
   }
diff --git a/superset/connectors/druid/models.py b/superset/connectors/druid/models.py
index 79b0dad607..e23537fe60 100644
--- a/superset/connectors/druid/models.py
+++ b/superset/connectors/druid/models.py
@@ -1100,6 +1100,18 @@ def _dimensions_to_values(dimensions):
 
         return values
 
+    @staticmethod
+    def sanitize_metric_object(metric):
+        """
+        Update a metric with the correct type if necessary.
+        :param dict metric: The metric to sanitize
+        """
+        if (
+            utils.is_adhoc_metric(metric) and
+            metric['column']['type'].upper() == 'FLOAT'
+        ):
+            metric['column']['type'] = 'DOUBLE'
+
     def run_query(  # noqa / druid
             self,
             groupby, metrics,
@@ -1143,11 +1155,8 @@ def run_query(  # noqa / druid
             LooseVersion(self.cluster.get_druid_version()) < LooseVersion('0.11.0')
         ):
             for metric in metrics:
-                if (
-                    utils.is_adhoc_metric(metric) and
-                    metric['column']['type'].upper() == 'FLOAT'
-                ):
-                    metric['column']['type'] = 'DOUBLE'
+                self.sanitize_metric_object(metric)
+            self.sanitize_metric_object(timeseries_limit_metric)
 
         aggregations, post_aggs = DruidDatasource.metrics_and_post_aggs(
             metrics,
@@ -1203,7 +1212,7 @@ def run_query(  # noqa / druid
             logging.info('Running two-phase topn query for dimension [{}]'.format(dim))
             pre_qry = deepcopy(qry)
             if timeseries_limit_metric:
-                order_by = timeseries_limit_metric
+                order_by = utils.get_metric_name(timeseries_limit_metric)
                 aggs_dict, post_aggs_dict = DruidDatasource.metrics_and_post_aggs(
                     [timeseries_limit_metric],
                     metrics_dict)
@@ -1272,7 +1281,7 @@ def run_query(  # noqa / druid
                     order_by = pre_qry_dims[0]
 
                 if timeseries_limit_metric:
-                    order_by = timeseries_limit_metric
+                    order_by = utils.get_metric_name(timeseries_limit_metric)
                     aggs_dict, post_aggs_dict = DruidDatasource.metrics_and_post_aggs(
                         [timeseries_limit_metric],
                         metrics_dict)
diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py
index 875707f55c..e08053ccd1 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -651,6 +651,8 @@ def get_sqla_query(  # sqla
 
         for col, ascending in orderby:
             direction = asc if ascending else desc
+            if utils.is_adhoc_metric(col):
+                col = self.adhoc_metric_to_sa(col, cols)
             qry = qry.order_by(direction(col))
 
         if row_limit:
@@ -675,8 +677,15 @@ def get_sqla_query(  # sqla
 
                 ob = inner_main_metric_expr
                 if timeseries_limit_metric:
-                    timeseries_limit_metric = metrics_dict.get(timeseries_limit_metric)
-                    ob = timeseries_limit_metric.sqla_col
+                    if utils.is_adhoc_metric(timeseries_limit_metric):
+                        ob = self.adhoc_metric_to_sa(timeseries_limit_metric, cols)
+                    elif timeseries_limit_metric in metrics_dict:
+                        timeseries_limit_metric = metrics_dict.get(
+                            timeseries_limit_metric,
+                        )
+                        ob = timeseries_limit_metric.sqla_col
+                    else:
+                        raise Exception(_("Metric '{}' is not valid".format(m)))
                 direction = desc if order_desc else asc
                 subq = subq.order_by(direction(ob))
                 subq = subq.limit(timeseries_limit)
diff --git a/superset/data/__init__.py b/superset/data/__init__.py
index 4f79be842a..dc681f4f40 100644
--- a/superset/data/__init__.py
+++ b/superset/data/__init__.py
@@ -626,7 +626,8 @@ def load_birth_names():
                     'op': 'in',
                     'val': ['girl'],
                 }],
-                row_limit=50)),
+                row_limit=50,
+                timeseries_limit_metric='sum__num')),
         Slice(
             slice_name="Boys",
             viz_type='table',
@@ -760,6 +761,53 @@ def load_birth_names():
                 },
                 viz_type="big_number_total",
                 granularity_sqla="ds")),
+        Slice(
+            slice_name='Top 10 California Names Timeseries',
+            viz_type='line',
+            datasource_type='table',
+            datasource_id=tbl.id,
+            params=get_slice_json(
+                defaults,
+                metrics=[{
+                    'expressionType': 'SIMPLE',
+                    'column': {
+                        'column_name': 'num_california',
+                        'expression': "CASE WHEN state = 'CA' THEN num ELSE 0 END",
+                    },
+                    'aggregate': 'SUM',
+                    'label': 'SUM(num_california)',
+                }],
+                viz_type='line',
+                granularity_sqla='ds',
+                groupby=['name'],
+                timeseries_limit_metric={
+                    'expressionType': 'SIMPLE',
+                    'column': {
+                        'column_name': 'num_california',
+                        'expression': "CASE WHEN state = 'CA' THEN num ELSE 0 END",
+                    },
+                    'aggregate': 'SUM',
+                    'label': 'SUM(num_california)',
+                },
+                limit='10')),
+        Slice(
+            slice_name="Names Sorted by Num in California",
+            viz_type='table',
+            datasource_type='table',
+            datasource_id=tbl.id,
+            params=get_slice_json(
+                defaults,
+                groupby=['name'],
+                row_limit=50,
+                timeseries_limit_metric={
+                    'expressionType': 'SIMPLE',
+                    'column': {
+                        'column_name': 'num_california',
+                        'expression': "CASE WHEN state = 'CA' THEN num ELSE 0 END",
+                    },
+                    'aggregate': 'SUM',
+                    'label': 'SUM(num_california)',
+                })),
     ]
     for slc in slices:
         merge_slice(slc)
diff --git a/superset/viz.py b/superset/viz.py
index f3db31c247..e7165d317d 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -510,7 +510,8 @@ def query_obj(self):
             order_by_cols = fd.get('order_by_cols') or []
             d['orderby'] = [json.loads(t) for t in order_by_cols]
         elif sort_by:
-            if sort_by not in d['metrics']:
+            sort_by_label = utils.get_metric_name(sort_by)
+            if sort_by_label not in utils.get_metric_names(d['metrics']):
                 d['metrics'] += [sort_by]
             d['orderby'] = [(sort_by, not fd.get('order_desc', True))]
 


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org