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
Public Class Methods
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
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
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
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
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
Internal: Check if options include filter.
# File lib/sleek/queries/query.rb, line 77 def filter? options[:filter].present? end
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
Internal: Get group_by
property.
# File lib/sleek/queries/query.rb, line 87 def group_by options[:group_by] end
Internal: Perform the query on a set of events.
# File lib/sleek/queries/query.rb, line 104 def perform(events) raise NotImplementedError end
Internal: Run the query.
# File lib/sleek/queries/query.rb, line 99 def run perform(events) end
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
Internal: Check if options include timeframe.
# File lib/sleek/queries/query.rb, line 82 def timeframe? timeframe.present? end
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