Skip to content

Commit

Permalink
Minor fixes and improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Aug 13, 2015
1 parent 59efbcd commit 36a58e8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
6 changes: 4 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import timedelta
from flask.ext.appbuilder.models.mixins import AuditMixin
from sqlalchemy import Column, Integer, String, ForeignKey, Text, Boolean, DateTime
from sqlalchemy import create_engine, MetaData
from sqlalchemy import create_engine, MetaData, desc
from sqlalchemy import Table as sqlaTable
from sqlalchemy.orm import relationship
from dateutil.parser import parse
Expand Down Expand Up @@ -210,11 +210,11 @@ def query(
select_exprs = [literal_column(s) for s in groupby]
groupby_exprs = [literal_column(s) for s in groupby]
inner_groupby_exprs = [literal_column(s).label('__' + s) for s in groupby]
select_exprs += metrics_exprs
if granularity != "all":
select_exprs += [timestamp]
groupby_exprs += [timestamp]

select_exprs += metrics_exprs
qry = select(select_exprs)
from_clause = table(self.table_name)
qry = qry.group_by(*groupby_exprs)
Expand All @@ -231,13 +231,15 @@ def query(
cond = ~cond
where_clause_and.append(cond)
qry = qry.where(and_(*where_clause_and))
qry = qry.order_by(desc(main_metric_expr))
qry = qry.limit(row_limit)

if timeseries_limit and groupby:
subq = select(inner_groupby_exprs)
subq = subq.select_from(table(self.table_name))
subq = subq.where(and_(*where_clause_and))
subq = subq.group_by(*inner_groupby_exprs)
subq = subq.order_by(desc(main_metric_expr))
subq = subq.limit(timeseries_limit)
on_clause = []
for gb in groupby:
Expand Down
1 change: 0 additions & 1 deletion app/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ <h3>
</div>
</div>
<div>{{ form.groupby.label }}: {{ form.groupby(class_="form-control select2") }}</div>
<div>{{ form.limit.label }}: {{ form.limit(class_="form-control select2") }}</div>
{% block extra_fields %}{% endblock %}
<hr>
<h4>Filters</h4>
Expand Down
1 change: 1 addition & 0 deletions app/templates/panoramix/viz_highcharts.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<span class="col col-sm-4">{{ form.rolling_periods.label }}: {{ form.rolling_periods(class_="form-control") }}</span>
</div>
{% endif %}
<div>{{ form.limit.label }}: {{ form.limit(class_="form-control select2") }}</div>
{% endblock %}

{% block tail %}
Expand Down
24 changes: 22 additions & 2 deletions app/templates/panoramix/viz_table.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
{% extends "panoramix/datasource.html" %}

{% block head_css %}
{{super()}}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='dataTables.bootstrap.css') }}">
{% endblock %}

{% block viz %}
{{ table|safe }}
{% endblock %}

{% block scripts %}
{% block extra_fields %}
<div>{{ form.row_limit.label }}: {{ form.row_limit(class_="form-control select2") }}</div>
{% endblock %}

{% block tail %}
{{ super() }}
<script src="{{ url_for('static', filename='jquery.dataTables.min.js') }}"></script>
<script src="{{ url_for('static', filename='dataTables.bootstrap.js') }}"></script>

<script>
$('table').css('background-color', 'white');
//$('table').css('background-color', 'white');
$(document).ready(function() {
var table = $('table').DataTable({
paging: false,
});
table.column('-1').order( 'desc' ).draw();
});
</script>
{% endblock %}

35 changes: 25 additions & 10 deletions app/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def fields(self):

def form_factory(datasource, form_args=None, extra_fields_dict=None):
extra_fields_dict = extra_fields_dict or {}
limits = [0, 5, 10, 25, 50, 100, 500]

if form_args:
limit = form_args.get("limit")
Expand All @@ -54,8 +53,6 @@ class QueryForm(OmgWtForm):
granularity = TextField('Time Granularity', default="one day")
since = TextField('Since', default="one day ago")
until = TextField('Until', default="now")
limit = SelectField(
'Limit', choices=[(s, s) for s in limits])
for i in range(10):
setattr(QueryForm, 'flt_col_' + str(i), SelectField(
'Filter 1', choices=[(s, s) for s in datasource.filterable_column_names]))
Expand Down Expand Up @@ -110,10 +107,13 @@ def query_obj(self):
groupby = args.getlist("groupby") or []
metrics = args.getlist("metrics") or ['count']
granularity = args.get("granularity", "1 day")
granularity = utils.parse_human_timedelta(
granularity).total_seconds() * 1000
if granularity != "all":
granularity = utils.parse_human_timedelta(
granularity).total_seconds() * 1000
limit = int(
args.get("limit", config.ROW_LIMIT)) or config.ROW_LIMIT
args.get("limit", config.ROW_LIMIT))
row_limit = int(
args.get("row_limit", config.ROW_LIMIT))
since = args.get("since", "1 year ago")
from_dttm = utils.parse_human_datetime(since)
if from_dttm > datetime.now():
Expand All @@ -129,6 +129,7 @@ def query_obj(self):
'to_dttm': to_dttm,
'groupby': groupby,
'metrics': metrics,
'row_limit': row_limit,
'filter': self.query_filters(),
'timeseries_limit': limit,
}
Expand Down Expand Up @@ -156,19 +157,29 @@ class TableViz(BaseViz):
verbose_name = "Table View"
template = 'panoramix/viz_table.html'
def render(self):
if self.df is None or self.df.empty:
df = self.df
row_limit = request.args.get("row_limit")
if df is None or df.empty:
flash("No data.", "error")
table = None
else:
if self.form_data.get("granularity") == "all":
del self.df['timestamp']
table = self.df.to_html(
if self.form_data.get("granularity") == "all" and 'timestamp' in df:
del df['timestamp']
table = df.to_html(
classes=[
'table', 'table-striped', 'table-bordered',
'table-condensed'],
index=False)
return super(TableViz, self).render(table=table)

def form_class(self):
limits = [10, 50, 100, 500, 1000, 5000, 10000]
return form_factory(self.datasource, request.args,
extra_fields_dict={
'row_limit':
SelectField('Row limit', choices=[(s, s) for s in limits])
})


class HighchartsViz(BaseViz):
verbose_name = "Base Highcharts Viz"
Expand All @@ -194,6 +205,7 @@ def render(self):
values=metrics,)

rolling_periods = request.args.get("rolling_periods")
limit = request.args.get("limit")
rolling_type = request.args.get("rolling_type")
if rolling_periods and rolling_type:
if rolling_type == 'mean':
Expand All @@ -214,13 +226,16 @@ def render(self):
return super(TimeSeriesViz, self).render(chart_js=chart.javascript_cmd)

def form_class(self):
limits = [0, 5, 10, 25, 50, 100, 500]
return form_factory(self.datasource, request.args,
extra_fields_dict={
#'compare': TextField('Period Compare',),
'rolling_type': SelectField(
'Rolling',
choices=[(s, s) for s in ['mean', 'sum', 'std']]),
'rolling_periods': TextField('Periods',),
'limit': SelectField(
'Series limit', choices=[(s, s) for s in limits])
})

def bake_query(self):
Expand Down

0 comments on commit 36a58e8

Please sign in to comment.