Class: Twingly::Metrics::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/metrics/snapshot.rb

Constant Summary collapse

MEDIAN_Q =
0.5
P75_Q =
0.75
P95_Q =
0.95
P98_Q =
0.98
P99_Q =
0.99
P999_Q =
0.999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Snapshot

Returns a new instance of Snapshot.



15
16
17
# File 'lib/twingly/metrics/snapshot.rb', line 15

def initialize(values)
  @values = values.sort
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



13
14
15
# File 'lib/twingly/metrics/snapshot.rb', line 13

def values
  @values
end

Instance Method Details

#get_75th_percentileObject

rubocop:disable Naming/AccessorMethodName



43
44
45
# File 'lib/twingly/metrics/snapshot.rb', line 43

def get_75th_percentile
  value(P75_Q)
end

#get_95th_percentileObject



47
48
49
# File 'lib/twingly/metrics/snapshot.rb', line 47

def get_95th_percentile
  value(P95_Q)
end

#get_98th_percentileObject



51
52
53
# File 'lib/twingly/metrics/snapshot.rb', line 51

def get_98th_percentile
  value(P98_Q)
end

#get_999th_percentileObject



59
60
61
# File 'lib/twingly/metrics/snapshot.rb', line 59

def get_999th_percentile
  value(P999_Q)
end

#get_99th_percentileObject



55
56
57
# File 'lib/twingly/metrics/snapshot.rb', line 55

def get_99th_percentile
  value(P99_Q)
end

#medianObject



38
39
40
# File 'lib/twingly/metrics/snapshot.rb', line 38

def median
  value(MEDIAN_Q)
end

#sizeObject



34
35
36
# File 'lib/twingly/metrics/snapshot.rb', line 34

def size
  @values.length
end

#value(quantile) ⇒ Object

rubocop:disable Metrics/AbcSize

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/twingly/metrics/snapshot.rb', line 19

def value(quantile) # rubocop:disable Metrics/AbcSize
  raise ArgumentError, "quantile must be between 0.0 and 1.0" if quantile < 0.0 || quantile > 1.0

  return 0.0 if @values.empty?

  pos = quantile * (@values.length + 1)

  return @values.first if pos < 1
  return @values.last if pos >= @values.length

  lower = @values[pos.to_i - 1]
  upper = @values[pos.to_i]
  lower + ((pos - pos.floor) * (upper - lower))
end