Skip to content

Commit

Permalink
[hotfix] grouping by grain, filtering on raw expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Mar 17, 2016
1 parent 73daf2f commit 7cdb23f
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,6 @@ def query(
raise Exception(
"Datetime column not provided as part table configuration "
"and is required by this type of chart")
if granularity:
dttm_expr = cols[granularity].expression or granularity

# Transforming time grain into an expression based on configuration
time_grain_sqla = extras.get('time_grain_sqla')
if time_grain_sqla:
udf = self.database.grains_dict().get(time_grain_sqla, '{col}')
dttm_expr = udf.function.format(col=dttm_expr)
timestamp = literal_column(dttm_expr).label('timestamp')

metrics_exprs = [
literal_column(m.expression).label(m.metric_name)
Expand Down Expand Up @@ -476,17 +467,23 @@ def query(
select_exprs.append(s)
metrics_exprs = []

if is_timeseries:
select_exprs += [timestamp]
groupby_exprs += [timestamp]
if granularity:
dttm_expr = cols[granularity].expression or granularity
timestamp = literal_column(dttm_expr).label('timestamp')

select_exprs += metrics_exprs
qry = select(select_exprs)
from_clause = table(self.table_name)
if not columns:
qry = qry.group_by(*groupby_exprs)
# Transforming time grain into an expression based on configuration
time_grain_sqla = extras.get('time_grain_sqla')
if time_grain_sqla:
udf = self.database.grains_dict().get(time_grain_sqla, '{col}')
timestamp_grain = literal_column(
udf.function.format(col=dttm_expr)).label('timestamp')
else:
timestamp_grain = timestamp

if is_timeseries:
select_exprs += [timestamp_grain]
groupby_exprs += [timestamp_grain]

if granularity:
tf = '%Y-%m-%d %H:%M:%S.%f'
time_filter = [
timestamp >= from_dttm.strftime(tf),
Expand All @@ -497,6 +494,13 @@ def query(
inner_time_filter[0] = timestamp >= inner_from_dttm.strftime(tf)
if inner_to_dttm:
inner_time_filter[1] = timestamp <= inner_to_dttm.strftime(tf)

select_exprs += metrics_exprs
qry = select(select_exprs)
from_clause = table(self.table_name)
if not columns:
qry = qry.group_by(*groupby_exprs)

where_clause_and = []
having_clause_and = []
for col, op, eq in filter:
Expand Down

0 comments on commit 7cdb23f

Please sign in to comment.