diff --git a/lib/von/config.rb b/lib/von/config.rb index 38a196e..f538721 100644 --- a/lib/von/config.rb +++ b/lib/von/config.rb @@ -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 \ No newline at end of file +end diff --git a/lib/von/period.rb b/lib/von/period.rb index c0fd60f..0f01bac 100644 --- a/lib/von/period.rb +++ b/lib/von/period.rb @@ -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 diff --git a/test/counter_test.rb b/test/counter_test.rb index f41a01c..d762e13 100644 --- a/test/counter_test.rb +++ b/test/counter_test.rb @@ -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 diff --git a/test/period_test.rb b/test/period_test.rb index 057c4b4..388fecb 100644 --- a/test/period_test.rb +++ b/test/period_test.rb @@ -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,7 +57,8 @@ 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' @@ -62,4 +66,4 @@ Period.new('foo', :yearly, 6).field.must_equal '2013' end -end \ No newline at end of file +end