Permalink
Browse files

Merge pull request #4 from thegrubbsian/master

Added 'minutely' for tracking by the minute.
  • Loading branch information...
2 parents 6e5e2ae + bbc3ae4 commit 075a53dd477fa5ce98f85ed5d5444b5f2d3008ef @blahed committed Jan 29, 2013
Showing with 29 additions and 5 deletions.
  1. +4 −1 lib/von/config.rb
  2. +3 −1 lib/von/period.rb
  3. +16 −1 test/counter_test.rb
  4. +6 −2 test/period_test.rb
View
@@ -11,6 +11,7 @@ module Config
attr_accessor :weekly_format
attr_accessor :daily_format
attr_accessor :hourly_format
+ attr_accessor :minutely_format
attr_reader :periods
@@ -31,6 +32,8 @@ def init!
self.daily_format = '%Y-%m-%d'
# 2013-01-02 12:00
self.hourly_format = '%Y-%m-%d %H:00'
+ # 2013-01-02 12:05
+ self.minutely_format = '%Y-%m-%d %H:%M'
end
# Set the Redis connection to use
@@ -78,4 +81,4 @@ def counter_options(field)
end
end
-end
+end
View
@@ -1,6 +1,6 @@
module Von
class Period
- AVAILABLE_PERIODS = [ :hourly, :daily, :weekly, :monthly, :yearly ]
+ AVAILABLE_PERIODS = [ :minutely, :hourly, :daily, :weekly, :monthly, :yearly ]
attr_reader :counter_key
attr_reader :length
@@ -22,6 +22,8 @@ def initialize(counter_key, period, length)
# for the current period.
def time_unit
@time_unit ||= case @period
+ when :minutely
+ :minute
when :hourly
:hour
when :daily
View
@@ -4,7 +4,7 @@
Counter = Von::Counter
before :each do
- Timecop.freeze(Time.local(2013, 01))
+ Timecop.freeze(Time.local(2013, 01, 01, 01, 01))
Von.config.init!
mock_connection!
end
@@ -47,6 +47,21 @@
@store['von:lists:foo:monthly'].size.must_equal 1
end
+ it 'increments a minute counter' do
+ Von.configure do |config|
+ config.counter 'foo', :minutely => 60
+ end
+
+ Counter.increment('foo')
+ Counter.increment('foo')
+
+ @store.has_key?('von:counters:foo').must_equal true
+ @store.has_key?('von:counters:foo:minutely').must_equal true
+ @store['von:counters:foo']['total'].must_equal 2
+ @store['von:counters:foo:minutely']['2013-01-01 01:01'].must_equal 2
+ @store['von:lists:foo:minutely'].size.must_equal 1
+ end
+
it "expires counters past the limit" do
Von.configure do |config|
config.counter 'foo', :monthly => 1
View
@@ -14,6 +14,7 @@
end
it "checks if the period is an hourly period" do
+ Period.new('foo', :minutely, 6).wont_be :hours?
Period.new('foo', :hourly, 6).must_be :hours?
Period.new('foo', :daily, 6).wont_be :hours?
Period.new('foo', :weekly, 6).wont_be :hours?
@@ -22,6 +23,7 @@
end
it "knows what time unit it is" do
+ Period.new('foo', :minutely, 6).time_unit.must_equal :minute
Period.new('foo', :hourly, 6).time_unit.must_equal :hour
Period.new('foo', :daily, 6).time_unit.must_equal :day
Period.new('foo', :weekly, 6).time_unit.must_equal :week
@@ -30,6 +32,7 @@
end
it "pulls a time format from config options" do
+ Period.new('foo', :minutely, 6).format.must_equal Von.config.minutely_format
Period.new('foo', :hourly, 6).format.must_equal Von.config.hourly_format
Period.new('foo', :daily, 6).format.must_equal Von.config.daily_format
Period.new('foo', :weekly, 6).format.must_equal Von.config.weekly_format
@@ -54,12 +57,13 @@
end
it "builds a redis field for the given period and current time" do
- Timecop.freeze(Time.local(2013, 02, 01, 05))
+ Timecop.freeze(Time.local(2013, 02, 01, 05, 15))
+ Period.new('foo', :minutely, 6).field.must_equal '2013-02-01 05:15'
Period.new('foo', :hourly, 6).field.must_equal '2013-02-01 05:00'
Period.new('foo', :daily, 6).field.must_equal '2013-02-01'
Period.new('foo', :weekly, 6).field.must_equal '2013-02-01'
Period.new('foo', :monthly, 6).field.must_equal '2013-02'
Period.new('foo', :yearly, 6).field.must_equal '2013'
end
-end
+end

0 comments on commit 075a53d

Please sign in to comment.