From 8ea0157f1c084a2edaee40c485f1cf21e2f5bcc6 Mon Sep 17 00:00:00 2001 From: Maxime Date: Thu, 6 Aug 2015 07:00:17 +0000 Subject: [PATCH] SQL working ! --- app/models.py | 29 +++++++++++++++++++++-------- app/viz.py | 3 ++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/models.py b/app/models.py index 95176ba80f56c..89440f90749ee 100644 --- a/app/models.py +++ b/app/models.py @@ -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 @@ -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'] @@ -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 diff --git a/app/viz.py b/app/viz.py index d4929c2df83d7..42c9570b4710a 100644 --- a/app/viz.py +++ b/app/viz.py @@ -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()