Class: SplitIoClient::Cache::Repositories::Metrics::MemoryRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb

Defined Under Namespace

Classes: SumAndCount, ValueAndCount

Constant Summary collapse

OPERATIONS =
%w(sdk.get_treatment sdk.get_treatments sdk.get_treatment_with_config sdk.get_treatments_with_config)

Instance Method Summary collapse

Constructor Details

#initialize(_ = nil, config) ⇒ MemoryRepository

Returns a new instance of MemoryRepository.



10
11
12
13
14
15
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 10

def initialize(_ = nil, config)
  @counts = Concurrent::Array.new
  @latencies = Concurrent::Array.new
  @gauges = Concurrent::Array.new
  @config = config
end

Instance Method Details

#add_count(counter, delta) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 17

def add_count(counter, delta)
  counter_hash = @counts.find { |c| c[:name] == counter }
  if counter_hash.nil?
    counter_delta = SumAndCount.new
    counter_delta.add_delta(delta)
    @counts << { name: counter, delta: counter_delta }
  else
    counter_hash[:delta].add_delta(delta)
  end
end

#add_gauge(gauge, value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 37

def add_gauge(gauge, value)
  gauge_hash = @gauges.find { |g| g[:name] == gauge }
  if gauge_hash.nil?
    gauge_value = ValueAndCount.new
    gauge_value.set_value(value)
    @gauges << { name: gauge, value: gauge_value }
  else
    gauge_hash[:value].set_value(value)
  end
end

#add_latency(operation, time_in_ms, binary_search) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 28

def add_latency(operation, time_in_ms, binary_search)
  if (OPERATIONS.include?(operation))
    index = binary_search.add_latency_millis(time_in_ms, true)
    increase_latency_bucket(operation, index)
  else
    concatenate_latency(operation, time_in_ms)
  end
end

#clearObject



80
81
82
83
84
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 80

def clear
  clear_counts
  clear_latencies
  clear_gauges
end

#clear_countsObject



68
69
70
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 68

def clear_counts
  @counts = Concurrent::Array.new
end

#clear_gaugesObject



76
77
78
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 76

def clear_gauges
  @gauges = Concurrent::Array.new
end

#clear_latenciesObject



72
73
74
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 72

def clear_latencies
  @latencies = Concurrent::Array.new
end

#countsObject



48
49
50
51
52
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 48

def counts
  @counts.each_with_object({}) do |count, memo|
    memo[count[:name]] = count[:delta].sum
  end
end

#fix_latenciesObject



64
65
66
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 64

def fix_latencies
  # NOOP
end

#gaugesObject



60
61
62
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 60

def gauges
  # TODO
end

#latenciesObject



54
55
56
57
58
# File 'lib/splitclient-rb/cache/repositories/metrics/memory_repository.rb', line 54

def latencies
  @latencies.each_with_object({}) do |latency, memo|
    memo[latency[:operation]] = latency[:latencies]
  end
end