Class: Legion::Extensions::Agentic::Executive::LoadBalancing::Helpers::Subsystem
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Executive::LoadBalancing::Helpers::Subsystem
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb
Constant Summary
Constants included from Constants
Constants::DEFAULT_CAPACITY, Constants::HEALTH_LABELS, Constants::LOAD_LABELS, Constants::MAX_SUBSYSTEMS, Constants::MAX_TASKS, Constants::OVERLOAD_THRESHOLD, Constants::REBALANCE_STEP, Constants::SUBSYSTEM_TYPES, Constants::UNDERLOAD_THRESHOLD
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#current_load ⇒ Object
readonly
Returns the value of attribute current_load.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subsystem_type ⇒ Object
readonly
Returns the value of attribute subsystem_type.
-
#tasks_processed ⇒ Object
readonly
Returns the value of attribute tasks_processed.
-
#tasks_shed ⇒ Object
readonly
Returns the value of attribute tasks_shed.
Instance Method Summary collapse
- #add_load!(amount:) ⇒ Object
- #available_capacity ⇒ Object
- #health ⇒ Object
- #health_label ⇒ Object
-
#initialize(name:, subsystem_type: :general, capacity: DEFAULT_CAPACITY) ⇒ Subsystem
constructor
A new instance of Subsystem.
- #load_label ⇒ Object
- #overloaded? ⇒ Boolean
- #shed_load!(amount:) ⇒ Object
- #to_h ⇒ Object
- #underloaded? ⇒ Boolean
- #utilization ⇒ Object
Constructor Details
#initialize(name:, subsystem_type: :general, capacity: DEFAULT_CAPACITY) ⇒ Subsystem
Returns a new instance of Subsystem.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 17 def initialize(name:, subsystem_type: :general, capacity: DEFAULT_CAPACITY) @id = SecureRandom.uuid @name = name @subsystem_type = subsystem_type.to_sym @capacity = capacity.to_f.clamp(0.1, 5.0) @current_load = 0.0 @tasks_processed = 0 @tasks_shed = 0 @created_at = Time.now.utc end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def capacity @capacity end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def created_at @created_at end |
#current_load ⇒ Object (readonly)
Returns the value of attribute current_load.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def current_load @current_load end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def name @name end |
#subsystem_type ⇒ Object (readonly)
Returns the value of attribute subsystem_type.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def subsystem_type @subsystem_type end |
#tasks_processed ⇒ Object (readonly)
Returns the value of attribute tasks_processed.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def tasks_processed @tasks_processed end |
#tasks_shed ⇒ Object (readonly)
Returns the value of attribute tasks_shed.
14 15 16 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 14 def tasks_shed @tasks_shed end |
Instance Method Details
#add_load!(amount:) ⇒ Object
60 61 62 63 64 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 60 def add_load!(amount:) @current_load = (@current_load + amount.to_f).clamp(0.0, @capacity * 1.5).round(10) @tasks_processed += 1 self end |
#available_capacity ⇒ Object
73 74 75 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 73 def available_capacity [(@capacity - @current_load), 0.0].max.round(10) end |
#health ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 47 def health if overloaded? [1.0 - ((utilization - OVERLOAD_THRESHOLD) * 3), 0.0].max.round(10) else 1.0 end end |
#health_label ⇒ Object
55 56 57 58 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 55 def health_label match = HEALTH_LABELS.find { |range, _| range.cover?(health) } match ? match.last : :failing end |
#load_label ⇒ Object
34 35 36 37 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 34 def load_label match = LOAD_LABELS.find { |range, _| range.cover?(utilization) } match ? match.last : :overloaded end |
#overloaded? ⇒ Boolean
39 40 41 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 39 def overloaded? utilization >= OVERLOAD_THRESHOLD end |
#shed_load!(amount:) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 66 def shed_load!(amount:) removed = [amount.to_f, @current_load].min @current_load = (@current_load - removed).clamp(0.0, @capacity * 1.5).round(10) @tasks_shed += 1 removed end |
#to_h ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 77 def to_h { id: @id, name: @name, subsystem_type: @subsystem_type, capacity: @capacity, current_load: @current_load, utilization: utilization, load_label: load_label, overloaded: overloaded?, health: health, health_label: health_label, available_capacity: available_capacity, tasks_processed: @tasks_processed, tasks_shed: @tasks_shed, created_at: @created_at } end |
#underloaded? ⇒ Boolean
43 44 45 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 43 def underloaded? utilization <= UNDERLOAD_THRESHOLD end |
#utilization ⇒ Object
28 29 30 31 32 |
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 28 def utilization return 0.0 if @capacity.zero? (@current_load / @capacity).clamp(0.0, 1.5).round(10) end |