Sha256: 62ea63baaeccf046b521a53a6bf010cd53bd9684fd6467d9368814a00cc28393

Contents?: true

Size: 1.92 KB

Versions: 62

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  module Cache
    module Senders
      class MemoryImpressionsSender < ImpressionsSenderAdapter
        def initialize(config, telemetry_api, impressions_api)
          @config = config
          @telemetry_api = telemetry_api
          @impressions_api = impressions_api
        end

        def record_uniques_key(uniques)
          uniques_keys = uniques_formatter(uniques)

          @telemetry_api.record_unique_keys(uniques_keys) unless uniques_keys.nil?
        rescue StandardError => e
          @config.log_found_exception(__method__.to_s, e)
        end

        def record_impressions_count(impressions_count)
          counts = impressions_count_formatter(impressions_count)

          @impressions_api.post_count(counts) unless counts.nil?
        rescue StandardError => e
          @config.log_found_exception(__method__.to_s, e)
        end

        private 

        def uniques_formatter(uniques)
          return if uniques.nil? || uniques.empty?

          to_return = { keys: [] }
          uniques.each do |key, value|
            to_return[:keys] << {
              f: key,
              ks: value.to_a
            }
          end

          to_return
        rescue StandardError => e
          @config.log_found_exception(__method__.to_s, e)
          nil
        end

        def impressions_count_formatter(counts)
          return if counts.nil? || counts.empty?

          formated_counts = {pf: []}

          counts.each do |key, value|              
            key_splited = key.split('::')
            
            formated_counts[:pf] << {
              f: key_splited[0].to_s, # feature name
              m: key_splited[1].to_i, # time frame
              rc: value # count
            }
          end

          formated_counts
        rescue StandardError => e
          @config.log_found_exception(__method__.to_s, e)
          nil
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
splitclient-rb-8.0.0.pre.rc2-java lib/splitclient-rb/cache/senders/impressions_adapter/memory_sender.rb
splitclient-rb-8.0.0.pre.rc2 lib/splitclient-rb/cache/senders/impressions_adapter/memory_sender.rb