Skip to content

Commit

Permalink
Supporting Druid'd time origin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Apr 14, 2016
1 parent 5cadd67 commit 1538cd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions caravel/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ def __init__(self, viz):
'Y',
choices=self.choicify(datasource.column_names),
description="Columns to display"),
'druid_time_origin': SelectField(
'Origin',
choices=(
('now', 'now'),
('2016-04-10', 'even'),
('2016-04-11', 'monday'),
),
description="Defines the anchor where time buckets start"),
'granularity': FreeFormSelectField(
'Time Granularity', default="one day",
choices=self.choicify([
Expand All @@ -224,6 +232,7 @@ def __init__(self, viz):
'6 hour',
'1 day',
'7 days',
# you could add a special string here
]),
description=(
"The time granularity for the visualization. Note that you "
Expand Down Expand Up @@ -615,9 +624,10 @@ def add_to_form(attrs):
time_fields = 'granularity_sqla'
add_to_form((time_fields, ))
else:
time_fields = 'granularity'
add_to_form(('granularity',))
time_fields = ('granularity', 'druid_time_origin')
add_to_form(('granularity', 'druid_time_origin'))
field_css_classes['granularity'] = ['form-control', 'select2_freeform']
field_css_classes['druid_time_origin'] = ['form-control', 'select2']
add_to_form(('since', 'until'))

QueryForm.fieldsets = ({
Expand Down
5 changes: 5 additions & 0 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,11 @@ def query(
granularity).total_seconds() * 1000
if not isinstance(granularity, string_types):
granularity = {"type": "duration", "duration": granularity}
origin = extras.get('druid_time_origin')
if origin == 'now':
granularity['origin'] = datetime.now().isoformat()
elif origin:
granularity['origin'] = origin

qry = dict(
datasource=self.datasource_name,
Expand Down
1 change: 1 addition & 0 deletions caravel/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def query_obj(self):
'where': form_data.get("where", ''),
'having': form_data.get("having", ''),
'time_grain_sqla': form_data.get("time_grain_sqla", ''),
'druid_time_origin': form_data.get("druid_time_origin", ''),
}
d = {
'granularity': granularity,
Expand Down

0 comments on commit 1538cd4

Please sign in to comment.