class UsageTracker

Public Class Methods

new(monthlyResetDay) click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 215
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

Public Instance Methods

allUsage(periodType) click to toggle source

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.

# File lib/quartz_flow/usagetracker.rb, line 251
def allUsage(periodType)
  getBuckets(periodType).all
end
currentUsage(periodType) click to toggle source

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.

# File lib/quartz_flow/usagetracker.rb, line 242
def currentUsage(periodType)
  getBuckets(periodType).current
end
update(usageForAllTime) click to toggle source

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.

# File lib/quartz_flow/usagetracker.rb, line 230
def update(usageForAllTime)
  usageForAllTime += @usageForAllTimeAdjustment
  @buckets.each do |k,buckets|
    buckets.update usageForAllTime
  end
  saveBucketsToDatastore
end

Private Instance Methods

getBuckets(type) click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 256
def getBuckets(type)
  buckets = @buckets[type]
  raise "Unsupported periodType #{periodType.to_s}" if ! buckets
  buckets
end
loadBucketsFromDatastore() click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 279
  def loadBucketsFromDatastore
    @buckets[:daily].fromModel(:daily)
    @buckets[:monthly].fromModel(:monthly)

    # If we are loading from datastore it means that the absolute usage returned from the current torrent session will not
    # contain the usage that we previously tracked, so we must add the old tracked value to what the torrent
    # session reports.
    if @buckets[:daily].current
      @usageForAllTimeAdjustment = @buckets[:daily].current.absoluteUsageAtStartOfBucket + @buckets[:daily].current.value
    end
  
=begin
    if @mongoDb
      $logger.info "Loading usage from Mongo."
      dailyCollection = @mongoDb.collection("daily_usage")
      monthlyCollection = @mongoDb.collection("monthly_usage")

      arr = dailyCollection.find_one
      @buckets[:daily].fromHash arr if arr
      arr = monthlyCollection.find_one
      @buckets[:monthly].fromHash arr if arr

      # If we are loading from Mongo it means that the absolute usage returned from the torrentflow session will not
      # contain the usage that we previously tracked, so we must add the old tracked value to what the torrentflow
      # session reports.
      if @buckets[:daily].current
        @usageForAllTimeAdjustment = @buckets[:daily].current.absoluteUsageAtStartOfBucket + @buckets[:daily].current.value
        $logger.info "Absolute usage at start of current daily bucket: " + @buckets[:daily].current.absoluteUsageAtStartOfBucket.to_s
        $logger.info "Usage in current daily bucket: " + @buckets[:daily].current.value.to_s
        $logger.info "Usage for all time adjustment: " + @usageForAllTimeAdjustment.to_s
      else
        $logger.info "No usage loaded in Mongo (empty collection)."
      end
    else
      $logger.info "Not loading usage from Mongo."
    end
=end
  end
saveBucketsToDatastore() click to toggle source
# File lib/quartz_flow/usagetracker.rb, line 262
  def saveBucketsToDatastore
    @buckets[:daily].toModel(:daily)
    @buckets[:monthly].toModel(:monthly)
=begin
    if @mongoDb
      dailyCollection = @mongoDb.collection("daily_usage")
      monthlyCollection = @mongoDb.collection("monthly_usage")
      # Remove all previous documents
      dailyCollection.remove
      monthlyCollection.remove

      dailyCollection.insert @buckets[:daily].toHash
      monthlyCollection.insert @buckets[:monthly].toHash
    end
=end
  end