Class: Druid::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/druid/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, client = nil) ⇒ Query

Returns a new instance of Query.



12
13
14
15
16
17
18
19
20
21
# File 'lib/druid/query.rb', line 12

def initialize(source, client = nil)
  @properties = {}
  @client = client

  # set some defaults
  data_source(source)
  granularity(:all)

  interval(today)
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/druid/query.rb', line 10

def properties
  @properties
end

Instance Method Details

#data_source(source) ⇒ Object



40
41
42
43
44
45
# File 'lib/druid/query.rb', line 40

def data_source(source)
  source = source.split('/')
  @properties[:dataSource] = source.last
  @service = source.first
  self
end

#filter(hash = nil, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/druid/query.rb', line 112

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

#get_query_typeObject



36
37
38
# File 'lib/druid/query.rb', line 36

def get_query_type()
  @properties[:queryType] || :groupBy
end

#granularity(gran, time_zone = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/druid/query.rb', line 146

def granularity(gran, time_zone = nil)
  gran = gran.to_s
  case gran
  when 'none', 'all', 'second', 'minute', 'fifteen_minute', 'thirty_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

#group_by(*dimensions) ⇒ Object



51
52
53
54
55
# File 'lib/druid/query.rb', line 51

def group_by(*dimensions)
  query_type(:groupBy)
  @properties[:dimensions] = dimensions.flatten
  self
end

#having(&block) ⇒ Object



138
139
140
141
142
# File 'lib/druid/query.rb', line 138

def having(&block)
  having = Having.new.instance_exec(&block)
  @properties[:having] = having
  self
end

#interval(from, to = Time.now) ⇒ Object Also known as: []



129
130
131
# File 'lib/druid/query.rb', line 129

def interval(from, to = Time.now)
  intervals([[from, to]])
end

#intervals(is) ⇒ Object



133
134
135
136
# File 'lib/druid/query.rb', line 133

def intervals(is)
  @properties[:intervals] = is.map{ |ii| mk_interval(ii[0], ii[1]) }
  self
end

#postagg(type = :long, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/druid/query.rb', line 94

def postagg(type=:long, &block)
  post_agg = PostAggregation.new.instance_exec(&block)
  @properties[:postAggregations] ||= []
  @properties[:postAggregations] << post_agg

  # make sure, the required fields are in the query
  field_type = (type.to_s + '_sum').to_sym
  # ugly workaround, because SOMEONE overwrote send
  sum_method = self.method(field_type)
  sum_method.call(post_agg.get_field_names)

  self
end

#postagg_double(&block) ⇒ Object



108
109
110
# File 'lib/druid/query.rb', line 108

def postagg_double(&block)
  postagg(:double, &block)
end

#query_type(type) ⇒ Object



31
32
33
34
# File 'lib/druid/query.rb', line 31

def query_type(type)
  @properties[:queryType] = type
  self
end

#sendObject



27
28
29
# File 'lib/druid/query.rb', line 27

def send
  @client.send(self)
end

#sourceObject



47
48
49
# File 'lib/druid/query.rb', line 47

def source
  "#{@service}/#{@properties[:dataSource]}"
end

#time_series(*aggregations) ⇒ Object



65
66
67
68
69
# File 'lib/druid/query.rb', line 65

def time_series(*aggregations)
  query_type(:timeseries)
  #@properties[:aggregations] = aggregations.flatten
  self
end

#to_jsonObject



169
170
171
# File 'lib/druid/query.rb', line 169

def to_json
  @properties.to_json
end

#todayObject



23
24
25
# File 'lib/druid/query.rb', line 23

def today
  Time.now.to_date.to_time
end

#topn(dimension, metric, threshold) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/druid/query.rb', line 57

def topn(dimension, metric, threshold)
  query_type(:topN)
  @properties[:dimension] = dimension
  @properties[:metric] = metric
  @properties[:threshold] = threshold
  self
end