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/08/05 20:33:24 UTC

[GitHub] pieter-lazzaro closed pull request #5539: Fix TableViz column order

pieter-lazzaro closed pull request #5539: Fix TableViz column order
URL: https://github.com/apache/incubator-superset/pull/5539
 
 
   

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/viz.py b/superset/viz.py
index 770d7eaff0..16cf9fa9d1 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -10,7 +10,7 @@
 from __future__ import print_function
 from __future__ import unicode_literals
 
-from collections import defaultdict
+from collections import defaultdict, OrderedDict
 import copy
 from datetime import datetime, timedelta
 import hashlib
@@ -521,6 +521,42 @@ def query_obj(self):
         d['is_timeseries'] = self.should_be_timeseries()
         return d
 
+    def get_column_names(self, df):
+        fd = self.form_data
+
+        all_columns = fd.get('all_columns') or []
+
+        if len(all_columns):
+            return list(filter(lambda c: c in df, all_columns))
+
+        columns = OrderedDict()
+
+        groupby_columns = fd.get('groupby') or []
+
+        for column_name in groupby_columns:
+            columns[column_name] = column_name
+
+        if fd.get('include_time'):
+            columns['__timestamp'] = '__timestamp'
+
+        metrics_columns = fd.get('metrics') or []
+
+        for metric in metrics_columns:
+            column_name = self.get_metric_label(metric)
+            columns[column_name] = column_name
+
+        percent_metrics_columns = fd.get('percent_metrics') or []
+        for metric in percent_metrics_columns:
+            column_name = '%' + self.get_metric_label(metric)
+            columns[column_name] = column_name
+
+        timeseries_limit_metric_column = fd.get('timeseries_limit_metric') or None
+        if timeseries_limit_metric_column:
+            column_name = self.get_metric_label(timeseries_limit_metric_column)
+            columns[column_name] = column_name
+
+        return list(filter(lambda c: c in df, columns))
+
     def get_data(self, df):
         fd = self.form_data
         if (
@@ -560,7 +596,7 @@ def get_data(self, df):
         data = self.handle_js_int_overflow(
             dict(
                 records=df.to_dict(orient='records'),
-                columns=list(df.columns),
+                columns=self.get_column_names(df),
             ))
 
         return data
diff --git a/tests/viz_tests.py b/tests/viz_tests.py
index 61539b6681..9281f496fa 100644
--- a/tests/viz_tests.py
+++ b/tests/viz_tests.py
@@ -121,8 +121,89 @@ def test_cache_timeout(self):
 
 
 class TableVizTestCase(unittest.TestCase):
+    def test_get_data_maintains_column_order_all_columns(self):
+        form_data = {
+            'all_columns': ['groupB'],
+            'percent_metrics': [],
+            'metrics': [],
+        }
+        datasource = Mock()
+        raw = {}
+        raw['SUM(value1)'] = [15, 20, 25, 40]
+        raw['avg__B'] = [10, 20, 5, 15]
+        raw['avg__C'] = [11, 22, 33, 44]
+        raw['count'] = [6, 7, 8, 9]
+        raw['groupA'] = ['A', 'B', 'C', 'C']
+        raw['groupB'] = ['x', 'x', 'y', 'z']
+        df = pd.DataFrame(raw)
+        test_viz = viz.TableViz(datasource, form_data)
+        data = test_viz.get_data(df)
+        # Check method correctly transforms data and computes percents
+        self.assertEqual(list(['groupB']), data['columns'])
+
+    def test_get_data_maintains_column_order_with_timestamp(self):
+        form_data = {
+            'groupby': ['groupA'],
+            'include_time': True,
+            'granularity_sqla': 'ds',
+            'time_grain_sqla': 'P1D',
+            'metrics': ['count', 'avg__C'],
+        }
+
+        datasource = Mock()
+        raw = {}
+        raw['SUM(value1)'] = [15, 20, 25, 40]
+        raw['avg__B'] = [10, 20, 5, 15]
+        raw['avg__C'] = [11, 22, 33, 44]
+        raw['count'] = [6, 7, 8, 9]
+        raw['__timestamp'] = [6, 7, 8, 9]
+        raw['groupA'] = ['A', 'B', 'C', 'C']
+        raw['groupB'] = ['x', 'x', 'y', 'z']
+        df = pd.DataFrame(raw)
+        test_viz = viz.TableViz(datasource, form_data)
+        data = test_viz.get_data(df)
+        # Check method correctly transforms data and computes percents
+        self.assertEqual(list([
+            'groupA', '__timestamp', 'count', 'avg__C',
+        ]), data['columns'])
+
+    def test_get_data_maintains_column_order_grouped(self):
+        form_data = {
+            'groupby': ['groupA', 'groupB'],
+            'percent_metrics': [{
+                'expressionType': 'SIMPLE',
+                'aggregate': 'SUM',
+                'label': 'SUM(value1)',
+                'column': {'column_name': 'value1', 'type': 'DOUBLE'},
+            }, 'avg__B'],
+            'metrics': [{
+                'expressionType': 'SIMPLE',
+                'aggregate': 'SUM',
+                'label': 'SUM(value1)',
+                'column': {'column_name': 'value1', 'type': 'DOUBLE'},
+            }, 'count', 'avg__C'],
+        }
+        datasource = Mock()
+        raw = {}
+        raw['SUM(value1)'] = [15, 20, 25, 40]
+        raw['avg__B'] = [10, 20, 5, 15]
+        raw['avg__C'] = [11, 22, 33, 44]
+        raw['count'] = [6, 7, 8, 9]
+        raw['groupA'] = ['A', 'B', 'C', 'C']
+        raw['groupB'] = ['x', 'x', 'y', 'z']
+        df = pd.DataFrame(raw)
+        test_viz = viz.TableViz(datasource, form_data)
+        data = test_viz.get_data(df)
+        # Check method correctly transforms data and computes percents
+        self.assertEqual(list([
+            'groupA', 'groupB',
+            'SUM(value1)', 'count', 'avg__C',
+            '%SUM(value1)', '%avg__B',
+        ]), data['columns'])
+
     def test_get_data_applies_percentage(self):
         form_data = {
+            'groupby': ['groupA', 'groupB'],
             'percent_metrics': [{
                 'expressionType': 'SIMPLE',
                 'aggregate': 'SUM',


 

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