Class: Legion::Extensions::Synapse::Actor::Homeostasis

Inherits:
Actors::Every
  • Object
show all
Defined in:
lib/legion/extensions/synapse/actors/homeostasis.rb

Instance Method Summary collapse

Instance Method Details

#action(**_opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 14

def action(**_opts)
  return { status: :skipped, reason: :no_data } unless defined?(Legion::Data)

  results = { spikes: 0, droughts: 0, updated: 0 }
  return results unless defined?(Legion::Extensions::Synapse::Data::Model::Synapse)

  cutoff = Time.now - 60
  window_seconds = 60.0
  signal_model = Legion::Extensions::Synapse::Data::Model::SynapseSignal
  synapse_model = Legion::Extensions::Synapse::Data::Model::Synapse

  # Single query: count signals per synapse in the last 60s window
  signal_counts = signal_model.where { created_at > cutoff }
                              .group_and_count(:synapse_id)
                              .as_hash(:synapse_id, :count)

  # Fetch only active synapses with a nonzero baseline — eager-load to avoid N+1
  active_synapses = synapse_model.where(status: 'active')
                                 .where { baseline_throughput > 0 } # rubocop:disable Style/NumericPredicate
                                 .all
  return results if active_synapses.empty?

  # Collect updates in memory, then apply in a single batch to avoid connection churn
  updates = []

  active_synapses.each do |synapse|
    baseline = synapse.baseline_throughput
    # Convert raw count in the window to signals/minute for apples-to-apples comparison
    current  = (signal_counts.fetch(synapse.id, 0).to_f / window_seconds) * 60.0

    if Helpers::Homeostasis.spike?(current, baseline, duration_seconds: window_seconds)
      results[:spikes] += 1
    elsif Helpers::Homeostasis.drought?(current, baseline, silent_seconds: window_seconds)
      results[:droughts] += 1
    end

    new_baseline = Helpers::Homeostasis.update_baseline(baseline, current)
    updates << { synapse_id: synapse.id, baseline_throughput: new_baseline }
    results[:updated] += 1
  end

  # Batch-update all baselines in a single transaction
  unless updates.empty?
    synapse_model.db.transaction do
      updates.each do |u|
        synapse_model.where(id: u[:synapse_id]).update(baseline_throughput: u[:baseline_throughput])
      end
    end
  end

  results
end

#check_subtask?Boolean

Returns:

  • (Boolean)


11
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 11

def check_subtask? = false

#generate_task?Boolean

Returns:

  • (Boolean)


12
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 12

def generate_task? = false

#runner_classObject



8
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 8

def runner_class = self.class

#timeObject



9
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 9

def time = 30

#use_runner?Boolean

Returns:

  • (Boolean)


10
# File 'lib/legion/extensions/synapse/actors/homeostasis.rb', line 10

def use_runner? = false