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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/druid/query.rb', line 103

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/druid/query.rb', line 137

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



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

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

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



120
121
122
# File 'lib/druid/query.rb', line 120

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

#intervals(is) ⇒ Object



124
125
126
127
# File 'lib/druid/query.rb', line 124

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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/druid/query.rb', line 85

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



99
100
101
# File 'lib/druid/query.rb', line 99

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



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

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

#to_jsonObject



160
161
162
# File 'lib/druid/query.rb', line 160

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