class GerbilCharts::Models::MonotonousGraphModel

MonotonousGraphModel

simple graph model (x increases monotonously) All timeseries models are derived from this class

Attributes

aggregation_factor[R]
rounderx[R]
roundery[R]
xarr[R]
xrange[R]
yarr[R]
yrange[R]

Public Class Methods

new(name,opt={}) click to toggle source
Calls superclass method GerbilCharts::Models::GraphModel::new
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 15
def initialize(name,opt={})
  super(name)
  @xrange = RawRange.new
  @yrange = RawRange.new
  @rounderx = RoundRange
  @roundery = RoundRange
  @xarr=[]
  @yarr=[]
      @aggregation_factor=1
end

Public Instance Methods

add(x_val,y_val) click to toggle source

add a tuple, apply a transformer lambda is supplied by the user

A new datapoint must have a x_val greater than the previous one

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 29
def add(x_val,y_val)

  if @xarr.length > 0 and @xarr.last > x_val
        # ignoring out of order data
    # raise "Expecting monotonous series data"
        return 
  end

      # x updates
  @xrange.update x_val
  @xarr << x_val 
  
      # y updates
  y_val = @transformer.xform(y_val) if @transformer
  @yarr << y_val
  @yrange.update y_val
   
end
add_tuples(tarr) click to toggle source

add an array of values at once (just a convenience method)

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 49
def add_tuples tarr
  tarr.each do |t|
    add t[0],t[1]
  end
end
clear() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 55
def clear
  @xrange.reset
  @yrange.reset
  @xarr=[]
  @yarr=[]
end
count() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 62
def count
  @xarr.size
end
crop_at(cutoff) click to toggle source

crop all tuples less than an absolute cutoff

cutoff

Cutoff value

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 139
def crop_at(cutoff)
  until @xarr.empty? or @xarr.first >= cutoff
    @xarr.shift
    @yarr.shift
  end
      recompute_ranges
end
crop_window(cutoffwindow) click to toggle source

crop all tuples older than a cutoff window

cutoffwindow

older than a number of seconds

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 131
def crop_window(cutoffwindow)
  xcutoff = latest_x - cutoffwindow
      crop_at(xcutoff)
end
each_tuple() { |xarr,yarr| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 115
def each_tuple
  for i in (0 .. @xarr.length-1)
    yield @xarr[i],@yarr[i]
  end
end
each_tuple_reverse() { |xarr,yarr| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 121
def each_tuple_reverse
      @xarr.length.downto(1) do  |i|
    yield @xarr[i-1],@yarr[i-1]
  end
end
first() { |xarr,yarr| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 82
def first
  yield @xarr[0],@yarr[0]
end
formatted_val(rawval) click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 90
def formatted_val rawval
  return @yrange.format_value(rawval)
end
get_statistical_analysis() click to toggle source

statistical analysis

standard deviation and 95th percentile go here

mode => :normal or :ninety_fifth

returns array 0 = min, 1 = max, 2 = avg, 3 = total, 4 = latest, 5=95th

todo :  5 = stddev  
todo : support :ninety_fifth
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 170
def get_statistical_analysis()
  return [0,0,0,0,0,0] if @yarr.size==0

  a = []
  a[0]= @yrange.rmin
  a[1]= @yrange.rmax
  a[3]=0

  @yarr.each do |v|
    a[3] = a[3] + v
  end
  a[2] = a[3]/@yarr.size
  a[3] = a[3] * @aggregation_factor
  a[4] = latest_val

  # 95th statistical
  rank=(@yarr.size * 0.95).round 
  a[5] = @yarr.sort()[rank-1] 
  
  return a
end
latest() { |last, last| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 66
def latest
  yield @xarr.last, @yarr.last
end
latest_formatted_val() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 86
def latest_formatted_val
  return @yrange.format_value(@yarr.last)
end
latest_val() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 74
def latest_val
  return @yarr.last
end
latest_x() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 70
def latest_x
  return @xarr.last
end
latest_x_dbtime() click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 78
def latest_x_dbtime
  return (@xarr.last.tv_sec) << 32
end
randomizeLastValue() click to toggle source

for test purposes

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 156
def randomizeLastValue
  v=@yarr.last
  @yarr[@yarr.size-1]=v * ( 0.5 + rand())
end
ranges() { |xrange, yrange| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 94
def ranges
  yield @xrange, @yrange if block_given?
      return @xrange,@yrange
end
round_given_x(rx) click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 107
def round_given_x(rx)
  return @rounderx.new(rx)
end
round_given_y(ry) click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 111
def round_given_y(ry)
  return @roundery.new(ry)
end
round_ranges() { |rounderx, roundery| ... } click to toggle source
# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 99
def round_ranges
  if block_given?
   yield @rounderx.new(@xrange), @roundery.new(@yrange) 
      else
   return @rounderx.new(@xrange), @roundery.new(@yrange)
      end
end
tuples_since(x_last) { |xarr,yarr| ... } click to toggle source

all data points newer than a cut off

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 148
def tuples_since(x_last)
    istart = binarySearch(@xarr,x_last,0,@xarr.length)
    for i in istart..@xarr.length-1
      yield @xarr[i],@yarr[i]
    end
end

Private Instance Methods

binarySearch(array, target, i, n) click to toggle source

rudimentary binary search

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 206
def binarySearch(array, target, i, n)
  case n
  when 0
      raise ArgumentError
  when 1
      if array[i] == target
          return i
      elsif i+1 < array.length and array[i] < target && array[i+1]>target
          return i
      else
          raise ArgumentError
      end
  else
      j = i + n / 2
      if array[j] <= target
          return binarySearch(array, target, j, n - n/2)
      else
          return binarySearch(array, target, i, n/2)
      end
  end
end
recompute_ranges() click to toggle source

recompute ranges

# File lib/gerbilcharts/models/monotonous_graph_model.rb, line 194
def recompute_ranges
      @xrange.reset
      @yrange.reset

      each_tuple do |x,y|
              @xrange.update x
              @yrange.update y
      end

end