Class: Sleek::QueryCommand

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @klass = klass
  @namespace = namespace
  @bucket = bucket
  @timeframe = options.delete(:timeframe)
  @timezone = options.delete(:timezone)
  @interval = options.delete(:interval)
  @options = options

  if @interval.present? && @timeframe.blank?
    raise ArgumentError, 'interval requires timeframe'
  end
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



6
7
8
# File 'lib/sleek/query_command.rb', line 6

def bucket
  @bucket
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/sleek/query_command.rb', line 6

def klass
  @klass
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/sleek/query_command.rb', line 6

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/sleek/query_command.rb', line 6

def options
  @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, options.merge(timeframe: timeframe))
end

#runObject

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

#seriesObject

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.

Returns:

  • (Boolean)


38
39
40
# File 'lib/sleek/query_command.rb', line 38

def series?
  @interval.present?
end

#timeframeObject

Internal: Parse a time range from the timeframe description. description.



44
45
46
# File 'lib/sleek/query_command.rb', line 44

def timeframe
  Sleek::Timeframe.to_range(@timeframe, @timezone) if @timeframe
end