Class: Legion::Telemetry::SlidingWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/telemetry/safety_metrics.rb

Instance Method Summary collapse

Constructor Details

#initialize(window_seconds) ⇒ SlidingWindow

Returns a new instance of SlidingWindow.



6
7
8
9
10
# File 'lib/legion/telemetry/safety_metrics.rb', line 6

def initialize(window_seconds)
  @window = window_seconds
  @entries = []
  @mutex = Mutex.new
end

Instance Method Details

#countObject



19
20
21
22
23
24
# File 'lib/legion/telemetry/safety_metrics.rb', line 19

def count
  @mutex.synchronize do
    prune!
    @entries.size
  end
end

#count_for(**filters) ⇒ Object



26
27
28
29
30
31
# File 'lib/legion/telemetry/safety_metrics.rb', line 26

def count_for(**filters)
  @mutex.synchronize do
    prune!
    @entries.count { |e| filters.all? { |k, v| e[k] == v } }
  end
end

#entries_matching(**filters) ⇒ Object



33
34
35
36
37
38
# File 'lib/legion/telemetry/safety_metrics.rb', line 33

def entries_matching(**filters)
  @mutex.synchronize do
    prune!
    @entries.select { |e| filters.all? { |k, v| e[k] == v } }
  end
end

#push(**entry) ⇒ Object



12
13
14
15
16
17
# File 'lib/legion/telemetry/safety_metrics.rb', line 12

def push(**entry)
  @mutex.synchronize do
    @entries << entry.merge(at: Time.now)
    prune!
  end
end