Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now supporting SQL Multiple database #13

Merged
merged 6 commits into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
SQL working !
  • Loading branch information
mistercrunch committed Aug 6, 2015
commit 8ea0157f1c084a2edaee40c485f1cf21e2f5bcc6
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