Skip to content

Commit

Permalink
Humanized time boundaries and granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jul 17, 2015
1 parent 16f7bf9 commit 8b17563
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 32 deletions.
Binary file modified app.db
Binary file not shown.
4 changes: 3 additions & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Datasource(Model, AuditMixin):

@property
def metrics_combo(self):
return [(m.metric_name, m.verbose_name) for m in self.metrics]
return sorted(
[(m.metric_name, m.verbose_name) for m in self.metrics],
key=lambda x: x[1])

def __repr__(self):
return self.datasource_name
Expand Down
67 changes: 55 additions & 12 deletions app/templates/panoramix/datasource.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
{% block head_css %}
{{super()}}
<style>
form .row {
margin-left: 0;
margin-right: 0;
}
form .col {
padding-right:0px;
padding-left:0px;
.select2-container-multi .select2-choices {
height: 70px;
overflow: auto;
}
</style>
{% endblock %}
Expand All @@ -24,18 +20,21 @@ <h3>
<form method="GET">
<div>{{ form.viz_type.label }}: {{ form.viz_type(class_="form-control select2") }}</div>
<div>{{ form.metric.label }}: {{ form.metric(class_="form-control select2") }}</div>
<div>{{ form.granularity.label }}: {{ form.granularity(class_="form-control select2") }}</div>
<div>{{ form.since.label }}: {{ form.since(class_="form-control select2") }}</div>
<div>{{ form.granularity.label }}: {{ form.granularity(class_="form-control select2_free_granularity") }}</div>
<div class="row">
<div class="form-group">
<div class="col-xs-6">{{ form.since.label }}: {{ form.since(class_="form-control select2_free_since") }}</div>
<div class="col-xs-6">{{ form.until.label }}: {{ form.until(class_="form-control select2_free_until") }}</div>
</div>
</div>
<div>{{ form.groupby.label }}: {{ form.groupby(class_="form-control select2") }}</div>
<div>{{ form.limit.label }}: {{ form.limit(class_="form-control select2") }}</div>
<hr>
<h4>Filters</h4>
<div id="filters">
{% for i in range(10) %}
<div id="flt{{ i }}" class="{{ "hidden" if i != 1 }}">
<div class="row">
<span class="" style="width: 100px;">{{ form['flt_col_' ~ i](class_="form-control select2 inc") }}</span>
</div>
<div class="row">
<span class="col col-md-3">{{ form['flt_op_' ~ i](class_="form-control select2 input-sm inc") }}</span>
<span class="col col-md-7">{{ form['flt_eq_' ~ i](class_="form-control inc") }}</span>
Expand Down Expand Up @@ -83,7 +82,51 @@ <h3>Latest Segment Metadata</h3>
<script>
$( document ).ready(function() {
$(".select2").select2();
$(".select2_tags").select2({tags: true});

function create_choices (term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term)===0;
}).length===0)
{return {id:term, text:term};}
}
$(".select2_free_since").select2({
createSearchChoice: create_choices,
multiple: false,
data: [
{id: '-1 hour', text: '-1 hour'},
{id: '-12 hours', text: '-12 hours'},
{id: '-1 day', text: '-1 day'},
{id: '-7 days', text: '-7 days'},
{id: '-28 days', text: '-28 days'},
{id: '-90 days', text: '-90 days'},
{id: '{{ form.data.since }}', text: '{{ form.data.since }}'},
]
});
$(".select2_free_until").select2({
createSearchChoice: create_choices,
multiple: false,
data: [
{id: '{{ form.data.until }}', text: '{{ form.data.until }}'},
{id: 'now', text: 'now'},
{id: '-1 day', text: '-1 day'},
{id: '-7 days', text: '-7 days'},
{id: '-28 days', text: '-28 days'},
{id: '-90 days', text: '-90 days'},
]
});
$(".select2_free_granularity").select2({
createSearchChoice: create_choices,
multiple: false,
data: [
{id: '{{ form.data.granularity }}', text: '{{ form.data.granularity }}'},
{id: '5 seconds', text: '5 seconds'},
{id: '30 seconds', text: '30 seconds'},
{id: '1 minute', text: '1 minute'},
{id: '5 minutes', text: '5 minutes'},
{id: '1 day', text: '1 day'},
{id: '7 days', text: '7 days'},
]
});
});
</script>
{% endblock %}
28 changes: 20 additions & 8 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
from datetime import timedelta, datetime
import parsedatetime

since_l = {
'1hour': timedelta(hours=1),
'1day': timedelta(days=1),
'7days': timedelta(days=7),
'28days': timedelta(days=28),
'all': timedelta(days=365*100)
}

def get_pydruid_client():
from pydruid import client
Expand All @@ -26,6 +19,25 @@ def parse_human_datetime(s):
True
"""
cal = parsedatetime.Calendar()
d = cal.parse(s)[0]
return dttm_from_timtuple(cal.parse(s)[0])


def dttm_from_timtuple(d):
return datetime(
d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec)


def parse_human_timedelta(s):
"""
Use the parsedatetime lib to return ``datetime.datetime`` from human
generated strings
>>> parse_human_datetime("now") <= datetime.now()
True
"""
cal = parsedatetime.Calendar()
dttm = dttm_from_timtuple(datetime.now().timetuple())
d = cal.parse(s, dttm)[0]
d = datetime(
d.tm_year, d.tm_mon, d.tm_mday, d.tm_hour, d.tm_min, d.tm_sec)
return d - dttm
14 changes: 9 additions & 5 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from wtforms import Form, SelectMultipleField, SelectField, TextField
from wtforms.fields import Field


class OmgWtForm(Form):
field_order = (
'viz_type', 'granularity', 'since', 'group_by', 'limit')
Expand Down Expand Up @@ -44,11 +45,14 @@ class QueryForm(OmgWtForm):
groupby = SelectMultipleField(
'Group by', choices=[
(s, s) for s in datasource.groupby_column_names])
granularity = SelectField(
'Time Granularity', choices=[(g, g) for g in grain])
since = SelectField(
'Since', choices=[(s, s) for s in utils.since_l.keys()],
default="all")
#granularity = SelectField(
# 'Time Granularity', choices=[(g, g) for g in grain])
#since = SelectField(
# 'Since', choices=[(s, s) for s in utils.since_l.keys()],
# default="all")
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):
Expand Down
19 changes: 14 additions & 5 deletions app/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ def query_obj(self):
ds = self.datasource
args = self.form_data
groupby = args.getlist("groupby") or []
granularity = args.get("granularity")
granularity = args.get("granularity", "1 day")
granularity = utils.parse_human_timedelta(granularity).total_seconds() * 1000
aggregations = {
m.metric_name: m.json_obj
for m in ds.metrics if m.metric_name == self.metric
}
limit = int(
args.get("limit", config.ROW_LIMIT)) or config.ROW_LIMIT
since = args.get("since", "all")
from_dttm = (datetime.now() - utils.since_l[since]).isoformat()
since = args.get("since", "1 year ago")
from_dttm = utils.parse_human_datetime(since)
if from_dttm > datetime.now():
from_dttm = datetime.now() - (from_dttm-datetime.now())
from_dttm = from_dttm.isoformat()
until = args.get("until", "now")
to_dttm = utils.parse_human_datetime(until).isoformat()
if from_dttm >= to_dttm:
flash("The date range doesn't seem right.", "danger")
from_dttm = to_dttm # Making them identicial to not raise
d = {
'datasource': ds.datasource_name,
'granularity': granularity or 'all',
'intervals': from_dttm + '/' + datetime.now().isoformat(),
'granularity': {"type": "duration", "duration": granularity},
'intervals': from_dttm + '/' + to_dttm,
'dimensions': groupby,
'aggregations': aggregations,
'limit_spec': {
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
# Theme configuration
# these are located on static/appbuilder/css/themes
# you can create your own and easily use them placing them on the same dir structure to override
APP_THEME = "bootstrap-theme.css" # default bootstrap
#APP_THEME = "bootstrap-theme.css" # default bootstrap
#APP_THEME = "cerulean.css"
#APP_THEME = "amelia.css"
#APP_THEME = "cosmo.css"
Expand Down

0 comments on commit 8b17563

Please sign in to comment.