Skip to content

Commit

Permalink
Merge pull request apache#48 from michellethomas/mt_cp_adhoc_metric_b…
Browse files Browse the repository at this point in the history
…ugfixes

cherry-pick adhoc metric bugfixes
  • Loading branch information
michellethomas authored May 30, 2018
2 parents 2493057 + cd3f2a0 commit b811851
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
26 changes: 15 additions & 11 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def get_data(self, df):

# 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))
Expand All @@ -543,15 +544,18 @@ def get_data(self, df):
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]
Expand Down Expand Up @@ -989,7 +993,7 @@ def as_floats(field):

def get_data(self, df):
df = df.fillna(0)
df['metric'] = df[[self.metric]]
df['metric'] = df[[self.get_metric_label(self.metric)]]
values = df['metric'].values
return {
'measures': values.tolist(),
Expand Down Expand Up @@ -1291,8 +1295,8 @@ def get_data(self, df):
if self.form_data.get('granularity') == 'all':
raise Exception(_('Pick a time granularity for your time series'))

metric = fd.get('metric')
metric_2 = fd.get('metric_2')
metric = self.get_metric_label(fd.get('metric'))
metric_2 = self.get_metric_label(fd.get('metric_2'))
df = df.pivot_table(
index=DTTM_ALIAS,
values=[metric, metric_2])
Expand Down Expand Up @@ -1343,7 +1347,7 @@ def get_data(self, df):
df = df.pivot_table(
index=DTTM_ALIAS,
columns='series',
values=fd.get('metric'))
values=self.get_metric_label(fd.get('metric')))
chart_data = self.to_series(df)
for serie in chart_data:
serie['rank'] = rank_lookup[serie['key']]
Expand Down Expand Up @@ -1511,8 +1515,8 @@ class SunburstViz(BaseViz):
def get_data(self, df):
fd = self.form_data
cols = fd.get('groupby')
metric = fd.get('metric')
secondary_metric = fd.get('secondary_metric')
metric = self.get_metric_label(fd.get('metric'))
secondary_metric = self.get_metric_label(fd.get('secondary_metric'))
if metric == secondary_metric or secondary_metric is None:
df.columns = cols + ['m1']
df['m2'] = df['m1']
Expand Down Expand Up @@ -1611,7 +1615,7 @@ def query_obj(self):
qry = super(ChordViz, self).query_obj()
fd = self.form_data
qry['groupby'] = [fd.get('groupby'), fd.get('columns')]
qry['metrics'] = [fd.get('metric')]
qry['metrics'] = [self.get_metric_label(fd.get('metric'))]
return qry

def get_data(self, df):
Expand Down Expand Up @@ -1679,8 +1683,8 @@ def get_data(self, df):
from superset.data import countries
fd = self.form_data
cols = [fd.get('entity')]
metric = fd.get('metric')
secondary_metric = fd.get('secondary_metric')
metric = self.get_metric_label(fd.get('metric'))
secondary_metric = self.get_metric_label(fd.get('secondary_metric'))
if metric == secondary_metric:
ndf = df[cols]
# df[metric] will be a DataFrame
Expand Down
36 changes: 23 additions & 13 deletions tests/viz_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,22 @@ def test_cache_timeout(self):
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]
Expand All @@ -137,29 +147,29 @@ def test_get_data_applies_percentage(self):
# 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'])
Expand Down

0 comments on commit b811851

Please sign in to comment.