Class: Legion::Extensions::Agentic::Executive::LoadBalancing::Helpers::Subsystem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#capacityObject (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_atObject (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_loadObject (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

#idObject (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

#nameObject (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_typeObject (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_processedObject (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_shedObject (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_capacityObject



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

#healthObject



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_labelObject



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_labelObject



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

Returns:

  • (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_hObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/legion/extensions/agentic/executive/load_balancing/helpers/subsystem.rb', line 43

def underloaded?
  utilization <= UNDERLOAD_THRESHOLD
end

#utilizationObject



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