Class: DDMetrics::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/ddmetrics/stats.rb

Defined Under Namespace

Classes: EmptyError

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Stats

Returns a new instance of Stats.



11
12
13
# File 'lib/ddmetrics/stats.rb', line 11

def initialize(values)
  @values = values
end

Instance Method Details

#avgObject



28
29
30
# File 'lib/ddmetrics/stats.rb', line 28

def avg
  sum.to_f / count
end

#countObject



19
20
21
# File 'lib/ddmetrics/stats.rb', line 19

def count
  @values.size
end

#inspectObject



15
16
17
# File 'lib/ddmetrics/stats.rb', line 15

def inspect
  "<#{self.class} count=#{count}>"
end

#maxObject



36
37
38
# File 'lib/ddmetrics/stats.rb', line 36

def max
  quantile(1.0)
end

#minObject



32
33
34
# File 'lib/ddmetrics/stats.rb', line 32

def min
  quantile(0.0)
end

#quantile(fraction) ⇒ Object

Raises:



40
41
42
43
44
45
46
# File 'lib/ddmetrics/stats.rb', line 40

def quantile(fraction)
  raise EmptyError if @values.empty?

  target = (@values.size - 1) * fraction.to_f
  interp = target % 1.0
  sorted_values[target.floor] * (1.0 - interp) + sorted_values[target.ceil] * interp
end

#sumObject

Raises:



23
24
25
26
# File 'lib/ddmetrics/stats.rb', line 23

def sum
  raise EmptyError if @values.empty?
  @values.reduce(:+)
end