Skip to content

Commit

Permalink
Fixing sortby adhoc metrics for table viz
Browse files Browse the repository at this point in the history
  • Loading branch information
michellethomas committed Jun 14, 2018
1 parent 4776828 commit b380a57
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
6 changes: 3 additions & 3 deletions superset/assets/src/visualizations/table.js
Expand Up @@ -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
Expand Down Expand Up @@ -187,15 +187,15 @@ 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];
}
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);
}
Expand Down
24 changes: 14 additions & 10 deletions superset/connectors/druid/models.py
Expand Up @@ -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,
Expand Down Expand Up @@ -1143,16 +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'
if (
utils.is_adhoc_metric(timeseries_limit_metric) and
timeseries_limit_metric['column']['type'].upper() == 'FLOAT'
):
timeseries_limit_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,
Expand Down
2 changes: 2 additions & 0 deletions superset/connectors/sqla/models.py
Expand Up @@ -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:
Expand Down
21 changes: 20 additions & 1 deletion superset/data/__init__.py
Expand Up @@ -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',
Expand Down Expand Up @@ -789,6 +790,24 @@ def load_birth_names():
'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)
Expand Down
3 changes: 2 additions & 1 deletion superset/viz.py
Expand Up @@ -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))]

Expand Down

0 comments on commit b380a57

Please sign in to comment.