dumbstats 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["ks.ruby@kurtstephens.com"]
7
7
  gem.description = %q{Collect data, generate stats, draw histograms, send to Graphite, do stuff in Ruby. }
8
8
  gem.summary = %q{Simple stats collection}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/kstephens/dumbstats"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -0,0 +1,20 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'rubygems'
4
+
5
+ require 'dumbstats/stats'
6
+ require 'dumbstats/histogram'
7
+
8
+ begin
9
+ stats = Dumbstats::Stats.new(:name => 'Roll 2x6')
10
+ 1000.times do | i |
11
+ d1 = (i % 6) + 1
12
+ d2 = ((i / 6) % 6) + 1
13
+ stats.add!(:d1, d1)
14
+ stats.add!(:d2, d2)
15
+ stats.add!(:d6_2, d1 + d2)
16
+ end
17
+ stats[:d1].h
18
+ stats[:d2].h
19
+ stats[:d6_2].h
20
+ end
@@ -1,3 +1,5 @@
1
+ require 'dumbstats'
2
+
1
3
  module Dumbstats
2
4
  # Collects details.
3
5
  class Bucket
@@ -0,0 +1,79 @@
1
+ require 'dumbstats/bucket'
2
+
3
+ module Dumbstats
4
+ # Prepare statitical graph for text display.
5
+ # A Graph of values.
6
+ class Graph < Bucket
7
+ include Initialization
8
+ attr_accessor :values, :width
9
+ attr_accessor :values_are_integers
10
+
11
+ def initialize *args
12
+ super
13
+ @force_min, @force_max = @min, @max
14
+ if @values
15
+ values = @values
16
+ @values = [ ]
17
+ values.each { | v | add! v }
18
+ finish!
19
+ end
20
+ @width ||= 20
21
+ end
22
+
23
+ def fix_width!
24
+ @width = 1 if @width < 1
25
+ @min = x_to_v(0)
26
+ @max = x_to_v(@width + 1)
27
+ @max_min = (@max - @min).to_f
28
+ self
29
+ end
30
+
31
+ def finish!
32
+ super
33
+ return nil if empty?
34
+ @min = @force_min if @force_min
35
+ @max = @force_max if @force_max
36
+ @max_min = @max - @min
37
+ @values_are_integers = @values.all?{|e| Integer === e.to_numeric}
38
+ if @values_are_integers
39
+ if @width > @max_min
40
+ @width = @max_min.to_i + 1
41
+ else
42
+ @max_min = @max_min.to_f
43
+ end
44
+ end
45
+ @width_f = @width.to_f
46
+ self
47
+ end
48
+
49
+ def sum!
50
+ finish! unless @sum
51
+ @sum
52
+ end
53
+
54
+ def percent value
55
+ '%5.1f%%' % (value * 100 / sum!.to_f)
56
+ end
57
+
58
+ def bar value
59
+ x = v_to_x(value).to_i
60
+ binding.pry if x < 0
61
+ if value > @min and x < 1
62
+ bar = '.'
63
+ else
64
+ bar = "*" * x
65
+ end
66
+ bar = "#{bar}#{' ' * (@width - bar.size)}"
67
+ bar
68
+ end
69
+
70
+ def v_to_x v
71
+ (v - @min) * @width / @max_min # = x
72
+ end
73
+
74
+ def x_to_v x
75
+ (x * @max_min / @width) + @min # = v
76
+ end
77
+ end
78
+ end # module
79
+
@@ -1,4 +1,5 @@
1
1
  require 'dumbstats/stats'
2
+ require 'dumbstats/graph'
2
3
 
3
4
  gem 'terminal-table'
4
5
  require 'terminal-table'
@@ -25,7 +26,7 @@ module Dumbstats
25
26
  return [ ] if @values.size < 2
26
27
  @x_graph = Graph.new(:min => @min, :max => @max, :values => @values, :width => @width)
27
28
  return [ ] if @x_graph.empty?
28
- @x_graph.fix_width!
29
+ # @x_graph.fix_width!
29
30
 
30
31
  @buckets = Hash.new { |h, k| b = Bucket.new; b.name = k; h[k] = b }
31
32
  @values.each do | v |
@@ -64,7 +65,7 @@ module Dumbstats
64
65
  s.padding_right = 1
65
66
 
66
67
  # Header:
67
- h = [ '<', '>', 'cnt', '%', "cnt h", "min", "avg", "max" ]
68
+ h = [ '>=', '<', 'cnt', '%', "cnt h", "min", "avg", "max" ]
68
69
  align_right = [ 0, 1, 2, 3, 5, 6, 7 ]
69
70
  if @show_sum
70
71
  h.push('sum', '%', 'sum h')
@@ -74,8 +75,12 @@ module Dumbstats
74
75
 
75
76
  cnt_sum = sum_sum = 0
76
77
  @width.times do | i |
77
- x0 = @x_graph.x_to_v(i).to_i
78
- x1 = @x_graph.x_to_v(i + 1).to_i - 1
78
+ x0 = @x_graph.x_to_v(i)
79
+ x1 = @x_graph.x_to_v(i + 1)
80
+ $stderr.puts " i=#{i} x0=#{x0.inspect} x1=#{x1.inspect} #{@x_graph.min.inspect} #{@x_graph.max.inspect}" if @debug
81
+ x0 = x0.to_i
82
+ x1 = x1.to_i
83
+
79
84
  b = @buckets[i]
80
85
  b.finish!
81
86
 
@@ -1,3 +1,4 @@
1
+ require 'dumbstats'
1
2
  require 'dumbstats/bucket'
2
3
  require 'dumbstats/value'
3
4
 
@@ -104,77 +105,6 @@ class Stats
104
105
  nil
105
106
  end
106
107
 
107
- # A Graph of values.
108
- class Graph < Bucket
109
- include Initialization
110
- attr_accessor :values, :width
111
- attr_accessor :values_are_integers
112
-
113
- def initialize *args
114
- super
115
- @force_min, @force_max = @min, @max
116
- if @values
117
- values = @values
118
- @values = [ ]
119
- values.each { | v | add! v }
120
- finish!
121
- end
122
- @width ||= 20
123
- end
124
-
125
- def fix_width!
126
- @width = 1 if @width < 1
127
- @max = x_to_v(@width + 1)
128
- @max_min = (@max - @min).to_f
129
- self
130
- end
131
-
132
- def finish!
133
- super
134
- return nil if empty?
135
- @min = @force_min if @force_min
136
- @max = @force_max if @force_max
137
- @max_min = @max - @min
138
- @values_are_integers = @values.all?{|e| Integer === e.to_numeric}
139
- if @values_are_integers
140
- if @width > @max_min
141
- @width = @max_min.to_i
142
- else
143
- @max_min = @max_min.to_f
144
- end
145
- end
146
- self
147
- end
148
-
149
- def sum!
150
- finish! unless @sum
151
- @sum
152
- end
153
-
154
- def percent value
155
- '%5.1f%%' % (value * 100 / sum!.to_f)
156
- end
157
-
158
- def bar value
159
- x = v_to_x(value).to_i
160
- binding.pry if x < 0
161
- if value > @min and x < 1
162
- bar = '.'
163
- else
164
- bar = "*" * x
165
- end
166
- bar = "#{bar}#{' ' * (@width - bar.size)}"
167
- bar
168
- end
169
-
170
- def v_to_x v
171
- (v - @min) * @width / @max_min # = x
172
- end
173
-
174
- def x_to_v x
175
- (x * @max_min / @width) + @min # = v
176
- end
177
- end
178
108
  end # class
179
109
  end # module
180
110
 
@@ -1,3 +1,3 @@
1
1
  module Dumbstats
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dumbstats
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kurt Stephens
@@ -34,14 +34,16 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - dumbstats.gemspec
37
+ - example/ex01.rb
37
38
  - lib/dumbstats.rb
38
39
  - lib/dumbstats/bucket.rb
40
+ - lib/dumbstats/graph.rb
39
41
  - lib/dumbstats/graphite.rb
40
42
  - lib/dumbstats/histogram.rb
41
43
  - lib/dumbstats/stats.rb
42
44
  - lib/dumbstats/value.rb
43
45
  - lib/dumbstats/version.rb
44
- homepage: ""
46
+ homepage: https://github.com/kstephens/dumbstats
45
47
  licenses: []
46
48
 
47
49
  post_install_message: