Skip to content

Commit

Permalink
SQL working !
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Aug 6, 2015
1 parent b70b270 commit 8ea0157
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
29 changes: 21 additions & 8 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pydruid import client
from pydruid.utils.filters import Dimension, Filter

from copy import deepcopy
from copy import deepcopy, copy
import logging
import json
import requests
Expand Down Expand Up @@ -91,12 +91,18 @@ def query(
from_dttm_iso = from_dttm.isoformat()
to_dttm_iso = to_dttm.isoformat()

if metrics:
main_metric_expr = [m.expression for m in self.metrics if m.metric_name == metrics[0]][0]
else:
main_metric_expr = "COUNT(*)"

select_exprs = []
groupby_exprs = []

if groupby:
select_exprs = groupby
select_exprs = copy(groupby)
groupby_exprs = [s for s in groupby]
inner_groupby_exprs = [s for s in groupby]
select_exprs += metrics_exprs
if granularity != "all":
select_exprs += ['ds as timestamp']
Expand All @@ -118,21 +124,28 @@ def query(
"{col} {op} ({l})".format(**locals())
)
where_clause = " AND\n".join(where_clause).format(**locals())
if timeseries_limit:
on_clause = " AND ".join(["{g} = __{g}".format(g=g) for g in groupby])
limiting_join = ""
if timeseries_limit and groupby:
inner_select = ", ".join(["{g} as __{g}".format(g=g) for g in inner_groupby_exprs])
inner_groupby_exprs = ", ".join(inner_groupby_exprs)
limiting_join = """
JOIN (
SELECT {groupby_exprs}
SELECT {inner_select}
FROM {self.table_name}
GROUP BY {groupby_exprs}
ORDER BY {metric} DESC
WHERE
{where_clause}
GROUP BY {inner_groupby_exprs}
ORDER BY {main_metric_expr} DESC
LIMIT {timeseries_limit}
) z ON
"""
) z ON {on_clause}
""".format(**locals())

sql = """
SELECT
{select_exprs}
FROM {self.table_name}
{limiting_join}
WHERE
{where_clause}
GROUP BY
Expand Down
3 changes: 2 additions & 1 deletion app/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def __init__(self, datasource, form_data, view):
self.df = self.bake_query()
self.view = view
if self.df is not None:
self.df.timestamp = pd.to_datetime(self.df.timestamp)
if 'timestamp' in self.df.columns:
self.df.timestamp = pd.to_datetime(self.df.timestamp)
self.df_prep()
self.form_prep()

Expand Down

0 comments on commit 8ea0157

Please sign in to comment.