ruby-druid 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -187,7 +187,7 @@ Druid::Query.new('service/source').filter{!dimension.eq(1)}
187
187
 
188
188
  #### Inclusion Filter
189
189
 
190
- This filter creates a set of equals filters in an and filter.
190
+ This filter creates a set of equals filters in an or filter.
191
191
 
192
192
  ```ruby
193
193
  Druid::Query.new('service/source').filter{dimension.in(1,2,3)}
@@ -204,6 +204,14 @@ Druid::Query.new('service/source').time_series().long_sum([:aggregate1]).filter{
204
204
  Druid::Query.new('service/source').time_series().long_sum([:aggregate1]).filter{coordinates.in_circ [[53.0,13.0], 5.0]}
205
205
  ```
206
206
 
207
+ #### Exclusion Filter
208
+
209
+ This filter creates a set of not-equals fitlers in an and filter.
210
+
211
+ ```ruby
212
+ Druid::Query.new('service/source').filter{dimension.nin(1,2,3)}
213
+ ```
214
+
207
215
  #### Hash syntax
208
216
 
209
217
  Sometimes it can be useful to use a hash syntax for filtering
@@ -69,6 +69,6 @@ module Druid
69
69
  client.query(@source)
70
70
  end
71
71
 
72
- def_delegators :query, :group_by, :sum, :long_sum, :double_sum, :postagg, :interval, :granularity, :filter, :time_series, :topn
72
+ def_delegators :query, :group_by, :sum, :long_sum, :double_sum, :count, :postagg, :interval, :granularity, :filter, :time_series, :topn
73
73
  end
74
74
  end
@@ -1,7 +1,7 @@
1
1
  module Druid
2
2
  class Filter
3
3
  (instance_methods + private_instance_methods).each do |method|
4
- unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect)/ || method.to_s =~ /\?/
4
+ unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect|send)/ || method.to_s =~ /\?/
5
5
  undef_method method
6
6
  end
7
7
  end
@@ -13,7 +13,7 @@ module Druid
13
13
 
14
14
  class FilterParameter
15
15
  (instance_methods + private_instance_methods).each do |method|
16
- unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect|class)/ || method.to_s =~ /\?/
16
+ unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect|class|send)/ || method.to_s =~ /\?/
17
17
  undef_method method
18
18
  end
19
19
  end
@@ -55,7 +55,6 @@ module Druid
55
55
 
56
56
  alias :'==' :eq
57
57
 
58
-
59
58
  def neq(value)
60
59
  return !self.in(value)
61
60
  end
@@ -64,20 +63,12 @@ module Druid
64
63
 
65
64
  def in(*args)
66
65
  values = args.flatten
67
- raise "Must provide non-empty array in in()" if values.empty?
68
-
69
- if (values.length == 1)
70
- return self.eq(values[0])
71
- end
66
+ filter_multiple(values, 'or', :eq)
67
+ end
72
68
 
73
- filter_or = FilterOperator.new('or', true)
74
- values.each do |value|
75
- raise "query is too complex" if value.is_a? FilterParameter
76
- param = FilterDimension.new(@name)
77
- param.eq value
78
- filter_or.add param
79
- end
80
- filter_or
69
+ def nin(*args)
70
+ values = args.flatten
71
+ filter_multiple(values, 'and', :neq)
81
72
  end
82
73
 
83
74
  def &(other)
@@ -145,6 +136,20 @@ module Druid
145
136
  end
146
137
  hash
147
138
  end
139
+
140
+ private
141
+
142
+ def filter_multiple(values, operator, method)
143
+ raise 'Values cannot be empty' if values.empty?
144
+ return self.send(method, values[0]) if values.length == 1
145
+
146
+ filter = FilterOperator.new(operator, true)
147
+ values.each do |value|
148
+ raise 'Value cannot be a parameter' if value.is_a?(FilterParameter)
149
+ filter.add(FilterDimension.new(@name).send(method, value))
150
+ end
151
+ filter
152
+ end
148
153
  end
149
154
 
150
155
  class FilterOperator < FilterParameter
@@ -68,9 +68,9 @@ module Druid
68
68
  self
69
69
  end
70
70
 
71
- [:long_sum, :double_sum].each do |method_name|
71
+ [:long_sum, :double_sum, :count].each do |method_name|
72
72
  agg_type = method_name.to_s.split('_')
73
- agg_type[1].capitalize!
73
+ agg_type[1].capitalize! if agg_type.length > 1
74
74
  agg_type = agg_type.join
75
75
 
76
76
  define_method method_name do |*metrics|
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "ruby-druid"
5
- spec.version = "0.1.5"
5
+ spec.version = "0.1.6"
6
6
  spec.authors = ["LiquidM, Inc."]
7
7
  spec.email = ["opensource@liquidm.com"]
8
8
  spec.summary = %q{Ruby client for metamx druid}
@@ -315,6 +315,16 @@ end
315
315
  "type" => "or"}
316
316
  end
317
317
 
318
+ it 'creates a nin statement with and filter' do
319
+ @query.filter{a.nin [1,2,3]}
320
+ JSON.parse(@query.to_json)['filter'].should == {"fields" => [
321
+ {"field"=>{"type"=>"selector", "dimension"=>"a", "value"=>1},"type" => "not"},
322
+ {"field"=>{"type"=>"selector", "dimension"=>"a", "value"=>2},"type" => "not"},
323
+ {"field"=>{"type"=>"selector", "dimension"=>"a", "value"=>3},"type" => "not"}
324
+ ],
325
+ "type" => "and"}
326
+ end
327
+
318
328
  it 'creates a javascript with > filter' do
319
329
  @query.filter{a > 100}
320
330
  JSON.parse(@query.to_json)['filter'].should == {
@@ -366,7 +376,7 @@ end
366
376
  end
367
377
 
368
378
  it 'does not accept in with empty array' do
369
- expect { @query.filter{a.in []} }.to raise_error "Must provide non-empty array in in()"
379
+ expect { @query.filter{a.in []} }.to raise_error "Values cannot be empty"
370
380
  end
371
381
 
372
382
  it 'does raise on invalid filter statement' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-druid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-17 00:00:00.000000000 Z
12
+ date: 2014-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zk
@@ -86,12 +86,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
86
  - - ! '>='
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
+ segments:
90
+ - 0
91
+ hash: -3649172061711800760
89
92
  required_rubygems_version: !ruby/object:Gem::Requirement
90
93
  none: false
91
94
  requirements:
92
95
  - - ! '>='
93
96
  - !ruby/object:Gem::Version
94
97
  version: '0'
98
+ segments:
99
+ - 0
100
+ hash: -3649172061711800760
95
101
  requirements: []
96
102
  rubyforge_project:
97
103
  rubygems_version: 1.8.25