Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add period support to query.granularity

  • Loading branch information...
commit 061d09b3bc66d919265e01c1c465a523e3101d97 1 parent d4d9831
Hagen Rother authored April 03, 2013
16  README.md
Source Rendered
@@ -85,12 +85,28 @@ query.interval("2013-01-01T00", Time.now)
85 85
 
86 86
 granularity can be `:all`, `:none`, `:minute`, `:fifteen_minute`, `:thirthy_minute`, `:hour` or `:day`.
87 87
 
  88
+It can also be a period granularity as described in https://github.com/metamx/druid/wiki/Granularities.
  89
+
  90
+The period `'day'` or `:day` will be interpreted as `'P1D'`.
  91
+
  92
+If a period granularity is specifed, the (optional) second parameter is a time zone. It defaults
  93
+to the machines local time zone.
  94
+
  95
+I.E:
88 96
 ```ruby
89 97
 query = Druid::Query.new('service/source').long_sum(:aggregate1)
90 98
 
91 99
 query.granularity(:day)
92 100
 ```
93 101
 
  102
+is (on my box) the same as
  103
+
  104
+```ruby
  105
+query = Druid::Query.new('service/source').long_sum(:aggregate1)
  106
+
  107
+query.granularity('P1D', 'Europe/Berlin')
  108
+```
  109
+
94 110
 ## filter
95 111
 
96 112
 Filters are set by the `filter` method. It takes a block or a hash as parameter.
22  lib/druid/query.rb
@@ -98,8 +98,26 @@ def interval(from, to = Time.now)
98 98
 
99 99
     alias_method :[], :interval
100 100
 
101  
-    def granularity(gran)
102  
-      @properties[:granularity] = gran.to_s
  101
+    def granularity(gran, time_zone = nil)
  102
+      gran = gran.to_s
  103
+      case gran
  104
+      when 'none', 'all', 'minute', 'fifteen_minute', 'thirthy_minute', 'hour'
  105
+        @properties[:granularity] = gran
  106
+        return self
  107
+      when 'day'
  108
+        gran = 'P1D'
  109
+      end
  110
+
  111
+      time_zone ||= Time.now.strftime('%Z')
  112
+      # druid doesn't seem to understand 'CEST'
  113
+      # this is a work around
  114
+      time_zone = 'Europe/Berlin' if time_zone == 'CEST'
  115
+
  116
+      @properties[:granularity] = {
  117
+        :type => 'period',
  118
+        :period => gran,
  119
+        :timeZone => time_zone
  120
+      }
103 121
       self
104 122
     end
105 123
 
2  ruby-druid.gemspec
@@ -3,7 +3,7 @@ require 'rake'
3 3
 Gem::Specification.new do |gem|
4 4
   gem.name          = 'ruby-druid'
5 5
   gem.version       = '0.0.3'
6  
-  gem.date          = '2013-02-20'
  6
+  gem.date          = '2013-04-03'
7 7
   gem.summary       = 'Ruby client for druid'
8 8
   gem.description   = 'Ruby client for metamx druid'
9 9
   gem.authors       = ['Hagen Rother', 'Holger Pillmann']
9  spec/lib/query_spec.rb
@@ -130,6 +130,15 @@
130 130
     JSON.parse(@query.to_json)['granularity'].should == 'all'
131 131
   end
132 132
 
  133
+  it 'should take a period' do
  134
+    @query.granularity(:day, 'CEST')
  135
+    @query.properties[:granularity].should == {
  136
+      :type => "period",
  137
+      :period => "P1D",
  138
+      :timeZone => "Europe/Berlin"
  139
+    }
  140
+  end
  141
+
133 142
   it 'creates an equals filter' do
134 143
     @query.filter{a.eq 1}
135 144
     JSON.parse(@query.to_json)['filter'].should == {"type"=>"selector", "dimension"=>"a", "value"=>1}

0 notes on commit 061d09b

Please sign in to comment.
Something went wrong with that request. Please try again.