Class: Legion::Gaia::TickHistory

Inherits:
Object
  • Object
show all
Extended by:
Logging::Helper
Defined in:
lib/legion/gaia/tick_history.rb

Constant Summary collapse

MAX_ENTRIES =
200

Instance Method Summary collapse

Constructor Details

#initializeTickHistory

Returns a new instance of TickHistory.



12
13
14
15
# File 'lib/legion/gaia/tick_history.rb', line 12

def initialize
  @entries = []
  @mutex = Mutex.new
end

Instance Method Details

#recent(limit: 50) ⇒ Object



38
39
40
41
42
# File 'lib/legion/gaia/tick_history.rb', line 38

def recent(limit: 50)
  @mutex.synchronize do
    @entries.last(limit).map(&:dup)
  end
end

#record(tick_result) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/gaia/tick_history.rb', line 17

def record(tick_result)
  return unless tick_result.is_a?(Hash) && tick_result[:results].is_a?(Hash)

  timestamp = Time.now.utc.iso8601
  new_events = tick_result[:results].filter_map do |phase_name, phase_data|
    next unless phase_data.is_a?(Hash)

    {
      timestamp: timestamp.freeze,
      phase: phase_name.to_s.freeze,
      duration_ms: phase_data[:elapsed_ms],
      status: phase_data[:status]
    }.freeze
  end

  @mutex.synchronize do
    @entries.concat(new_events)
    @entries.shift(@entries.size - MAX_ENTRIES) if @entries.size > MAX_ENTRIES
  end
end

#sizeObject



44
45
46
# File 'lib/legion/gaia/tick_history.rb', line 44

def size
  @mutex.synchronize { @entries.size }
end