Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

Commit

Permalink
Add period support to query.granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagen Rother committed Apr 3, 2013
1 parent d4d9831 commit 061d09b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions lib/druid/query.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion ruby-druid.gemspec
Expand Up @@ -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']
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/query_spec.rb
Expand Up @@ -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}
Expand Down

0 comments on commit 061d09b

Please sign in to comment.