class PeriodicBuckets
Public Class Methods
new(criteria, maxBuckets = nil)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 65 def initialize(criteria, maxBuckets = nil) setBucketChangeCriteria(criteria) @buckets = [] @maxBuckets = maxBuckets @maxBuckets = 1 if @maxBuckets && @maxBuckets < 1 end
Public Instance Methods
all()
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 101 def all @buckets end
current(absoluteUsage = nil)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 97 def current(absoluteUsage = nil) @buckets.last end
fromHash(hash)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 113 def fromHash(hash) @buckets = [] hash["buckets"].each do |b| bucket = Bucket.new(nil, nil, nil) bucket.fromHash b @buckets.push bucket end end
fromModel(type)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 122 def fromModel(type) @buckets = [] buckets = UsageBucket.all(:type => type, :order => [:index.asc]) buckets.each do |model| bucket = Bucket.new(nil, nil, nil) bucket.fromModel model @buckets.push bucket end end
setBucketChangeCriteria(criteria)
click to toggle source
Set the criteria that determines when the current bucket is full and we should make a new empty bucket the current bucket, and that is used to set the label for the new bucket.
# File lib/quartz_flow/usagetracker.rb, line 75 def setBucketChangeCriteria(criteria) @bucketChangeCriteria = criteria end
toHash()
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 105 def toHash array = [] @buckets.each do |b| array.push b.toHash end { "buckets" => array } end
toModel(type)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 132 def toModel(type) UsageBucket.all(:type => type).destroy! index = 0 @buckets.each do |b| model = b.toModel model.type = type model.index = index index += 1 model.save end end
update(absoluteUsage = nil)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 79 def update(absoluteUsage = nil) if @buckets.size == 0 prev = nil @buckets.push @bucketChangeCriteria.newBucket setAbsoluteUsage(prev, @buckets.last, absoluteUsage) if absoluteUsage else prev = @buckets.last # Time for a new bucket? if @bucketChangeCriteria.newBucket?(@buckets.last) @buckets.push @bucketChangeCriteria.newBucket setAbsoluteUsage(prev, @buckets.last, absoluteUsage) if absoluteUsage end @buckets.shift if @maxBuckets && @buckets.size > @maxBuckets end setValue(@buckets.last, absoluteUsage) if absoluteUsage end
Private Instance Methods
setAbsoluteUsage(previousBucket, newBucket, absoluteUsage)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 145 def setAbsoluteUsage(previousBucket, newBucket, absoluteUsage) if previousBucket newBucket.absoluteUsageAtStartOfBucket = previousBucket.absoluteUsageAtStartOfBucket + previousBucket.value else newBucket.absoluteUsageAtStartOfBucket = absoluteUsage end end
setValue(newBucket, absoluteUsage)
click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 152 def setValue(newBucket, absoluteUsage) newBucket.value = absoluteUsage - newBucket.absoluteUsageAtStartOfBucket end