Class: Karafka::Web::Tracking::Helpers::Ttls::Array

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Core::Helpers::Time
Defined in:
lib/karafka/web/tracking/helpers/ttls/array.rb

Overview

Array that allows us to store data points that expire over time automatically.

Instance Method Summary collapse

Constructor Details

#initialize(ttl) ⇒ Array

Returns a new instance of Array.

Parameters:

  • ttl (Integer)

    milliseconds ttl



15
16
17
18
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 15

def initialize(ttl)
  @ttl = ttl
  @accu = []
end

Instance Method Details

#<<(value) ⇒ Object

Returns added element.

Parameters:

  • value (Object)

    adds value to the array

Returns:

  • (Object)

    added element



31
32
33
34
35
36
37
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 31

def <<(value)
  @accu << { value: value, added_at: monotonic_now }

  clear

  value
end

#eachObject

Iterates over only active elements



21
22
23
24
25
26
27
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 21

def each
  clear

  @accu.each do |sample|
    yield sample[:value]
  end
end

#empty?Boolean

Returns is the array empty.

Returns:

  • (Boolean)

    is the array empty



40
41
42
43
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 40

def empty?
  clear
  @accu.empty?
end

#samplesHash

Samples that are within our TTL time window with the times

Returns:



48
49
50
51
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 48

def samples
  clear
  @accu
end

#to_a::Array

Returns pure array version with only active elements.

Returns:

  • (::Array)

    pure array version with only active elements



54
55
56
57
# File 'lib/karafka/web/tracking/helpers/ttls/array.rb', line 54

def to_a
  clear
  super
end