class GerbilCharts::Models::BucketizedTimeSeriesGraphModel

BucketizedTimeSeriedGraphMode

Automatically bucketizes incoming timeseries data into predefined buckets

name

Name of the model

bucketsec

Bucket size in seconds

opts

Model options hash

Attributes

behavior[R]
bucket_size_secs[R]
vartype[R]

Public Class Methods

new(name,bucketsec, opt={:behavior => :average, :vartype => :bits_per_sec }) click to toggle source
Calls superclass method
# 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

Public Instance Methods

add(x_tm,y_val) click to toggle source

add

x_val

A Time object

y_val

Value

This will be bucketized by the model automatically

Calls superclass method
# 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_sweep() click to toggle source

begin a sweep session

# File lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb, line 118
def begin_sweep
    @last_sweep_pos=0
end
bucket_diff(tv1,tv2) click to toggle source

how many buckets separate the two buckettimes

# 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) click to toggle source

accumulate last item

# 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) click to toggle source

insert zero values to represent missing time buckets

# 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) click to toggle source

sweep this bucket

# 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_interval() click to toggle source
# File lib/gerbilcharts/models/bucketized_timeseries_graph_model.rb, line 33
def sweep_interval
    return @bucket_size_secs
end
to_buckettime(tv_in) click to toggle source

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

# 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