Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
counter instance for dispatching count queries, support for bests closes
- Loading branch information
Showing
7 changed files
with
139 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Von | ||
class Counter | ||
|
||
def initialize(field) | ||
@field = field.to_sym | ||
end | ||
|
||
def to_s | ||
Counters::Total.new(@field).count.to_s | ||
rescue Redis::BaseError => e | ||
raise e if Von.config.raise_connection_errors | ||
end | ||
|
||
def to_i | ||
Counters::Total.new(@field).count | ||
rescue Redis::BaseError => e | ||
raise e if Von.config.raise_connection_errors | ||
end | ||
|
||
def total | ||
Counters::Total.new(@field).count | ||
rescue Redis::BaseError => e | ||
raise e if Von.config.raise_connection_errors | ||
end | ||
|
||
def per(unit) | ||
periods = Von.config.periods[@field] | ||
period = Period.unit_to_period(unit) | ||
|
||
if period.nil? | ||
raise ArgumentError, "`#{unit}' is an unknown time unit" | ||
else | ||
Counters::Period.new(@field, periods).count(period) | ||
end | ||
rescue Redis::BaseError => e | ||
raise e if Von.config.raise_connection_errors | ||
end | ||
|
||
def best(unit) | ||
periods = Von.config.bests[@field] | ||
period = Period.unit_to_period(unit) | ||
|
||
if period.nil? | ||
raise ArgumentError, "`#{unit}' is an unknown time unit" | ||
else | ||
Counters::Best.new(@field, periods).count(period) | ||
end | ||
rescue Redis::BaseError => e | ||
raise e if Von.config.raise_connection_errors | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'test_helper' | ||
|
||
describe Von::Counter do | ||
Counter = Von::Counter | ||
|
||
before :each do | ||
Timecop.freeze(Time.local(2013, 01, 01, 01, 01)) | ||
Von.config.init! | ||
@redis = Redis.new | ||
@redis.flushall | ||
end | ||
|
||
it "returns count for key" do | ||
3.times { Von.increment('foo') } | ||
Counter.new('foo').total.must_equal 3 | ||
end | ||
|
||
it "returns count for key and parent keys" do | ||
3.times { Von.increment('foo:bar') } | ||
Counter.new('foo').total.must_equal 3 | ||
Counter.new('foo:bar').total.must_equal 3 | ||
end | ||
|
||
|
||
it "returns counts for a given period" do | ||
Von.configure do |config| | ||
config.counter 'foo', :monthly => 2 | ||
end | ||
|
||
Von.increment('foo') | ||
Timecop.freeze(Time.local(2013, 02)) | ||
Von.increment('foo') | ||
Timecop.freeze(Time.local(2013, 03)) | ||
Von.increment('foo') | ||
|
||
Counter.new('foo').per(:month).must_equal [{ "2013-02" => 1 }, { "2013-03" => 1 }] | ||
end | ||
|
||
it "returns best count for a given period" do | ||
Von.configure do |config| | ||
config.counter 'foo', :best => [:minute, :week] | ||
end | ||
|
||
Von.increment('foo') | ||
|
||
Timecop.freeze(Time.local(2013, 01, 13, 06, 05)) | ||
4.times { Von.increment('foo') } | ||
Timecop.freeze(Time.local(2013, 01, 20, 06, 10)) | ||
3.times { Von.increment('foo') } | ||
|
||
Counter.new('foo').best(:minute).must_equal({ "2013-01-13 06:05" => 4 }) | ||
Counter.new('foo').best(:week).must_equal({ "2013-01-07" => 4 }) | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters