stats 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -30,7 +30,7 @@ begin
30
30
  gemspec.email = "matt@mattduncan.org"
31
31
  gemspec.homepage = "http://github.com/mrduncan/stats"
32
32
  gemspec.authors = ["Matt Duncan"]
33
- gemspec.version = '0.1.0'
33
+ gemspec.version = '0.2.0'
34
34
  gemspec.add_dependency 'redis'
35
35
  gemspec.description = <<description
36
36
  The stats gem is a simple way to keep track of different statistics using
@@ -29,6 +29,11 @@ module Stats
29
29
  Stats.redis.decrby(name, by)
30
30
  end
31
31
 
32
+ # Returns the value of the counter with the specified name.
33
+ def counter(name)
34
+ Stats.redis.get(name).to_i
35
+ end
36
+
32
37
  # Clears the stat with the specified name.
33
38
  def clear(name)
34
39
  Stats.redis.del(name)
@@ -2,8 +2,7 @@ module Stats
2
2
  module Times
3
3
  # Returns the timing data for the stat with the specified name.
4
4
  def get_times(name)
5
- times = Stats.redis.lrange(name, 0, -1)
6
- TimingStat.new(times.map { |t| t.to_f })
5
+ TimingStat.new(Stats.redis.lrange(name, 0, -1).map { |time| time.to_f })
7
6
  end
8
7
 
9
8
  # Adds a new time for the stat with the specified name.
@@ -1,19 +1,22 @@
1
1
  module Stats
2
2
  class TimingStat < Array
3
3
  # Returns the average value.
4
- def average
4
+ def avg
5
+ return nil if length == 0
5
6
  sum / length.to_f
6
7
  end
7
8
 
8
- # Returns the sample variance of the values.
9
- def population_variance
10
- avg = average
11
- 1 / length.to_f * inject(0) { |acc, i| acc + (i - avg) ** 2 }
9
+ # Returns the population variance of the values.
10
+ def pop_var
11
+ return nil if length == 0
12
+ average = avg
13
+ 1 / length.to_f * inject(0) { |acc, i| acc + (i - average) ** 2 }
12
14
  end
13
15
 
14
16
  # Returns the standard deviation of the values.
15
- def standard_deviation
16
- Math.sqrt(population_variance)
17
+ def std_dev
18
+ return nil if length == 0
19
+ Math.sqrt(pop_var)
17
20
  end
18
21
 
19
22
  private
@@ -5,7 +5,7 @@ require 'stats'
5
5
  class TestStats < Test::Unit::TestCase
6
6
  def setup
7
7
  Stats.redis = 'localhost:6379:stats_test'
8
- Stats.redis.flush_db
8
+ Stats.redis.flushdb
9
9
  end
10
10
 
11
11
  def test_should_set
@@ -16,19 +16,19 @@ class TestStats < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def test_should_increment
19
- assert_equal 0, Stats.get("downloads").to_i
19
+ assert_equal 0, Stats.counter("downloads")
20
20
  Stats.incr("downloads")
21
- assert_equal 1, Stats.get("downloads").to_i
21
+ assert_equal 1, Stats.counter("downloads")
22
22
  Stats.incr("downloads", 2)
23
- assert_equal 3, Stats.get("downloads").to_i
23
+ assert_equal 3, Stats.counter("downloads")
24
24
  end
25
25
 
26
26
  def test_should_decrement
27
27
  Stats.set("invitations", 100)
28
28
  Stats.decr("invitations")
29
- assert_equal 99, Stats.get("invitations").to_i
29
+ assert_equal 99, Stats.counter("invitations")
30
30
  Stats.decr("invitations", 4)
31
- assert_equal 95, Stats.get("invitations").to_i
31
+ assert_equal 95, Stats.counter("invitations")
32
32
  end
33
33
 
34
34
  def test_should_clear
@@ -67,6 +67,12 @@ class TestStats < Test::Unit::TestCase
67
67
  Stats.decr("tickets_left", 4)
68
68
  end
69
69
 
70
+ def test_should_get_counter
71
+ Stats.redis = mock('redis')
72
+ Stats.redis.expects(:get).with("downloads").returns("10")
73
+ assert_equal 10, Stats.counter("downloads")
74
+ end
75
+
70
76
  def test_should_clear
71
77
  Stats.redis = mock('redis')
72
78
  Stats.redis.expects(:del).with("downloads")
@@ -3,15 +3,27 @@ require 'mocha'
3
3
  require 'stats'
4
4
 
5
5
  class TestStats < Test::Unit::TestCase
6
- def test_should_average
7
- assert_equal 2.0, Stats::TimingStat.new([1, 2, 2, 3]).average
6
+ def test_should_avg
7
+ assert_equal 2.0, Stats::TimingStat.new([1, 2, 2, 3]).avg
8
8
  end
9
9
 
10
- def test_should_get_population_variance
11
- assert_equal 0.25, Stats::TimingStat.new([1, 1, 2, 2]).population_variance
10
+ def test_should_have_avg_of_nil_with_no_data
11
+ assert_nil Stats::TimingStat.new([]).avg
12
12
  end
13
13
 
14
- def test_should_get_standard_deviation
15
- assert_equal 0.5, Stats::TimingStat.new([1, 1, 2, 2]).standard_deviation
14
+ def test_should_get_pop_var
15
+ assert_equal 0.25, Stats::TimingStat.new([1, 1, 2, 2]).pop_var
16
+ end
17
+
18
+ def test_should_have_pop_var_of_nil_with_no_data
19
+ assert_nil Stats::TimingStat.new([]).pop_var
20
+ end
21
+
22
+ def test_should_get_std_dev
23
+ assert_equal 0.5, Stats::TimingStat.new([1, 1, 2, 2]).std_dev
24
+ end
25
+
26
+ def test_should_have_std_dev_of_nil_with_no_data
27
+ assert_nil Stats::TimingStat.new([]).std_dev
16
28
  end
17
29
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Duncan
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-17 00:00:00 -04:00
17
+ date: 2010-05-09 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency