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



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

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



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

def max
  quantile(1.0)
end

#minObject



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

def min
  quantile(0.0)
end

#quantile(fraction) ⇒ Object

Raises:



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

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
27
# File 'lib/ddmetrics/stats.rb', line 23

def sum
  raise EmptyError if @values.empty?

  @values.reduce(:+)
end