Sha256: 49003238c647ffe170f5f6a19c0e0a7d9d40b6af01136c4d879e926282d31a9e

Contents?: true

Size: 1015 Bytes

Versions: 164

Compression:

Stored size: 1015 Bytes

Contents

# frozen_string_literal: true

require 'concurrent'

module SplitIoClient
  module Engine
    module Common
      TIME_INTERVAL_MS = 3600 * 1000

      class ImpressionCounter
        DEFAULT_AMOUNT = 1

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

        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

        def pop_all
          to_return = Concurrent::Hash.new

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

          to_return
        end

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

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

Version data entries

164 entries across 164 versions & 1 rubygems

Version Path
splitclient-rb-7.3.3-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc18-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc18 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc17-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc17 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc16-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc16 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc15-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc15 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc14-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc14 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc13-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc13 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc12-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc12 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc11-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc11 lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc10-java lib/splitclient-rb/engine/common/impressions_counter.rb
splitclient-rb-7.3.3.pre.rc10 lib/splitclient-rb/engine/common/impressions_counter.rb