Skip to content

Commit

Permalink
Merge pull request #4 from thegrubbsian/master
Browse files Browse the repository at this point in the history
Added 'minutely' for tracking by the minute.
  • Loading branch information
trvsdnn committed Jan 29, 2013
2 parents 6e5e2ae + bbc3ae4 commit 075a53d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/von/config.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -78,4 +81,4 @@ def counter_options(field)
end

end
end
end
4 changes: 3 additions & 1 deletion 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
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion test/counter_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions test/period_test.rb
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.