Class: Sleek::QueryCommand
- Inherits:
-
Object
- Object
- Sleek::QueryCommand
- Defined in:
- lib/sleek/query_command.rb
Overview
Internal: A query command. It’s primarily responsible for breaking a timeframe into intervals (if applicable), running the query on each sub-timeframe, and wrapping up a result.
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(klass, namespace, bucket, options = {}) ⇒ QueryCommand
constructor
Internal: Initialize the query command.
-
#new_query(timeframe) ⇒ Object
Internal: Instantiate a query object.
-
#run ⇒ Object
Internal: Run the query on each timeframe.
-
#series ⇒ Object
Internal: Split timeframe into sub-timeframes of interval.
-
#series? ⇒ Boolean
Internal: Check if options include interval.
-
#timeframe ⇒ Object
Internal: Parse a time range from the timeframe description.
Constructor Details
#initialize(klass, namespace, bucket, options = {}) ⇒ QueryCommand
Internal: Initialize the query command.
klass - the Sleek::Queries::Query subclass. namespace - the Sleek::Namespace object. bucket - the String bucket name. options - the optional Hash of options. Everything but
:timeframe and :interval will be passed on to the
query class.
:timeframe - the optional timeframe description.
:timezone - the optional TZ identifier.
:interval - the optional interval description. If
passed, requires that :timeframe is passed
as well.
Raises ArgumentError if :interval is passed but :timeframe is not.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sleek/query_command.rb', line 23 def initialize(klass, namespace, bucket, = {}) @klass = klass @namespace = namespace @bucket = bucket @timeframe = .delete(:timeframe) @timezone = .delete(:timezone) @interval = .delete(:interval) @options = if @interval.present? && @timeframe.blank? raise ArgumentError, 'interval requires timeframe' end end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
6 7 8 |
# File 'lib/sleek/query_command.rb', line 6 def bucket @bucket end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/sleek/query_command.rb', line 6 def klass @klass end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
6 7 8 |
# File 'lib/sleek/query_command.rb', line 6 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/sleek/query_command.rb', line 6 def @options end |
Instance Method Details
#new_query(timeframe) ⇒ Object
Internal: Instantiate a query object.
timeframe - the time range.
56 57 58 |
# File 'lib/sleek/query_command.rb', line 56 def new_query(timeframe) klass.new(namespace, bucket, .merge(timeframe: timeframe)) end |
#run ⇒ Object
Internal: Run the query on each timeframe.
61 62 63 64 65 66 67 68 69 |
# File 'lib/sleek/query_command.rb', line 61 def run if series? series.map do |tf| { timeframe: tf, value: new_query(tf).run } end else new_query(timeframe).run end end |
#series ⇒ Object
Internal: Split timeframe into sub-timeframes of interval.
49 50 51 |
# File 'lib/sleek/query_command.rb', line 49 def series Sleek::Interval.new(@interval, timeframe).timeframes end |
#series? ⇒ Boolean
Internal: Check if options include interval.
38 39 40 |
# File 'lib/sleek/query_command.rb', line 38 def series? @interval.present? end |