class SAAL::ChartDataRange
Constants
- ALIGN
- DAYNAMES
- MONTHNAMES
- NUMHOURS
Attributes
num[R]
periods[R]
Public Class Methods
new(opts={})
click to toggle source
# File lib/chart_data.rb 14 def initialize(opts={}) 15 last = opts[:last] || opts['last'].to_i 16 periods = opts[:periods] || (opts['periods'] ? opts['periods'].to_sym : nil) 17 @now = opts[:now] || Time.now.utc 18 if last && periods 19 @num = last 20 @periods = periods 21 calc_alignment 22 else 23 @from = opts[:from] || 0 24 @to = opts[:to] || @now 25 end 26 end
Public Instance Methods
from()
click to toggle source
# File lib/chart_data.rb 28 def from 29 @from.to_i 30 end
get_data(method, sensor, num)
click to toggle source
# File lib/chart_data.rb 36 def get_data(method, sensor, num) 37 step = (@to - @from).to_i/num 38 t = @from - 1 39 (0..num-2).map do |i| 40 f = t + 1 41 t = (f+step) 42 _v = sensor.send(method, f.to_i, t.to_i) 43 end << sensor.send(method, (t+1).to_i, to.to_i) 44 end
periodnames()
click to toggle source
# File lib/chart_data.rb 46 def periodnames 47 if !@num 48 raise RuntimeError, 49 "Trying to get periodnames without a :last & :periods definition" 50 end 51 52 case @periods 53 when :hours 54 (0...@num).map{|i| ((@now.getlocal - i*3600).hour).to_s}.reverse 55 when :days 56 (1..@num).map{|i| (@now.wday - i)%7}.map{|w| DAYNAMES[w]}.reverse 57 when :weeks 58 initial = @now - (@now.wday-1)*24*60*60 59 (0...@num).map do |i| 60 time = Time.at(initial - i*24*60*60*7) 61 time.day.to_s+" "+ MONTHNAMES[time.month-1] 62 end.reverse 63 when :months 64 (1..@num).map{|i| (@now.month - i)%12}.map{|m| MONTHNAMES[m]}.reverse 65 when :years 66 (0...@num).map{|i| (@now.year - i).to_s}.reverse 67 else 68 raise RuntimeError, "No such period type #{@periods}" 69 end 70 end
to()
click to toggle source
# File lib/chart_data.rb 32 def to 33 @to.to_i 34 end
Private Instance Methods
calc_alignment()
click to toggle source
# File lib/chart_data.rb 73 def calc_alignment 74 if [:years, :year].include? periods 75 # Calculate by date manipulation 76 from = Time.utc(@now.year - num + 1, 1, 1, 0, 0, 0) 77 to = Time.utc(@now.year, 12, 31, 23, 59, 59) 78 elsif [:months, :month].include? periods 79 # advance to the 1st of the next month 80 newm = @now.month%12 + 1 81 newy = @now.year + (@now.month == 12 ? 1 : 0) 82 to = Time.utc(newy, newm, 1, 0, 0, 0) 83 # Go back num months for from 84 from = dec_months(num, to) 85 # subtract 1 second from to to get the end of current month 86 to -= 1 87 else 88 # Calculate by elasped time 89 args = [@now.year, @now.month, @now.day, @now.hour, @now.min, @now.sec] 90 args = args[0..-(ALIGN[periods].size+1)] 91 args += ALIGN[periods] 92 to = Time.utc(*args) 93 to += (7-@now.wday)*60*60*24 if [:weeks,:week].include?(periods) 94 from = to - NUMHOURS[periods]*60*60*num+1 95 end 96 @from = from 97 @to = to 98 end
dec_months(num, time)
click to toggle source
Subtract num months from a given Time
# File lib/chart_data.rb 101 def dec_months(num, time) 102 # Go back any 12 month intervals (aka years) 103 newy = time.year - num/12 104 num = num%12 105 # Go back the remainder months 106 newm = time.month - num 107 if newm < 1 108 newm = 12 - (-newm) 109 newy -= 1 110 end 111 Time.utc(newy, newm, time.day, time.hour, time.min, time.sec) 112 end