Class: GerbilCharts::Models::BucketizedTimeSeriesGraphModel

Inherits:
TimeSeriesGraphModel show all
Defined in:
lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb

Overview

BucketizedTimeSeriedGraphMode

Automatically bucketizes incoming timeseries data into predefined buckets

name

Name of the model

bucketsec

Bucket size in seconds

[opts] Model options hash

Instance Attribute Summary collapse

Attributes inherited from MonotonousGraphModel

#aggregation_factor, #rounderx, #roundery, #xarr, #xrange, #yarr, #yrange

Attributes inherited from GraphModel

#altname, #href, #href_pivot, #name, #transformer, #userdata, #userlabel1, #userlabel2

Instance Method Summary collapse

Methods inherited from TimeSeriesGraphModel

#crop_older, #is_timeseries?, #normalize_time_input

Methods inherited from MonotonousGraphModel

#add_tuples, #clear, #count, #crop_at, #crop_window, #each_tuple, #each_tuple_reverse, #first, #formatted_val, #get_statistical_analysis, #latest, #latest_formatted_val, #latest_val, #latest_x, #latest_x_dbtime, #randomizeLastValue, #ranges, #round_given_x, #round_given_y, #round_ranges, #tuples_since

Methods inherited from GraphModel

#count, #each_value_pair, #hasHref?, #hasHrefPivot?, #hasUserData?, #hasUserTips?, #is_timeseries?, #min_max_x, #min_max_y, #recreate, #setHref, #setHrefPivot, #setUserData, #setUserTip1, #setUserTip2, #updateOptions

Constructor Details

#initialize(name, bucketsec, opt = {:behavior => :average, :vartype => :bits_per_sec }) ⇒ BucketizedTimeSeriesGraphModel

Returns a new instance of BucketizedTimeSeriesGraphModel.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 18

def initialize(name,bucketsec, opt={:behavior => :average, :vartype => :bits_per_sec })
  super(name,opt)
  @bucket_size_secs=bucketsec
  @samp_count =0
  @behavior = opt[:behavior]
  @last_sweep_pos=0
  @vartype = opt[:vartype]
  @aggregation_factor = case opt[:vartype]
when :bits_per_sec ;  @bucket_size_secs / 8.0 
when :per_sec ; @bucket_size_secs
when :counter ; 1.0
else ;  @bucket_size_secs / 8.0 
  end
end

Instance Attribute Details

#behaviorObject (readonly)

:average or :max or :sum



14
15
16
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 14

def behavior
  @behavior
end

#bucket_size_secsObject (readonly)

current bucket size



13
14
15
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 13

def bucket_size_secs
  @bucket_size_secs
end

#last_sweep_posObject (readonly)

:nodoc:



15
16
17
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 15

def last_sweep_pos
  @last_sweep_pos
end

#vartypeObject (readonly)

Returns the value of attribute vartype.



16
17
18
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 16

def vartype
  @vartype
end

Instance Method Details

#add(x_tm, y_val) ⇒ Object

add [x_val] A Time object [y_val] Value

This will be bucketized by the model automatically



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

def add(x_tm,y_val)
  buckettm_in = to_buckettime(x_tm)
  

  # first time
  if @xarr.length==0
      super(buckettm_in,y_val)
      @samp_count=1
      return
  end
    
  # subsequent times
  if buckettm_in== latest_x
      ynew = bucketize(y_val)
  elsif buckettm_in > latest_x 
      npad = bucket_diff(latest_x,buckettm_in)
      if (npad > 1)
          pad_empty_buckets(latest_x,npad-1)
      end
      super(buckettm_in,y_val)
      @samp_count=1
  else
# ignoring out of order data point 
# otherwise pad_empty_buckets will loop endlessly and
# eat up memory ! reported on Forums (check it out) 
  end
end

#begin_sweepObject

begin a sweep session



118
119
120
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 118

def begin_sweep
    @last_sweep_pos=0
end

#bucket_diff(tv1, tv2) ⇒ Object

how many buckets separate the two buckettimes



86
87
88
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 86

def bucket_diff(tv1,tv2)
  return (tv2-tv1).abs / @bucket_size_secs
end

#bucketize(val) ⇒ Object

accumulate last item



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 99

def bucketize(val)
  
  if @behavior == :average 
    cval = @yarr.last
    cval = (cval * @samp_count) + val
    cval /= (@samp_count+1)
    @yarr[@yarr.length-1]=cval
    @samp_count += 1
  elsif @behavior == :maxima
    cval = @yarr.last
    @yarr[@yarr.length-1]=max(cval,val)
  elsif @behavior == :sum
    @yarr[@yarr.length-1]+=val
  end
  return @yarr.last

end

#pad_empty_buckets(tv_first, count) ⇒ Object

insert zero values to represent missing time buckets



91
92
93
94
95
96
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 91

def pad_empty_buckets(tv_first,count)
  for i in (1..count)
    @yarr << 0
    @xarr << tv_first + i*@bucket_size_secs
  end
end

#sweep(tval) ⇒ Object

sweep this bucket



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 123

def sweep(tval)
  
  return 0 if @xarr.length == 0
  return 0 if @last_sweep_pos >= @xarr.length
  
  xv=@xarr[@last_sweep_pos]
  nBucks=bucket_diff(xv,tval)

  if tval < xv
      return 0
  elsif tval == xv || nBucks < 1 
      @last_sweep_pos+=1
      rval = @yarr[@last_sweep_pos-1]
  else 
      @last_sweep_pos+= nBucks
  end    
  return rval.nil? ? 0:rval
  
end

#sweep_intervalObject



33
34
35
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 33

def sweep_interval
    return @bucket_size_secs
end

#to_buckettime(tv_in) ⇒ Object

to_buckettime

[tv] A Time object

returns the time floor of the bucket this belongs to example 8:06 AM will belong to the 8:05AM bucket if bucketsize = 5 min



78
79
80
81
82
83
# File 'lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb', line 78

def to_buckettime(tv_in)
    tv= normalize_time_input(tv_in)
    exp=tv.tv_sec.divmod(@bucket_size_secs)
    
      return Time.at(exp[0]*@bucket_size_secs)
end