Class: PeriodicBuckets

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

Instance Method Summary collapse

Constructor Details

#initialize(criteria, maxBuckets = nil) ⇒ PeriodicBuckets

Returns a new instance of PeriodicBuckets.



65
66
67
68
69
70
# 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

Instance Method Details

#allObject



101
102
103
# File 'lib/quartz_flow/usagetracker.rb', line 101

def all
  @buckets
end

#current(absoluteUsage = nil) ⇒ Object



97
98
99
# File 'lib/quartz_flow/usagetracker.rb', line 97

def current(absoluteUsage = nil)
  @buckets.last
end

#fromHash(hash) ⇒ Object



113
114
115
116
117
118
119
120
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
# 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) ⇒ Object

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.



75
76
77
# File 'lib/quartz_flow/usagetracker.rb', line 75

def setBucketChangeCriteria(criteria)
  @bucketChangeCriteria = criteria
end

#toHashObject



105
106
107
108
109
110
111
# 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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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