Permalink
Browse files

Add period support to query.granularity

  • Loading branch information...
sixtus committed Apr 3, 2013
1 parent d4d9831 commit 061d09b3bc66d919265e01c1c465a523e3101d97
Showing with 46 additions and 3 deletions.
  1. +16 −0 README.md
  2. +20 −2 lib/druid/query.rb
  3. +1 −1 ruby-druid.gemspec
  4. +9 −0 spec/lib/query_spec.rb
View
@@ -85,12 +85,28 @@ query.interval("2013-01-01T00", Time.now)
granularity can be `:all`, `:none`, `:minute`, `:fifteen_minute`, `:thirthy_minute`, `:hour` or `:day`.
+It can also be a period granularity as described in https://github.com/metamx/druid/wiki/Granularities.
+
+The period `'day'` or `:day` will be interpreted as `'P1D'`.
+
+If a period granularity is specifed, the (optional) second parameter is a time zone. It defaults
+to the machines local time zone.
+
+I.E:
```ruby
query = Druid::Query.new('service/source').long_sum(:aggregate1)
query.granularity(:day)
```
+is (on my box) the same as
+
+```ruby
+query = Druid::Query.new('service/source').long_sum(:aggregate1)
+
+query.granularity('P1D', 'Europe/Berlin')
+```
+
## filter
Filters are set by the `filter` method. It takes a block or a hash as parameter.
View
@@ -98,8 +98,26 @@ def interval(from, to = Time.now)
alias_method :[], :interval
- def granularity(gran)
- @properties[:granularity] = gran.to_s
+ def granularity(gran, time_zone = nil)
+ gran = gran.to_s
+ case gran
+ when 'none', 'all', 'minute', 'fifteen_minute', 'thirthy_minute', 'hour'
+ @properties[:granularity] = gran
+ return self
+ when 'day'
+ gran = 'P1D'
+ end
+
+ time_zone ||= Time.now.strftime('%Z')
+ # druid doesn't seem to understand 'CEST'
+ # this is a work around
+ time_zone = 'Europe/Berlin' if time_zone == 'CEST'
+
+ @properties[:granularity] = {
+ :type => 'period',
+ :period => gran,
+ :timeZone => time_zone
+ }
self
end
View
@@ -3,7 +3,7 @@ require 'rake'
Gem::Specification.new do |gem|
gem.name = 'ruby-druid'
gem.version = '0.0.3'
- gem.date = '2013-02-20'
+ gem.date = '2013-04-03'
gem.summary = 'Ruby client for druid'
gem.description = 'Ruby client for metamx druid'
gem.authors = ['Hagen Rother', 'Holger Pillmann']
View
@@ -130,6 +130,15 @@
JSON.parse(@query.to_json)['granularity'].should == 'all'
end
+ it 'should take a period' do
+ @query.granularity(:day, 'CEST')
+ @query.properties[:granularity].should == {
+ :type => "period",
+ :period => "P1D",
+ :timeZone => "Europe/Berlin"
+ }
+ end
+
it 'creates an equals filter' do
@query.filter{a.eq 1}
JSON.parse(@query.to_json)['filter'].should == {"type"=>"selector", "dimension"=>"a", "value"=>1}

0 comments on commit 061d09b

Please sign in to comment.