Skip to content

Commit

Permalink
Adding query time
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Aug 7, 2015
1 parent 055d760 commit b4e7623
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
64 changes: 39 additions & 25 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
from pydruid.utils.filters import Dimension, Filter

from copy import deepcopy, copy
from collections import namedtuple
from datetime import datetime
import logging
import json
import requests
import textwrap

from app import db, get_session

QueryResult = namedtuple('namedtuple', ['df', 'query', 'duration'])


class Queryable(object):
@property
def column_names(self):
Expand Down Expand Up @@ -84,6 +90,7 @@ def query(
is_timeseries=True,
timeseries_limit=15, row_limit=None):
from pandas import read_sql_query
qry_start_dttm = datetime.now()
metrics_exprs = [
"{} AS {}".format(m.expression, m.metric_name)
for m in self.metrics if m.metric_name in metrics]
Expand Down Expand Up @@ -128,33 +135,36 @@ def query(
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 {inner_select}
FROM {self.table_name}
WHERE
{where_clause}
GROUP BY {inner_groupby_exprs}
ORDER BY {main_metric_expr} DESC
LIMIT {timeseries_limit}
) z ON {on_clause}
""".format(**locals())

sql = """
SELECT
{select_exprs}
FROM {self.table_name}
{limiting_join}
WHERE
{where_clause}
GROUP BY
{groupby_exprs}
""".format(**locals())
limiting_join = (
"JOIN ( \n"
" SELECT {inner_select} \n"
" FROM {self.table_name} \n"
" WHERE \n"
" {where_clause}\n"
" GROUP BY {inner_groupby_exprs}\n"
" ORDER BY {main_metric_expr} DESC\n"
" LIMIT {timeseries_limit}\n"
") z ON {on_clause}\n"
).format(**locals())

sql = (
"SELECT\n"
" {select_exprs}\n"
"FROM {self.table_name}\n"
"{limiting_join}\n"
"WHERE\n"
" {where_clause}\n"
"GROUP BY\n"
" {groupby_exprs}\n"
).format(**locals())
df = read_sql_query(
sql=sql,
con=self.database.get_sqla_engine()
)
return df
textwrap.dedent(sql)

return QueryResult(
df=df, duration=datetime.now() - qry_start_dttm, query=sql)


def fetch_metadata(self):
Expand All @@ -180,6 +190,7 @@ def fetch_metadata(self):
db.session.commit()



class SqlMetric(Model, AuditMixin):
__tablename__ = 'sql_metrics'
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -337,6 +348,7 @@ def query(
filter=None,
is_timeseries=True,
timeseries_limit=15, row_limit=None):
qry_start_dttm = datetime.now()

aggregations = {
m.metric_name: m.json_obj
Expand Down Expand Up @@ -424,10 +436,12 @@ def query(

client.groupby(**qry)
df = client.export_pandas()
return df
return QueryResult(
df=df, query="", duration=datetime.now() - qry_start_dttm)


class Metric(Model, AuditMixin):
#class Metric(Model, AuditMixin):
class Metric(Model):
__tablename__ = 'metrics'
id = Column(Integer, primary_key=True)
metric_name = Column(String(512))
Expand Down
11 changes: 10 additions & 1 deletion app/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
form input.form-control {
margin-bottom: 5px !important;
}
.tooltip-inner {
max-width: 350px;
//width: 350px;
}
</style>
{% endblock %}
{% block content_fluid %}
Expand Down Expand Up @@ -71,7 +75,12 @@ <h4>Filters</h4>
</div>

<div class="col-md-9">
<h3>{{ viz.verbose_name }}</h3>
<h3>{{ viz.verbose_name }}
<span class="label label-success">{{ "{0:0.2f}".format(results.duration.total_seconds()) }} s</span>
<span class="label label-info" data-html="true"
title="<code><pre>{{ results.query }}</pre></code>"
data-toggle="tooltip" data-placement="bottom" >query</span>
</h3>
<hr/>
{% block viz %}
{% endblock %}
Expand Down
7 changes: 4 additions & 3 deletions app/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def __init__(self, datasource, form_data, view):
self.metrics = form_data.getlist('metrics') or ['count']
self.groupby = form_data.getlist('groupby') or []

self.df = self.bake_query()
self.results = self.bake_query()
self.df = self.results.df
self.view = view
if self.df is not None:
if 'timestamp' in self.df.columns:
Expand Down Expand Up @@ -147,6 +148,7 @@ def render(self, *args, **kwargs):
form = self.form_class(self.form_data)
return self.view.render_template(
self.template, form=form, viz=self, datasource=self.datasource,
results=self.results,
*args, **kwargs)


Expand Down Expand Up @@ -225,8 +227,7 @@ def bake_query(self):
"""
Doing a 2 phase query where we limit the number of series.
"""
qry = self.query_obj()
return self.datasource.query(**qry)
return self.datasource.query(**self.query_obj())

class TimeSeriesCompareViz(TimeSeriesViz):
verbose_name = "Time Series - Percent Change"
Expand Down

0 comments on commit b4e7623

Please sign in to comment.