class Sleek::Queries::Query

Public: The query.

Queries are performed on a set of events and usually return numeric values. You shouldn't be using Sleek::Queries::Query directly, instead, you should subclass it and define `#perform` on it, which takes an events criteria and does its job.

Sleek::Queries::Query would take care of processing options, filtering events, handling series and groups.

Examples

class SomeQuery < Query
  def perform(events)
    ...
  end
end

Attributes

bucket[R]
namespace[R]
options[R]
timeframe[R]

Public Class Methods

new(namespace, bucket, options = {}) click to toggle source

Internal: Initialize the query.

namespace - the Sleek::Namespace object. bucket - the String bucket name. options - the optional Hash of options.

:timeframe - the optional timeframe description.
:interval  - the optional interval description.

Raises ArgumentError if passed options are invalid.

# File lib/sleek/queries/query.rb, line 34
def initialize(namespace, bucket, options = {})
  @namespace = namespace
  @bucket = bucket
  @options = options
  @timeframe = options[:timeframe]

  raise ArgumentError, 'options are invalid' unless valid_options?
end
require_target_property!() click to toggle source

Public: Indicate that the query requires target property.

Examples

class SomeQuery < Query
  require_target_property!

  def perform(events)
    ...
  end
end
# File lib/sleek/queries/query.rb, line 127
def require_target_property!
  @require_target_property = true
end
require_target_property?() click to toggle source

Public: Check if the query requires target property.

# File lib/sleek/queries/query.rb, line 132
def require_target_property?
  !!@require_target_property
end

Public Instance Methods

apply_filters(criteria) click to toggle source

Internal: Apply all the filters to the criteria.

# File lib/sleek/queries/query.rb, line 59
def apply_filters(criteria)
  filters.reduce(criteria) { |crit, filter| filter.apply(crit) }
end
events() click to toggle source

Internal: Get Mongoid::Criteria for events to perform the query.

time_range - the optional range of Time objects.

# File lib/sleek/queries/query.rb, line 46
def events
  evts = namespace.events(bucket)
  evts = evts.between('s.t' => timeframe) if timeframe?
  evts = apply_filters(evts) if filter?

  if group_by.present?
    evts = Sleek::GroupByCriteria.new(evts, "d.#{group_by}")
  end

  evts
end
filter?() click to toggle source

Internal: Check if options include filter.

# File lib/sleek/queries/query.rb, line 77
def filter?
  options[:filter].present?
end
filters() click to toggle source

Internal: Get filters.

# File lib/sleek/queries/query.rb, line 64
def filters
  filters = options[:filter]

  if filters.is_a?(Array) && filters.size == 3 && filters.none? { |f| f.is_a?(Array) }
    filters = [filters]
  elsif !filters.is_a?(Array) || !filters.all? { |f| f.is_a?(Array) && f.size == 3 }
    raise ArgumentError, "wrong filter - #{filters}"
  end

  filters.map { |f| Sleek::Filter.new(*f) }
end
group_by() click to toggle source

Internal: Get group_by property.

# File lib/sleek/queries/query.rb, line 87
def group_by
  options[:group_by]
end
perform(events) click to toggle source

Internal: Perform the query on a set of events.

# File lib/sleek/queries/query.rb, line 104
def perform(events)
  raise NotImplementedError
end
run() click to toggle source

Internal: Run the query.

# File lib/sleek/queries/query.rb, line 99
def run
  perform(events)
end
target_property() click to toggle source

Internal: Get the target property.

# File lib/sleek/queries/query.rb, line 92
def target_property
  if options[:target_property].present?
    "d.#{options[:target_property]}"
  end
end
timeframe?() click to toggle source

Internal: Check if options include timeframe.

# File lib/sleek/queries/query.rb, line 82
def timeframe?
  timeframe.present?
end
valid_options?() click to toggle source

Internal: Validate the options.

# File lib/sleek/queries/query.rb, line 109
def valid_options?
  options.is_a?(Hash) &&
    (filter? ? options[:filter].is_a?(Array) : true) &&
    (require_target_property? ? options[:target_property].present? : true)
end