Class: Async::Container::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/statistics.rb

Overview

Tracks various statistics relating to child instances in a container.

Defined Under Namespace

Classes: Rate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window: 60) ⇒ Statistics

Initialize the statistics all to 0.



77
78
79
80
81
82
83
84
# File 'lib/async/container/statistics.rb', line 77

def initialize(window: 60)
	@spawns = 0
	@restarts = 0
	@failures = 0
	
	@restart_rate = Rate.new(window: window)
	@failure_rate = Rate.new(window: window)
end

Instance Attribute Details

#failure_rateObject

Get the failure rate tracker.



121
122
123
# File 'lib/async/container/statistics.rb', line 121

def failure_rate
  @failure_rate
end

#failuresObject (readonly)

How many child instances have failed.



96
97
98
# File 'lib/async/container/statistics.rb', line 96

def failures
  @failures
end

#restart_rateObject

Get the restart rate tracker.



117
118
119
# File 'lib/async/container/statistics.rb', line 117

def restart_rate
  @restart_rate
end

#restartsObject (readonly)

How many child instances have been restarted.



92
93
94
# File 'lib/async/container/statistics.rb', line 92

def restarts
  @restarts
end

#spawnsObject (readonly)

How many child instances have been spawned.



88
89
90
# File 'lib/async/container/statistics.rb', line 88

def spawns
  @spawns
end

Instance Method Details

#<<(other) ⇒ Object

Append another statistics instance into this one.



131
132
133
134
135
# File 'lib/async/container/statistics.rb', line 131

def << other
	@spawns += other.spawns
	@restarts += other.restarts
	@failures += other.failures
end

#as_jsonObject

Generate a hash representation of the statistics.



140
141
142
143
144
145
146
147
148
# File 'lib/async/container/statistics.rb', line 140

def as_json(...)
	{
		spawns: @spawns,
		restarts: @restarts,
		failures: @failures,
		restart_rate: @restart_rate.per_second,
		failure_rate: @failure_rate.per_second,
	}
end

#failed?Boolean

Whether there have been any failures.

Returns:

  • (Boolean)


125
126
127
# File 'lib/async/container/statistics.rb', line 125

def failed?
	@failures > 0
end

#failure!Object

Increment the number of failures by 1.



110
111
112
113
# File 'lib/async/container/statistics.rb', line 110

def failure!
	@failures += 1
	@failure_rate.add(1)
end

#restart!Object

Increment the number of restarts by 1.



104
105
106
107
# File 'lib/async/container/statistics.rb', line 104

def restart!
	@restarts += 1
	@restart_rate.add(1)
end

#spawn!Object

Increment the number of spawns by 1.



99
100
101
# File 'lib/async/container/statistics.rb', line 99

def spawn!
	@spawns += 1
end

#to_jsonObject

Generate a JSON representation of the statistics.



153
154
155
# File 'lib/async/container/statistics.rb', line 153

def to_json(...)
	as_json.to_json(...)
end