-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
early implementation of "best" counters
- Loading branch information
Showing
5 changed files
with
96 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Von | ||
class BestCounter | ||
|
||
def initialize(parent) | ||
@parent = parent | ||
@field = parent.field | ||
@periods = Von.config.bests[@field] | ||
end | ||
|
||
# Returns the Redis hash key used for storing counts for this Counter | ||
def hash_key | ||
@hash_key ||= "#{Von.config.namespace}:counters:bests:#{@field}" | ||
end | ||
|
||
def increment | ||
return if @periods.nil? | ||
|
||
@periods.each do |period| | ||
# TODO: subclass counter (or somethin) and add hincrby/etc helpers | ||
|
||
current_timestamp = Von.connection.hget("#{hash_key}:#{period}:current", 'timestamp') | ||
|
||
# TODO: this logic "seems" backwards, rethink current_timestamp | ||
if period.timestamp != current_timestamp | ||
# changing current period | ||
current_total = Von.connection.hget("#{hash_key}:#{period}:current", 'total').to_i | ||
best_total = Von.connection.hget("#{hash_key}:#{period}:best", 'total').to_i | ||
|
||
Von.connection.hset("#{hash_key}:#{period}:current", 'total', 1) | ||
Von.connection.hset("#{hash_key}:#{period}:current", 'timestamp', period.timestamp) | ||
|
||
if best_total < current_total | ||
Von.connection.hset("#{hash_key}:#{period}:best", 'total', current_total) | ||
Von.connection.hset("#{hash_key}:#{period}:best", 'timestamp', current_timestamp) | ||
end | ||
else | ||
Von.connection.hincrby("#{hash_key}:#{period}:current", 'total', 1) | ||
end | ||
end | ||
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,31 @@ | ||
require 'test_helper' | ||
|
||
describe Von::BestCounter do | ||
Counter = Von::BestCounter | ||
|
||
before :each do | ||
Timecop.freeze(Time.local(2013, 01, 01)) | ||
Von.config.init! | ||
@redis = Redis.new | ||
@redis.flushall | ||
end | ||
|
||
it "increments the best counter for a period" do | ||
Von.configure do |config| | ||
config.counter 'foo', :best => :day | ||
end | ||
|
||
Counter.increment('foo') | ||
|
||
Timecop.freeze(Time.local(2013, 01, 02)) | ||
10.times { Counter.increment('foo') } | ||
Timecop.freeze(Time.local(2013, 01, 03)) | ||
3.times { Counter.increment('foo') } | ||
|
||
@redis.hget('von:counters:bests:foo:daily:current', 'timestamp').must_equal '2013-01-03' | ||
@redis.hget('von:counters:bests:foo:daily:current', 'total').must_equal '3' | ||
@redis.hget('von:counters:bests:foo:daily:best', 'timestamp').must_equal '2013-01-02' | ||
@redis.hget('von:counters:bests:foo:daily:best', 'total').must_equal '10' | ||
end | ||
|
||
end |