Merge pull request #65 from mistercrunch/custom_having

Adding custom HAVING clause
This commit is contained in:
Maxime Beauchemin 2015-11-24 11:51:33 -08:00
commit 5968344613
3 changed files with 9 additions and 2 deletions

View File

@ -155,6 +155,7 @@ class FormFactory(object):
default=default_metric,
choices=datasource.metrics_combo),
'where': TextField('Custom WHERE clause', default=''),
'having': TextField('Custom HAVING clause', default=''),
'compare_lag': TextField('Comparison Period Lag',
description="Based on granularity, number of time periods to compare against"),
'compare_suffix': TextField('Comparison suffix',
@ -287,8 +288,9 @@ class FormFactory(object):
# datasource type specific form elements
if datasource.__class__.__name__ == 'SqlaTable':
QueryForm.field_order += ['where']
QueryForm.field_order += ['where', 'having']
setattr(QueryForm, 'where', px_form_fields['where'])
setattr(QueryForm, 'having', px_form_fields['having'])
if 'granularity' in viz.form_fields:
setattr(

View File

@ -435,6 +435,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
if inner_to_dttm:
inner_time_filter[1] = timestamp <= inner_to_dttm.isoformat()
where_clause_and = []
having_clause_and = []
for col, op, eq in filter:
col_obj = cols[col]
if op in ('in', 'not in'):
@ -449,7 +450,10 @@ class SqlaTable(Model, Queryable, AuditMixinNullable):
where_clause_and.append(cond)
if extras and 'where' in extras:
where_clause_and += [text(extras['where'])]
if extras and 'having' in extras:
having_clause_and += [text(extras['having'])]
qry = qry.where(and_(*(time_filter + where_clause_and)))
qry = qry.having(and_(*having_clause_and))
qry = qry.order_by(desc(main_metric_expr))
qry = qry.limit(row_limit)

View File

@ -159,7 +159,8 @@ class BaseViz(object):
# extras are used to query elements specific to a datasource type
# for instance the extra where clause that applies only to Tables
extras = {
'where': form_data.get("where", '')
'where': form_data.get("where", ''),
'having': form_data.get("having", ''),
}
d = {
'granularity': granularity,