You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2018/05/20 16:11:07 UTC

[incubator-superset] branch master updated: [bugfix] Fix ZeroDivisionError and get metrics label with percent metrics (#5026)

This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f66dae  [bugfix] Fix ZeroDivisionError and get metrics label with percent metrics (#5026)
9f66dae is described below

commit 9f66dae328b70b277bfc0e1f47c3f63aff541ae4
Author: Yongjie Zhao <yo...@gmail.com>
AuthorDate: Mon May 21 00:10:57 2018 +0800

    [bugfix] Fix ZeroDivisionError and get metrics label with percent metrics (#5026)
    
    * Fix percent_metrics ZeroDivisionError and can not get metrics with label issue
    
    * convert iterator to list
    
    * get percentage metrics with get_metric_label method
    
    * Adding tests case for expression type metrics
    
    * Simplify expression
---
 superset/viz.py    |  8 ++++++--
 tests/viz_tests.py | 36 +++++++++++++++++++++++-------------
 2 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/superset/viz.py b/superset/viz.py
index 38e5680..39d3411 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -573,6 +573,7 @@ class TableViz(BaseViz):
 
         # Sum up and compute percentages for all percent metrics
         percent_metrics = fd.get('percent_metrics') or []
+        percent_metrics = [self.get_metric_label(m) for m in percent_metrics]
 
         if len(percent_metrics):
             percent_metrics = list(filter(lambda m: m in df, percent_metrics))
@@ -581,15 +582,18 @@ class TableViz(BaseViz):
                 for m in percent_metrics
             }
             metric_percents = {
-                m: list(map(lambda a: a / metric_sums[m], df[m]))
+                m: list(map(
+                    lambda a: None if metric_sums[m] == 0 else a / metric_sums[m], df[m]))
                 for m in percent_metrics
             }
             for m in percent_metrics:
                 m_name = '%' + m
                 df[m_name] = pd.Series(metric_percents[m], name=m_name)
             # Remove metrics that are not in the main metrics list
+            metrics = fd.get('metrics', [])
+            metrics = [self.get_metric_label(m) for m in metrics]
             for m in filter(
-                lambda m: m not in fd['metrics'] and m in df.columns,
+                lambda m: m not in metrics and m in df.columns,
                 percent_metrics,
             ):
                 del df[m]
diff --git a/tests/viz_tests.py b/tests/viz_tests.py
index fb56581..30c37c8 100644
--- a/tests/viz_tests.py
+++ b/tests/viz_tests.py
@@ -120,12 +120,22 @@ class BaseVizTestCase(unittest.TestCase):
 class TableVizTestCase(unittest.TestCase):
     def test_get_data_applies_percentage(self):
         form_data = {
-            'percent_metrics': ['sum__A', 'avg__B'],
-            'metrics': ['sum__A', 'count', 'avg__C'],
+            '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__A'] = [15, 20, 25, 40]
+        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]
@@ -137,29 +147,29 @@ class TableVizTestCase(unittest.TestCase):
         # Check method correctly transforms data and computes percents
         self.assertEqual(set([
             'groupA', 'groupB', 'count',
-            'sum__A', 'avg__C',
-            '%sum__A', '%avg__B',
+            'SUM(value1)', 'avg__C',
+            '%SUM(value1)', '%avg__B',
         ]), set(data['columns']))
         expected = [
             {
                 'groupA': 'A', 'groupB': 'x',
-                'count': 6, 'sum__A': 15, 'avg__C': 11,
-                '%sum__A': 0.15, '%avg__B': 0.2,
+                'count': 6, 'SUM(value1)': 15, 'avg__C': 11,
+                '%SUM(value1)': 0.15, '%avg__B': 0.2,
             },
             {
                 'groupA': 'B', 'groupB': 'x',
-                'count': 7, 'sum__A': 20, 'avg__C': 22,
-                '%sum__A': 0.2, '%avg__B': 0.4,
+                'count': 7, 'SUM(value1)': 20, 'avg__C': 22,
+                '%SUM(value1)': 0.2, '%avg__B': 0.4,
             },
             {
                 'groupA': 'C', 'groupB': 'y',
-                'count': 8, 'sum__A': 25, 'avg__C': 33,
-                '%sum__A': 0.25, '%avg__B': 0.1,
+                'count': 8, 'SUM(value1)': 25, 'avg__C': 33,
+                '%SUM(value1)': 0.25, '%avg__B': 0.1,
             },
             {
                 'groupA': 'C', 'groupB': 'z',
-                'count': 9, 'sum__A': 40, 'avg__C': 44,
-                '%sum__A': 0.40, '%avg__B': 0.3,
+                'count': 9, 'SUM(value1)': 40, 'avg__C': 44,
+                '%SUM(value1)': 0.40, '%avg__B': 0.3,
             },
         ]
         self.assertEqual(expected, data['records'])

-- 
To stop receiving notification emails like this one, please contact
maximebeauchemin@apache.org.