Class: Rhcf::Timeseries::Query
- Inherits:
-
Object
- Object
- Rhcf::Timeseries::Query
- Defined in:
- lib/rhcf/timeseries/query.rb
Instance Method Summary collapse
- #better_resolution ⇒ Object
-
#initialize(subject, from, to, series, filter = nil, limit = 1000) ⇒ Query
constructor
A new instance of Query.
- #point_range(resolution_id) ⇒ Object
- #points(resolution_id) ⇒ Object
- #total(resolution_id = nil) ⇒ Object
Constructor Details
#initialize(subject, from, to, series, filter = nil, limit = 1000) ⇒ Query
Returns a new instance of Query.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rhcf/timeseries/query.rb', line 4 def initialize(subject, from, to, series, filter = nil, limit = 1000) from, to = to, from if from > to @series = series @subject = subject @from = from @to = to @filter = filter @limit = limit end |
Instance Method Details
#better_resolution ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rhcf/timeseries/query.rb', line 59 def better_resolution span = @to.to_time - @from.to_time resolutions = @series.resolutions.sort_by{|h| h[:span]}.reverse 5.downto(1) do |div| res = resolutions.find{|r| r[:span] < span / div } return res if res end return nil end |
#point_range(resolution_id) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rhcf/timeseries/query.rb', line 46 def point_range(resolution_id) resolution = @series.resolution(resolution_id) span = resolution[:span] ptr = @from.dup while ptr < @to point = @series.resolution_value_at(ptr, resolution_id) yield point ptr += span.to_i end rescue FloatDomainError # OK end |
#points(resolution_id) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rhcf/timeseries/query.rb', line 28 def points(resolution_id) list =[] point_range(resolution_id) do |point| values = @series.crunch_values(@subject, resolution_id, point, @filter, @limit) next if values.empty? data = {moment: point, values: values } if block_given? yield data else list << data end end list unless block_given? end |
#total(resolution_id = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rhcf/timeseries/query.rb', line 16 def total(resolution_id=nil) accumulator={} points(resolution_id || better_resolution[:id]) do |data| data[:values].each do |key, value| accumulator[key]||=0 accumulator[key]+=value end end accumulator end |