Class: Expeditor::RollingNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/expeditor/rolling_number.rb

Overview

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).

Instance Method Summary collapse

Constructor Details

#initialize(size:, per_time:) ⇒ RollingNumber

Returns a new instance of RollingNumber.



10
11
12
13
14
15
16
17
# 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

Instance Method Details

#currentObject

Deprecated.

Don’t use, use ‘#total` instead.



36
37
38
39
40
41
42
# 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) ⇒ Object



20
21
22
23
24
25
# File 'lib/expeditor/rolling_number.rb', line 20

def increment(type)
  @mutex.synchronize do
    update
    @ring.current.increment(type)
  end
end

#totalExpeditor::Status

Returns Newly created status.

Returns:



28
29
30
31
32
33
# 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