class Druid::Query::Builder

Attributes

query[R]

Public Class Methods

new() click to toggle source
# File lib/druid/query.rb, line 305
def initialize
  @query = Query.new
  query_type(:timeseries)
  interval(Time.now.utc.beginning_of_day)
end

Public Instance Methods

cardinality(metric, dimensions, by_row = false) click to toggle source
# File lib/druid/query.rb, line 432
def cardinality(metric, dimensions, by_row = false)
  @query.aggregations << Aggregation.new({
    type: 'cardinality',
    name: metric,
    fieldNames: dimensions,
    byRow: by_row,
  }) unless @query.contains_aggregation?(metric)
  self
end
chain_having(having) click to toggle source
# File lib/druid/query.rb, line 515
def chain_having(having)
  having = @query.having.chain(having) if @query.having
  @query.having = having
  self
end
data_source(source) click to toggle source
# File lib/druid/query.rb, line 316
def data_source(source)
  @query.dataSource = source.split('/').last
  self
end
filter(hash = nil, type = :in, &block) click to toggle source

filters

# File lib/druid/query.rb, line 479
def filter(hash = nil, type = :in, &block)
  filter_from_hash(hash, type) if hash
  filter_from_block(&block) if block
  self
end
filter_from_block(&block) click to toggle source
# File lib/druid/query.rb, line 494
def filter_from_block(&block)
  filter = Filter.new.instance_exec(&block)
  @query.filter = @query.filter ? @query.filter.&(filter) : filter
end
filter_from_hash(hash, type = :in) click to toggle source
# File lib/druid/query.rb, line 485
def filter_from_hash(hash, type = :in)
  last = nil
  hash.each do |k, values|
    filter = DimensionFilter.new(dimension: k).__send__(type, values)
    last = last ? last.&(filter) : filter
  end
  @query.filter = @query.filter ? @query.filter.&(last) : last
end
filtered_aggregation(metric, name, aggregation_type, &filter) click to toggle source
# File lib/druid/query.rb, line 454
def filtered_aggregation(metric, name, aggregation_type, &filter)
  @query.aggregations << Aggregation.new(
    type: 'filtered',
    filter: Filter.new.instance_exec(&filter),
    aggregator: Aggregation.new(
      type: aggregation_type.to_s.camelize(:lower),
      name: name,
      fieldName: metric
    )
  ) unless @query.contains_aggregation?(name)
  self
end
granularity(gran, time_zone = "UTC") click to toggle source
# File lib/druid/query.rb, line 338
def granularity(gran, time_zone = "UTC")
  gran = gran.to_s
  if %w(all none minute fifteen_minute thirty_minute hour day).include?(gran)
    @query.granularity = gran
  else
    @query.granularity = Granularity.new({
      type: 'period',
      period: gran,
      timeZone: time_zone
    })
  end
  self
end
group_by(*dimensions) click to toggle source
# File lib/druid/query.rb, line 366
def group_by(*dimensions)
  query_type(:groupBy)
  @query.dimensions = dimensions.flatten.map do |dimension|
    dimension.is_a?(Dimension) ? dimension : Dimension.new(dimension)
  end
  self
end
having(hash = nil, &block) click to toggle source

having

# File lib/druid/query.rb, line 501
def having(hash = nil, &block)
  having_from_hash(hash) if hash
  having_from_block(&block) if block
  self
end
having_from_block(&block) click to toggle source
# File lib/druid/query.rb, line 507
def having_from_block(&block)
  chain_having(Having.new.instance_exec(&block))
end
having_from_hash(h) click to toggle source
# File lib/druid/query.rb, line 511
def having_from_hash(h)
  chain_having(Having.new(h))
end
histogram(metric, type = "equalBuckets", args = {}) click to toggle source
# File lib/druid/query.rb, line 414
def histogram(metric, type = "equalBuckets", args = {})
  @query.aggregations << Aggregation.new({
    type: "approxHistogramFold",
    name: "raw_#{metric}",
    fieldName: metric,
  })
  type = type.dup
  type[0] = type[0].upcase
  options = args.dup.merge({
    name: metric,
    fieldName: "raw_#{metric}"
  })
  @query.postAggregations << ::Druid.const_get("PostAggregationHistogram#{type}").new(options)
  self
end
histograms(metrics) click to toggle source
# File lib/druid/query.rb, line 409
def histograms(metrics)
  metrics.each{|m| histogram(m) }
  self
end
interval(from, to = Time.now) click to toggle source
# File lib/druid/query.rb, line 321
def interval(from, to = Time.now)
  intervals([[from, to]])
end
intervals(is) click to toggle source
# File lib/druid/query.rb, line 325
def intervals(is)
  @query.intervals = is.map do |from, to|
    from = from.respond_to?(:iso8601) ? from.iso8601 : ISO8601::DateTime.new(from).to_s
    to = to.respond_to?(:iso8601) ? to.iso8601 : ISO8601::DateTime.new(to).to_s
    "#{from}/#{to}"
  end
  self
end
js_aggregation(metric, columns, functions) click to toggle source
# File lib/druid/query.rb, line 442
def js_aggregation(metric, columns, functions)
  @query.aggregations << Aggregation.new({
    type: 'javascript',
    name: metric,
    fieldNames: columns,
    fnAggregate: functions[:aggregate],
    fnCombine: functions[:combine],
    fnReset: functions[:reset],
  }) unless @query.contains_aggregation?(metric)
  self
end
last(duration) click to toggle source
# File lib/druid/query.rb, line 334
def last(duration)
  interval(Time.now - duration)
end
limit(limit, columns) click to toggle source

limit/sort

# File lib/druid/query.rb, line 523
def limit(limit, columns)
  @query.limitSpec = {
    type: :default,
    limit: limit,
    columns: columns.map do |dimension, direction|
      { dimension: dimension, direction: direction }
    end
  }
  self
end
metadata() click to toggle source

query types

# File lib/druid/query.rb, line 354
def metadata
  query_type(:segmentMetadata)
  @query.context.useCache = false
  @query.context.populateCache = false
  self
end
postagg(type = :long_sum, &block) click to toggle source

post aggregations

# File lib/druid/query.rb, line 469
def postagg(type = :long_sum, &block)
  post_agg = PostAggregation.new.instance_exec(&block)
  @query.postAggregations << post_agg
  # make sure, the required fields are in the query
  self.method(type).call(post_agg.field_names)
  self
end
query_type(type) click to toggle source
# File lib/druid/query.rb, line 311
def query_type(type)
  @query.queryType = type.to_s
  self
end
timeseries() click to toggle source
# File lib/druid/query.rb, line 361
def timeseries
  query_type(:timeseries)
  self
end
topn(dimension, metric, threshold) click to toggle source
# File lib/druid/query.rb, line 374
def topn(dimension, metric, threshold)
  query_type(:topN)
  @query.dimension = dimension
  @query.metric = metric
  @query.threshold = threshold
  self
end