Class: Dumbstats::Stats

Inherits:
Object
  • Object
show all
Includes:
Initialization
Defined in:
lib/dumbstats/stats.rb

Overview

Collects stats by name into Buckets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Initialization

#update_from_hash!

Constructor Details

#initialize(*opts) ⇒ Stats

Returns a new instance of Stats.



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

def initialize *opts
  super
  @s = { }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sel, *args) ⇒ Object



40
41
42
# File 'lib/dumbstats/stats.rb', line 40

def method_missing sel, *args
  super unless args.empty? and ! block_given? and @s[sel]
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



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

def chain
  @chain
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/dumbstats/stats.rb', line 10

def name
  @name
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/dumbstats/stats.rb', line 12

def verbose
  @verbose
end

Instance Method Details

#[](k) ⇒ Object



26
27
28
# File 'lib/dumbstats/stats.rb', line 26

def [] k
  @s[k] || @s[k.to_sym]
end

#add!(k, v) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/dumbstats/stats.rb', line 52

def add! k, v
  $stderr.puts "  add! #{k.inspect} #{v.inspect}" if @verbose
  b = stat(k)
  b.add! v
  @chain.add! k, v if @chain
  self
end

#add_delta!(k, v0, v1) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/dumbstats/stats.rb', line 60

def add_delta! k, v0, v1
  $stderr.puts "  add_delta! #{k.inspect} #{v0.inspect}, #{v1.inspect}" if @verbose
  b = stat(k)
  b.add_delta! v0, v1
  @chain.add_delta! k, v0, v1 if @chain
  self
end

#clear!Object



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

def clear!
  @s.clear
end

#count!(k, v = 1) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/dumbstats/stats.rb', line 44

def count! k, v = 1
  $stderr.puts "  count! #{k.inspect} #{v.inspect}" if @verbose
  b = stat(k)
  b.count! v
  @chain.count! k, v if @chain
  self
end

#eachObject



32
33
34
35
36
# File 'lib/dumbstats/stats.rb', line 32

def each
  keys.each do | k |
    yield k, @s[k]
  end
end

#finish!Object



68
69
70
71
# File 'lib/dumbstats/stats.rb', line 68

def finish!
  @s.values.each{|b| b.finish!}
  self
end

#hObject



103
104
105
106
# File 'lib/dumbstats/stats.rb', line 103

def h
  put
  nil
end

#inspectObject



14
15
16
# File 'lib/dumbstats/stats.rb', line 14

def inspect
  to_s
end

#keysObject



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

def keys
  @s.keys.sort_by{|s| s.to_s}
end

#put(opts = { }) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dumbstats/stats.rb', line 73

def put opts = { }
  o = opts[:output] || $stdout
  show_histogram = opts[:show_histogram]
  ks = @s.keys.sort_by{|e| e.to_s}
  ks.each do | k |
    c = @s[k]
    c.finish!
    if c.count_only?
      o.puts "    :'#{k}': #{c.count}"
      next
    end
    histogram = nil
    if show_histogram and values = c.values and ! values.empty?
      histogram = c.histogram(:width => 30, :height => 15)
      histogram = nil if histogram.empty?
    end
    o.puts "    #{k.to_sym.inspect}:"
    c.to_a.each do | k, v |
      o.puts "       #{k.to_sym.inspect}: #{v.inspect}"
    end
    if histogram
      o.puts "       :histogram:"
      histogram.each do | l |
        o.puts "         - #{l.inspect}"
      end
    end
  end
  self
end

#stat(k) ⇒ Object



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

def stat k
  @s[k] ||= Bucket.new(:name => k, :values => [ ])
end