Class: Sleek::Queries::Query
- Inherits:
-
Object
- Object
- Sleek::Queries::Query
- Defined in:
- lib/sleek/queries/query.rb
Overview
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
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#timeframe ⇒ Object
readonly
Returns the value of attribute timeframe.
Class Method Summary collapse
-
.require_target_property! ⇒ Object
Public: Indicate that the query requires target property.
-
.require_target_property? ⇒ Boolean
Public: Check if the query requires target property.
Instance Method Summary collapse
-
#apply_filters(criteria) ⇒ Object
Internal: Apply all the filters to the criteria.
-
#events ⇒ Object
Internal: Get Mongoid::Criteria for events to perform the query.
-
#filter? ⇒ Boolean
Internal: Check if options include filter.
-
#filters ⇒ Object
Internal: Get filters.
-
#group_by ⇒ Object
Internal: Get group_by property.
-
#initialize(namespace, bucket, options = {}) ⇒ Query
constructor
Internal: Initialize the query.
-
#perform(events) ⇒ Object
Internal: Perform the query on a set of events.
-
#run ⇒ Object
Internal: Run the query.
-
#target_property ⇒ Object
Internal: Get the target property.
-
#timeframe? ⇒ Boolean
Internal: Check if options include timeframe.
-
#valid_options? ⇒ Boolean
Internal: Validate the options.
Constructor Details
#initialize(namespace, bucket, options = {}) ⇒ Query
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.
34 35 36 37 38 39 40 41 |
# File 'lib/sleek/queries/query.rb', line 34 def initialize(namespace, bucket, = {}) @namespace = namespace @bucket = bucket = @timeframe = [:timeframe] raise ArgumentError, 'options are invalid' unless end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
21 22 23 |
# File 'lib/sleek/queries/query.rb', line 21 def bucket @bucket end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/sleek/queries/query.rb', line 21 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/sleek/queries/query.rb', line 21 def end |
#timeframe ⇒ Object (readonly)
Returns the value of attribute timeframe.
21 22 23 |
# File 'lib/sleek/queries/query.rb', line 21 def timeframe @timeframe end |
Class Method Details
.require_target_property! ⇒ Object
Public: Indicate that the query requires target property.
Examples
class SomeQuery < Query
require_target_property!
def perform(events)
...
end
end
127 128 129 |
# File 'lib/sleek/queries/query.rb', line 127 def require_target_property! @require_target_property = true end |
.require_target_property? ⇒ Boolean
Public: Check if the query requires target property.
132 133 134 |
# File 'lib/sleek/queries/query.rb', line 132 def require_target_property? !!@require_target_property end |
Instance Method Details
#apply_filters(criteria) ⇒ Object
Internal: Apply all the filters to the criteria.
59 60 61 |
# File 'lib/sleek/queries/query.rb', line 59 def apply_filters(criteria) filters.reduce(criteria) { |crit, filter| filter.apply(crit) } end |
#events ⇒ Object
Internal: Get Mongoid::Criteria for events to perform the query.
time_range - the optional range of Time objects.
46 47 48 49 50 51 52 53 54 55 56 |
# 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? ⇒ Boolean
Internal: Check if options include filter.
77 78 79 |
# File 'lib/sleek/queries/query.rb', line 77 def filter? [:filter].present? end |
#filters ⇒ Object
Internal: Get filters.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sleek/queries/query.rb', line 64 def filters filters = [: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 ⇒ Object
Internal: Get group_by property.
87 88 89 |
# File 'lib/sleek/queries/query.rb', line 87 def group_by [:group_by] end |
#perform(events) ⇒ Object
Internal: Perform the query on a set of events.
104 105 106 |
# File 'lib/sleek/queries/query.rb', line 104 def perform(events) raise NotImplementedError end |
#run ⇒ Object
Internal: Run the query.
99 100 101 |
# File 'lib/sleek/queries/query.rb', line 99 def run perform(events) end |
#target_property ⇒ Object
Internal: Get the target property.
92 93 94 95 96 |
# File 'lib/sleek/queries/query.rb', line 92 def target_property if [:target_property].present? "d.#{options[:target_property]}" end end |
#timeframe? ⇒ Boolean
Internal: Check if options include timeframe.
82 83 84 |
# File 'lib/sleek/queries/query.rb', line 82 def timeframe? timeframe.present? end |
#valid_options? ⇒ Boolean
Internal: Validate the options.
109 110 111 112 113 |
# File 'lib/sleek/queries/query.rb', line 109 def .is_a?(Hash) && (filter? ? [:filter].is_a?(Array) : true) && (require_target_property? ? [:target_property].present? : true) end |