Class: Druid::Query
- Inherits:
-
Object
- Object
- Druid::Query
- Defined in:
- lib/druid/query.rb
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Instance Method Summary collapse
- #data_source(source) ⇒ Object
- #filter(hash = nil, &block) ⇒ Object
- #get_query_type ⇒ Object
- #granularity(gran, time_zone = nil) ⇒ Object
- #group_by(*dimensions) ⇒ Object
- #having(&block) ⇒ Object
-
#initialize(source, client = nil) ⇒ Query
constructor
A new instance of Query.
- #interval(from, to = Time.now) ⇒ Object (also: #[])
- #intervals(is) ⇒ Object
- #limit_spec(limit, columns) ⇒ Object
- #postagg(type = :long, &block) ⇒ Object
- #postagg_double(&block) ⇒ Object
- #query_type(type) ⇒ Object
- #send ⇒ Object
- #source ⇒ Object
- #time_series(*aggregations) ⇒ Object
- #to_json ⇒ Object
- #today ⇒ Object
- #topn(dimension, metric, threshold) ⇒ Object
Constructor Details
#initialize(source, client = nil) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/druid/query.rb', line 14 def initialize(source, client = nil) @properties = {} @client = client # set some defaults data_source(source) granularity(:all) interval(today) end |
Instance Attribute Details
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
12 13 14 |
# File 'lib/druid/query.rb', line 12 def properties @properties end |
Instance Method Details
#data_source(source) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/druid/query.rb', line 42 def data_source(source) source = source.split('/') @properties[:dataSource] = source.last @service = source.first self end |
#filter(hash = nil, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/druid/query.rb', line 114 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_type ⇒ Object
38 39 40 |
# File 'lib/druid/query.rb', line 38 def get_query_type() @properties[:queryType] || :groupBy end |
#granularity(gran, time_zone = nil) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/druid/query.rb', line 161 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
53 54 55 56 57 |
# File 'lib/druid/query.rb', line 53 def group_by(*dimensions) query_type(:groupBy) @properties[:dimensions] = dimensions.flatten self end |
#having(&block) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/druid/query.rb', line 140 def having(&block) having = Having.new.instance_exec(&block) if old_having = @properties[:having] if old_having.operator? && old_having.and? new_having = old_having else new_having = HavingOperator.new('and') new_having.add(old_having) end new_having.add(having) else new_having = having end @properties[:having] = new_having self end |
#interval(from, to = Time.now) ⇒ Object Also known as: []
131 132 133 |
# File 'lib/druid/query.rb', line 131 def interval(from, to = Time.now) intervals([[from, to]]) end |
#intervals(is) ⇒ Object
135 136 137 138 |
# File 'lib/druid/query.rb', line 135 def intervals(is) @properties[:intervals] = is.map{ |ii| mk_interval(ii[0], ii[1]) } self end |
#limit_spec(limit, columns) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/druid/query.rb', line 188 def limit_spec(limit, columns) @properties[:limitSpec] = { :type => :default, :limit => limit, :columns => order_by_column_spec(columns) } self end |
#postagg(type = :long, &block) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/druid/query.rb', line 96 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
110 111 112 |
# File 'lib/druid/query.rb', line 110 def postagg_double(&block) postagg(:double, &block) end |
#query_type(type) ⇒ Object
33 34 35 36 |
# File 'lib/druid/query.rb', line 33 def query_type(type) @properties[:queryType] = type self end |
#send ⇒ Object
29 30 31 |
# File 'lib/druid/query.rb', line 29 def send @client.send(self) end |
#source ⇒ Object
49 50 51 |
# File 'lib/druid/query.rb', line 49 def source "#{@service}/#{@properties[:dataSource]}" end |
#time_series(*aggregations) ⇒ Object
67 68 69 70 71 |
# File 'lib/druid/query.rb', line 67 def time_series(*aggregations) query_type(:timeseries) #@properties[:aggregations] = aggregations.flatten self end |
#to_json ⇒ Object
184 185 186 |
# File 'lib/druid/query.rb', line 184 def to_json @properties.to_json end |
#today ⇒ Object
25 26 27 |
# File 'lib/druid/query.rb', line 25 def today Time.now.to_date.to_time end |
#topn(dimension, metric, threshold) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/druid/query.rb', line 59 def topn(dimension, metric, threshold) query_type(:topN) @properties[:dimension] = dimension @properties[:metric] = metric @properties[:threshold] = threshold self end |