Skip to content

Commit

Permalink
rename hash/list to hash_key/list_key and add tests for period class
Browse files Browse the repository at this point in the history
  • Loading branch information
trvsdnn committed Jan 24, 2013
1 parent 0eee213 commit 59b21fb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
22 changes: 11 additions & 11 deletions lib/von/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ def periods
}.inject({}) { |h, (p, l)| h[p] = Period.new(@field, p, l); h }
end

def hash
@hash ||= "#{Von.config.namespace}:#{@field}"
def hash_key
@hash_key ||= "#{Von.config.namespace}:#{@field}"
end

def increment(field = 'total')
if field.is_a?(Period)
increment_period(field)
else
Von.connection.hincrby(hash, field, 1)
Von.connection.hincrby(hash_key, field, 1)
increment_parents
end
end

def increment_period(period)
Von.connection.hincrby(period.hash, period.field, 1)
unless Von.connection.lrange(period.list, 0, -1).include?(period.field)
Von.connection.rpush(period.list, period.field)
Von.connection.hincrby(period.hash_key, period.field, 1)
unless Von.connection.lrange(period.list_key, 0, -1).include?(period.field)
Von.connection.rpush(period.list_key, period.field)
end

if Von.connection.llen(period.list) > period.length
expired_counter = Von.connection.lpop(period.list)
Von.connection.hdel(period.hash, expired_counter)
if Von.connection.llen(period.list_key) > period.length
expired_counter = Von.connection.lpop(period.list_key)
Von.connection.hdel(period.hash_key, expired_counter)
end
end

Expand All @@ -56,7 +56,7 @@ def increment_parents

def count(period)
if period.nil?
Von.connection.hget(hash, 'total')
Von.connection.hget(hash_key, 'total')
else
_count = []
_period = periods[period]
Expand All @@ -68,7 +68,7 @@ def count(period)
now = _period.hours? ? now.ago(3600) : now.send(:"prev_#{_period.time_unit}")
end

keys = Von.connection.hgetall("#{hash}:#{period}")
keys = Von.connection.hgetall("#{hash_key}:#{period}")
_count.map { |date| { date => keys.fetch(date, 0) }}
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/von/period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Von
class Period
AVAILABLE_PERIODS = [ :hourly, :daily, :weekly, :monthly, :yearly ]

attr_reader :counter
attr_reader :length

def initialize(counter, period, length)
Expand Down Expand Up @@ -34,11 +35,11 @@ def format
@format ||= Von.config.send(:"#{@period}_format")
end

def hash
def hash_key
@hash ||= "#{Von.config.namespace}:#{@counter}:#{@period}"
end

def list
def list_key
@list ||= "#{Von.config.namespace}:lists:#{@counter}:#{@period}"
end

Expand Down
2 changes: 0 additions & 2 deletions test/config_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require 'test_helper'

require 'test_helper'

describe Von::Config do

before :each do
Expand Down
Empty file removed test/period_test.tb
Empty file.

0 comments on commit 59b21fb

Please sign in to comment.