Permalink
Browse files

use period.name not to_s

  • Loading branch information...
blahed committed Jan 31, 2013
1 parent dfd09e5 commit 092cfa09c40148d33c64104d80d9aeb4f834a1d1
Showing with 44 additions and 50 deletions.
  1. +12 −12 lib/von/counter.rb
  2. +20 −21 lib/von/counters/best.rb
  3. +6 −6 lib/von/counters/period.rb
  4. +0 −5 lib/von/period.rb
  5. +2 −2 test/counter_test.rb
  6. +3 −3 test/period_test.rb
  7. +1 −1 test/von_test.rb
View
@@ -1,32 +1,32 @@
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
@@ -35,19 +35,19 @@ def per(unit)
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?
+ period_name = Period.unit_to_period(unit)
+
+ if period_name.nil?
raise ArgumentError, "`#{unit}' is an unknown time unit"
else
- Counters::Best.new(@field, periods).count(period)
+ Counters::Best.new(@field, periods).count(period_name)
end
rescue Redis::BaseError => e
raise e if Von.config.raise_connection_errors
end
-
+
end
end
View
@@ -12,50 +12,49 @@ def hash_key
@hash_key ||= "#{Von.config.namespace}:counters:bests:#{@field}"
end
- def best_total(period)
- hget("#{hash_key}:#{period}:best", 'total').to_i
+ def best_total(period_name)
+ hget("#{hash_key}:#{period_name}:best", 'total').to_i
end
- def best_timestamp(period)
- hget("#{hash_key}:#{period}:best", 'timestamp')
+ def best_timestamp(period_name)
+ hget("#{hash_key}:#{period_name}:best", 'timestamp')
end
- def current_total(period)
- hget("#{hash_key}:#{period}:current", 'total').to_i
+ def current_total(period_name)
+ hget("#{hash_key}:#{period_name}:current", 'total').to_i
end
- def current_timestamp(period)
- hget("#{hash_key}:#{period}:current", 'timestamp')
+ def current_timestamp(period_name)
+ hget("#{hash_key}:#{period_name}:current", 'timestamp')
end
def increment
return if @periods.empty?
@periods.each do |period|
- # TODO: subclass counter (or somethin) and add hincrby/etc helpers
- _current_timestamp = current_timestamp(period)
- _current_total = current_total(period)
+ _current_timestamp = current_timestamp(period.name)
+ _current_total = current_total(period.name)
if period.timestamp != _current_timestamp
# changing current period
- hset("#{hash_key}:#{period}:current", 'total', 1)
- hset("#{hash_key}:#{period}:current", 'timestamp', period.timestamp)
+ hset("#{hash_key}:#{period.name}:current", 'total', 1)
+ hset("#{hash_key}:#{period.name}:current", 'timestamp', period.timestamp)
if best_total(period) < _current_total
- hset("#{hash_key}:#{period}:best", 'total', _current_total)
- hset("#{hash_key}:#{period}:best", 'timestamp', _current_timestamp)
+ hset("#{hash_key}:#{period.name}:best", 'total', _current_total)
+ hset("#{hash_key}:#{period.name}:best", 'timestamp', _current_timestamp)
end
else
- hincrby("#{hash_key}:#{period}:current", 'total', 1)
+ hincrby("#{hash_key}:#{period.name}:current", 'total', 1)
end
end
end
- def count(period)
- _current_timestamp = current_timestamp(period)
- _current_total = current_total(period)
- _best_timestamp = best_timestamp(period)
- _best_total = best_total(period)
+ def count(period_name)
+ _current_timestamp = current_timestamp(period_name)
+ _current_total = current_total(period_name)
+ _best_timestamp = best_timestamp(period_name)
+ _best_total = best_total(period_name)
if _current_total > _best_total
{ _current_timestamp => _current_total }
View
@@ -9,21 +9,21 @@ def initialize(field, periods = nil)
end
# Returns the Redis hash key used for storing counts for this Period
- def hash_key(period)
- "#{Von.config.namespace}:counters:#{@field}:#{period}"
+ def hash_key(name)
+ "#{Von.config.namespace}:counters:#{@field}:#{name}"
end
# Returns the Redis list key used for storing current "active" counters
- def list_key(period)
- "#{Von.config.namespace}:lists:#{@field}:#{period}"
+ def list_key(name)
+ "#{Von.config.namespace}:lists:#{@field}:#{name}"
end
def increment
return if @periods.empty?
@periods.each do |period|
- _hash_key = hash_key(period)
- _list_key = list_key(period)
+ _hash_key = hash_key(period.name)
+ _list_key = list_key(period.name)
hincrby(_hash_key, period.timestamp, 1)
View
@@ -32,11 +32,6 @@ def initialize(period, length = nil)
@format = Von.config.send(:"#{@name}_format")
end
- # TODO: take this out, dirty
- def to_s
- @name.to_s
- end
-
# Returns a Symbol representing the time unit
# for the current period.
def time_unit
View
@@ -35,7 +35,7 @@
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]
@@ -47,7 +47,7 @@
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
View
@@ -11,21 +11,21 @@
it "intiializes given a period" do
period = Period.new(:monthly)
- period.to_s.must_equal 'monthly'
+ period.name.must_equal :monthly
period.length.must_be_nil
period.format.must_equal '%Y-%m'
end
it "intiializes given a time unit" do
period = Period.new(:month)
- period.to_s.must_equal 'monthly'
+ period.name.must_equal :monthly
period.length.must_be_nil
period.format.must_equal '%Y-%m'
end
it "intiializes given a period and length" do
period = Period.new(:monthly, 3)
- period.to_s.must_equal 'monthly'
+ period.name.must_equal :monthly
period.length.must_equal 3
period.format.must_equal '%Y-%m'
end
View
@@ -13,7 +13,7 @@
3.times { Von.increment('foo') }
Von.count('foo').total.must_equal 3
end
-
+
it "increments a counter and parent counters and counts them" do
3.times { Von.increment('foo:bar') }
Von.count('foo').total.must_equal 3

0 comments on commit 092cfa0

Please sign in to comment.