Class: Dumbstats::Bucket
- Inherits:
-
Object
- Object
- Dumbstats::Bucket
- Includes:
- Initialization
- Defined in:
- lib/dumbstats/bucket.rb
Overview
Collects details.
Direct Known Subclasses
Constant Summary collapse
- KEYS =
[ :count, :min, :median, :avg, :stddev, :max, :sum, :dt, ]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #add!(x) ⇒ Object
-
#add_delta!(x0, x1) ⇒ Object
Adds stat as a positive delta.
- #count!(x) ⇒ Object
- #count_only? ⇒ Boolean
- #empty? ⇒ Boolean
- #finish! ⇒ Object
- #h(opts = nil) ⇒ Object
- #histogram(*opts) ⇒ Object
-
#initialize(*args) ⇒ Bucket
constructor
A new instance of Bucket.
- #inspect ⇒ Object
-
#rate!(dt) ⇒ Object
Converts this to a rate over time.
- #rate? ⇒ Boolean
- #to_a ⇒ Object
- #to_s(opts = nil) ⇒ Object
Methods included from Initialization
Constructor Details
#initialize(*args) ⇒ Bucket
Returns a new instance of Bucket.
23 24 25 26 |
# File 'lib/dumbstats/bucket.rb', line 23 def initialize *args super @count = 0 end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
21 22 23 |
# File 'lib/dumbstats/bucket.rb', line 21 def name @name end |
#values ⇒ Object
Returns the value of attribute values.
21 22 23 |
# File 'lib/dumbstats/bucket.rb', line 21 def values @values end |
Instance Method Details
#add!(x) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dumbstats/bucket.rb', line 46 def add! x @values << x if @values x = x.to_numeric if x.respond_to?(:to_numeric) unless @min @min = @max = x else @min = x if x < @min @max = x if x > @max end @sum ||= 0 s = @sum += x c = @count += 1 @avg = s.to_f / c self end |
#add_delta!(x0, x1) ⇒ Object
Adds stat as a positive delta.
63 64 65 66 67 68 |
# File 'lib/dumbstats/bucket.rb', line 63 def add_delta! x0, x1 if x0 and (dx = x1 - x0) >= 0 add! dx end self end |
#count!(x) ⇒ Object
37 38 39 40 |
# File 'lib/dumbstats/bucket.rb', line 37 def count! x @count += 1 self end |
#count_only? ⇒ Boolean
42 43 44 |
# File 'lib/dumbstats/bucket.rb', line 42 def count_only? ! @sum end |
#empty? ⇒ Boolean
70 71 72 |
# File 'lib/dumbstats/bucket.rb', line 70 def empty? ! @min || @max == @min end |
#finish! ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/dumbstats/bucket.rb', line 87 def finish! if @dt return self end if @count == 1 @min = @max = @avg = nil end if @avg && @values && ! @values.empty? @values.sort! n = @values.size @median = @values[n / 2] v = @values.map{|e| e = (e.to_numeric - @avg); e * e} v.sort! s = 0 v.each {|e| s += e } @stddev = Math.sqrt(s.to_f / n) end self end |
#h(opts = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/dumbstats/bucket.rb', line 112 def h opts = nil h = histogram(opts) $stdout.puts "# #{self.class} #{@name}" to_a.each do | k, v | $stdout.puts "#{k.inspect}: #{v}" if v end $stdout.puts h * "\n" nil end |
#histogram(*opts) ⇒ Object
107 108 109 110 |
# File 'lib/dumbstats/bucket.rb', line 107 def histogram *opts opts << { :values => finish!.values } Histogram.new(*opts).generate end |
#inspect ⇒ Object
126 127 128 |
# File 'lib/dumbstats/bucket.rb', line 126 def inspect to_s end |
#rate!(dt) ⇒ Object
Converts this to a rate over time.
75 76 77 78 79 80 |
# File 'lib/dumbstats/bucket.rb', line 75 def rate! dt @dt = dt @min = @max = nil @avg = (@count || @sum).to_f / dt self end |
#rate? ⇒ Boolean
83 84 85 |
# File 'lib/dumbstats/bucket.rb', line 83 def rate? ! ! @dt end |
#to_a ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/dumbstats/bucket.rb', line 28 def to_a h = [ ] KEYS.each do | k | v = instance_variable_get("@#{k}") h << [ k, v ] if v end h end |
#to_s(opts = nil) ⇒ Object
122 123 124 |
# File 'lib/dumbstats/bucket.rb', line 122 def to_s opts = nil h({:width => 50, :height => 40}.update(opts || {})) end |