Class: Karafka::Web::Tracking::Helpers::Ttls::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/tracking/helpers/ttls/stats.rb

Overview

Object that simplifies computing aggregated statistics out of ttl data For TTL based operations we may collect samples from multiple consumers/producers etc but in the end we are interested in the collective result of the whole process.

For example when we talk about data received from Kafka, we want to materialize total number of bytes and not bytes per given client connection. This layer simplifies this by doing necessary aggregations and providing the final results

Instance Method Summary collapse

Constructor Details

#initialize(ttls_hash) ⇒ Stats

Returns a new instance of Stats.

Parameters:



17
18
19
20
21
22
23
24
# File 'lib/karafka/web/tracking/helpers/ttls/stats.rb', line 17

def initialize(ttls_hash)
  @data = ttls_hash
          .values
          .map(&:samples)
          .map(&:to_a)
          .delete_if { |samples| samples.size < 2 }
          .map { |samples| samples.map(&:values) }
end

Instance Method Details

#rpsFloat

Computes the rate out of the samples provided on a per second basis. The samples need to come from the window aggregations

Returns:

  • (Float)

    per second rate value



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/karafka/web/tracking/helpers/ttls/stats.rb', line 30

def rps
  sub_results = @data.map do |samples|
    oldest = samples.first
    newest = samples.last

    value = oldest[0] - newest[0]
    # Convert to seconds as we want to have it in a 1 sec pace
    time = (oldest[1] - newest[1]) / 1_000

    value / time.to_f
  end

  sub_results.flatten.sum
end