Class: UsageTracker

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

Instance Method Summary collapse

Constructor Details

#initialize(monthlyResetDay) ⇒ UsageTracker

Returns a new instance of UsageTracker.



213
214
215
216
217
218
219
220
# File 'lib/quartz_flow/usagetracker.rb', line 213

def initialize(monthlyResetDay)
  @buckets = {}
  @buckets[:daily] = PeriodicBuckets.new(DailyBucketChangeCriteria.new,31)
  #@buckets[:minute] = PeriodicBuckets.new(MinuteBucketChangeCriteria.new,3)
  @buckets[:monthly] = PeriodicBuckets.new(MonthlyBucketChangeCriteria.new(monthlyResetDay),2)
  @usageForAllTimeAdjustment = 0
  loadBucketsFromDatastore
end

Instance Method Details

#allUsage(periodType) ⇒ Object

Returns the usage as of the last time update() was called. This method returns all the tracked usage for the specified period type (:daily or :monthly). The usage is accurate as of the last time update() was called. The returned value is an array of Bucket objects.



249
250
251
# File 'lib/quartz_flow/usagetracker.rb', line 249

def allUsage(periodType)
  getBuckets(periodType).all
end

#currentUsage(periodType) ⇒ Object

This method returns the usage in the current bucket for the specified period type (:daily or :monthly). The usage is accurate as of the last time update() was called. The returned value is a single Bucket object.



240
241
242
# File 'lib/quartz_flow/usagetracker.rb', line 240

def currentUsage(periodType)
  getBuckets(periodType).current
end

#update(usageForAllTime) ⇒ Object

Update the UsageTracker with more usage. The value passed should be the usage since the torrentflow session was created. If a datastore is not used, then this means stopping and starting the session will cause UsageTracking to only track usage for the session. However if Mongo is used then the usage can be saved and persisted between sessions and internally the value passed here is added to the value loaded from Mongo.



228
229
230
231
232
233
234
# File 'lib/quartz_flow/usagetracker.rb', line 228

def update(usageForAllTime)
  usageForAllTime += @usageForAllTimeAdjustment
  @buckets.each do |k,buckets|
    buckets.update usageForAllTime
  end
  saveBucketsToDatastore
end