Class: SplitIoClient::Engine::Common::ImpressionCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/engine/common/impressions_counter.rb

Constant Summary collapse

DEFAULT_AMOUNT =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImpressionCounter

Returns a new instance of ImpressionCounter.



13
14
15
# File 'lib/splitclient-rb/engine/common/impressions_counter.rb', line 13

def initialize
  @cache = Concurrent::Hash.new
end

Class Method Details

.truncate_time_frame(timestamp_ms) ⇒ Object



39
40
41
# File 'lib/splitclient-rb/engine/common/impressions_counter.rb', line 39

def self.truncate_time_frame(timestamp_ms)
  timestamp_ms - (timestamp_ms % TIME_INTERVAL_MS)
end

Instance Method Details

#inc(split_name, time_frame) ⇒ Object



17
18
19
20
21
22
# File 'lib/splitclient-rb/engine/common/impressions_counter.rb', line 17

def inc(split_name, time_frame)
  key = make_key(split_name, time_frame)

  current_amount = @cache[key]
  @cache[key] = current_amount.nil? ? DEFAULT_AMOUNT : (current_amount + DEFAULT_AMOUNT)
end

#make_key(split_name, time_frame) ⇒ Object



35
36
37
# File 'lib/splitclient-rb/engine/common/impressions_counter.rb', line 35

def make_key(split_name, time_frame)
  "#{split_name}::#{ImpressionCounter.truncate_time_frame(time_frame)}"
end

#pop_allObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/splitclient-rb/engine/common/impressions_counter.rb', line 24

def pop_all
  to_return = Concurrent::Hash.new

  @cache.each do |key, value|
    to_return[key] = value
  end
  @cache.clear

  to_return
end