This repository has been archived by the owner on Feb 19, 2018. It is now read-only.
/
query.rb
368 lines (299 loc) · 7.62 KB
/
query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
require 'time'
require 'json'
module Druid
class Query
attr_reader :properties
def initialize(source, client = nil)
@properties = {}
@client = client
# set some defaults
data_source(source)
granularity(:all)
interval(Time.now - 86400, Time.now)
end
def send
@client.send(self)
end
def query_type(type)
@properties[:queryType] = type
self
end
def data_source(source)
source = source.split('/')
@properties[:dataSource] = source.last
@service = source.first
self
end
def source
"#{@service}/#{@properties[:dataSource]}"
end
def group_by(*dimensions)
query_type(:groupBy)
@properties[:dimensions] = dimensions.flatten
self
end
[:long_sum, :double_sum].each do |method_name|
agg_type = method_name.to_s.split('_')
agg_type[1].capitalize!
agg_type = agg_type.join
define_method method_name do |*metrics|
query_type(:groupBy)
aggregations = (@properties[:aggregations] || []).select{|agg| agg[:type] != agg_type }
aggregations.concat(metrics.flatten.map{ |metric|
{
:type => agg_type,
:name => metric,
:fieldName => metric
}
})
@properties[:aggregations] = aggregations
self
end
end
def postagg(&block)
post_agg = PostAggregation.new.instance_exec(&block)
@properties[:postAggregations] = post_agg
self
end
def filter(hash = nil, &block)
if hash
last = nil
hash.each do |k,values|
filter = FilterDimension.new(k).in(values)
last = last ? last.&(filter) : filter
end
@properties[:filter] = @properties[:filter] ? @properties[:filter].&(last) : last
end
if block
filter = Filter.new.instance_exec(&block)
raise "Not a valid filter" unless filter.is_a? FilterParameter
@properties[:filter] = @properties[:filter] ? @properties[:filter].&(filter) : filter
end
self
end
def interval(from, to = Time.now)
from = Time.now + from if from.is_a?(Fixnum)
to = Time.now + to if to.is_a?(Fixnum)
from = DateTime.parse(from.to_s) unless from.respond_to? :iso8601
to = DateTime.parse(to.to_s) unless to.respond_to? :iso8601
@properties[:intervals] = ["#{from.iso8601}/#{to.iso8601}"]
self
end
alias_method :[], :interval
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
def to_json
@properties.to_json
end
end
class PostAggregation
def initialize
@values = []
end
def method_missing(name, *args, &block)
PostAggregationField.new(name)
end
def to_json(*a)
@values.to_json
end
end
class PostAggregationOperation
def initialize(left, name, right)
@name = name
right = PostAggregationConstant.new(1) if right.is_a? Numeric
@values = [left, right]
end
def as(output_field)
@as = output_field.name
self
end
def to_json(*a)
[{ "type" => "arithmetic", "fn" => @name, "fields" => @values, "name" => @as}].to_json
end
end
class PostAggregationField
attr_accessor :name
def initialize(name)
@name = name
end
[:+, :-, :/, :*].each do |method_name|
define_method method_name do |*params|
PostAggregationOperation.new(self, method_name, params[0])
end
end
def to_json(*a)
{"type" => "fieldAccess", "name" => @name, "fieldName" => @name}.to_json
end
end
class PostAggregationConstant < PostAggregationField
def initialize(name)
@name = name
end
def to_json(*a)
{'type' => 'constant', 'value' => @name }.to_json
end
end
class FilterParameter
(instance_methods + private_instance_methods).each do |method|
unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect)/ || method.to_s =~ /\?/
undef_method method
end
end
end
class Filter
(instance_methods + private_instance_methods).each do |method|
unless method.to_s =~ /^(__|instance_eval|instance_exec|initialize|object_id|raise|puts|inspect)/ || method.to_s =~ /\?/
undef_method method
end
end
def method_missing(method_id, *args)
FilterDimension.new(method_id)
end
end
class FilterDimension < FilterParameter
def initialize(name)
@name = name
@value = nil
end
def eq(value)
return self.in(value) if value.is_a? Array
@value = value
self
end
alias :'==' :eq
def neq(value)
return !self.in(value)
end
alias :'!=' :neq
def in(*args)
values = args.flatten
raise "Must provide non-empty array in in()" if values.empty?
if (values.length == 1)
@value = values[0]
return self
end
filter_or = FilterOperator.new('or', true)
values.each do |value|
raise "query is too complex" if value.is_a? FilterParameter
param = FilterDimension.new(@name)
param.eq value
filter_or.add param
end
filter_or
end
def &(other)
filter_and = FilterOperator.new('and', true)
filter_and.add(self)
filter_and.add(other)
filter_and
end
def |(other)
filter_or = FilterOperator.new('or', true)
filter_or.add(self)
filter_or.add(other)
filter_or
end
def !()
filter_not = FilterOperator.new('not', false)
filter_not.add(self)
filter_not
end
def to_hash
raise 'no value assigned' if @value.nil?
{
:type => 'selector',
:dimension => @name,
:value => @value
}
end
def to_s
to_hash.to_s
end
def as_json(*a)
to_hash
end
def to_json(*a)
to_hash.to_json(*a)
end
end
class FilterOperator < FilterParameter
def initialize(name, takes_many)
@name = name
@takes_many = takes_many
@elements = []
end
def add(element)
@elements.push element
end
def &(other)
if @name == 'and'
filter_and = self
else
filter_and = FilterOperator.new('and', true)
filter_and.add(self)
end
filter_and.add(other)
filter_and
end
def |(other)
if @name == 'or'
filter_or = self
else
filter_or = FilterOperator.new('or', true)
filter_or.add(self)
end
filter_or.add(other)
filter_or
end
def !()
if @name == 'not'
@elements[0]
else
filter_not = FilterOperator.new('not', false)
filter_not.add(self)
filter_not
end
end
def to_json(*a)
to_hash.to_json(*a)
end
def as_json(*a)
to_hash
end
def to_s
to_hash.to_s
end
def as_json(options)
to_hash
end
def to_hash
result = {
:type => @name
}
if @takes_many
result[:fields] = @elements.map(&:to_hash)
else
result[:field] = @elements[0].to_hash
end
result
end
end
end