Class: SAAL::ChartDataRange

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_data.rb

Constant Summary collapse

ALIGN =
{:years => [12,31,23,59,59],
:months => [31,23,59,59],
:days => [23,59,59],
:weeks => [23,59,59],
:hours => [59,59]}
NUMHOURS =
{:hours => 1, :days => 24, :weeks => 24*7}
DAYNAMES =
["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
MONTHNAMES =
["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ChartDataRange

Returns a new instance of ChartDataRange.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chart_data.rb', line 14

def initialize(opts={})
  last = opts[:last] || opts['last'].to_i
  periods = opts[:periods] || (opts['periods'] ? opts['periods'].to_sym : nil)
  @now = opts[:now] || Time.now.utc
  if last && periods
    @num = last
    @periods = periods
    calc_alignment
  else
    @from = opts[:from] || 0
    @to = opts[:to] || @now
  end
end

Instance Attribute Details

#numObject (readonly)

Returns the value of attribute num.



13
14
15
# File 'lib/chart_data.rb', line 13

def num
  @num
end

#periodsObject (readonly)

Returns the value of attribute periods.



13
14
15
# File 'lib/chart_data.rb', line 13

def periods
  @periods
end

Instance Method Details

#fromObject



28
29
30
# File 'lib/chart_data.rb', line 28

def from
  @from.to_i
end

#get_data(method, sensor, num) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/chart_data.rb', line 36

def get_data(method, sensor, num)
  step = (@to - @from).to_i/num
  t = @from - 1
  (0..num-2).map do |i|
    f = t + 1
    t = (f+step)
    v = sensor.send(method, f.to_i, t.to_i)
  end << sensor.send(method, (t+1).to_i, to.to_i)
end

#periodnamesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chart_data.rb', line 46

def periodnames
  if !@num
    raise RuntimeError, 
          "Trying to get periodnames without a :last & :periods definition" 
  end

  case @periods
  when :hours
    (0...@num).map{|i| ((@now.getlocal - i*3600).hour).to_s}.reverse
  when :days
    (1..@num).map{|i| (@now.wday - i)%7}.map{|w| DAYNAMES[w]}.reverse
  when :weeks
    initial = @now - (@now.wday-1)*24*60*60
    (0...@num).map do |i| 
      time = Time.at(initial - i*24*60*60*7)
      time.day.to_s+" "+ MONTHNAMES[time.month-1]
    end.reverse
  when :months
    (1..@num).map{|i| (@now.month - i)%12}.map{|m| MONTHNAMES[m]}.reverse
  when :years
    (0...@num).map{|i| (@now.year - i).to_s}.reverse
  else
    raise RuntimeError, "No such period type #{@periods}" 
  end
end

#toObject



32
33
34
# File 'lib/chart_data.rb', line 32

def to
  @to.to_i
end