class Expeditor::RollingNumber

A RollingNumber holds some Status objects and it rolls statuses each `per_time` (default is 1 second). This is done so that the statistics are recorded gradually with short time interval rahter than reset all the record every wide time range (default is 10 seconds).

Public Class Methods

new(size:, per_time:) click to toggle source
# File lib/expeditor/rolling_number.rb, line 10
def initialize(size:, per_time:)
  @mutex = Mutex.new
  @ring = RingBuffer.new(size) do
    Expeditor::Status.new
  end
  @per_time = per_time
  @current_start = Time.now
end

Public Instance Methods

current() click to toggle source

@deprecated Don't use, use `#total` instead.

# File lib/expeditor/rolling_number.rb, line 36
def current
  warn 'Expeditor::RollingNumber#current is deprecated. Please use #total instead to fetch correct status object.'
  @mutex.synchronize do
    update
    @ring.current
  end
end
increment(type) click to toggle source

@params [Symbol] type

# File lib/expeditor/rolling_number.rb, line 20
def increment(type)
  @mutex.synchronize do
    update
    @ring.current.increment(type)
  end
end
total() click to toggle source

@return [Expeditor::Status] Newly created status

# File lib/expeditor/rolling_number.rb, line 28
def total
  @mutex.synchronize do
    update
    @ring.all.inject(Expeditor::Status.new) {|i, s| i.merge!(s) }
  end
end

Private Instance Methods

last_passing() click to toggle source
# File lib/expeditor/rolling_number.rb, line 54
def last_passing
  (Time.now - @current_start).div(@per_time)
end
update() click to toggle source
# File lib/expeditor/rolling_number.rb, line 46
def update
  passing = last_passing
  if passing > 0
    @current_start = @current_start + @per_time * passing
    @ring.move(passing)
  end
end