Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #7 from teeparham/refactor
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
trvsdnn committed May 27, 2013
2 parents 5fc76c4 + 972e6ca commit d4c39b2
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 59 deletions.
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -27,10 +27,10 @@ By default Von will only bump a "total" counter for the given key. This is great
```ruby
Von.configure do |config|
# Keep daily stats for 30 days
config.counter 'downloads', :daily => 30
config.counter 'downloads', daily: 30

# Keep monthly stats for 3 months and yearly stats for 2 years
config.counter 'uploads', :monthly => 3, :yearly => 2
config.counter 'uploads', monthly: 3, yearly: 2
end
```

Expand All @@ -41,10 +41,10 @@ If just wanna track stats on the current minute/hour/day/week/etc, you can set t
```ruby
Von.configure do |config|
# Track downloads stats for the current hour
config.counter 'downloads', :current => :hour
config.counter 'downloads', current: :hour

# Track page views for the current day and week
config.counter 'page-views', :current => [ :day, :week ]
config.counter 'page-views', current: [ :day, :week ]
end
```

Expand All @@ -55,10 +55,10 @@ Time periods are pretty cool, but sometimes you wanna know when you did your bes
```ruby
Von.configure do |config|
# Track the best day for downloads
config.counter 'downloads', :best => :day
config.counter 'downloads', best: :day

# Track the best hour and week for page views
config.counter 'page-views', :best => [ :hour, :week ]
config.counter 'page-views', best: [ :hour, :week ]
end
```

Expand All @@ -79,7 +79,7 @@ Von.increment('page-views')
Von.count('downloads').total #=> 4

# get the monthly counts (returns an Array of Hashes)
Von.count('uploads').per(:month) #=> [ { :timestamp => '2012-03', :count => 3 }, { :timestamp => '2013-04', :count => 1 }, { :timestamp => '2013-05', :count => 0 }]
Von.count('uploads').per(:month) #=> [ { timestamp: '2012-03', count: 3 }, { timestamp: '2013-04', count: 1 }, { timestamp: '2013-05', count: 0 }]

# get the download stats for the hour
Von.count('downloads').this(:hour) #=> 10
Expand All @@ -91,7 +91,7 @@ Von.count('page-views').today #=> 78
Von.count('page-views').current(:day) #=> 78

# get the best day for downloads (returns a Hash)
Von.count('downloads').best(:day) #=> { :timestamp => '2012-03-01', :count => 10 }
Von.count('downloads').best(:day) #=> { timestamp: '2012-03-01', count: 10 }
```

One nice thing to note, if you're counting a time period and there wasn't a value stored for the particular hour/day/week/etc, it'll be populated with a zero, this ensures that if you want 30 days of stats, you get 30 days of stats.
Expand All @@ -105,7 +105,7 @@ Von.configure do |config|
# set the Redis connection to an already existing connection
config.redis = Redis.current
# Initialize a new Redis connection given options
config.redis = { :host => 'localhost', :port => 6379 }
config.redis = { host: 'localhost', port: 6379 }

# rescue Redis connection errors
# if the connection fails, no errors are raised by default
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -7,4 +7,4 @@ Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end

task :default => :test
task default: :test
18 changes: 9 additions & 9 deletions lib/von/counter.rb
Expand Up @@ -26,10 +26,10 @@ def total
def per(unit)
periods = Von.config.periods[@field]

if !Period.time_unit_exists?(unit)
raise ArgumentError, "`#{unit}' is an unknown time unit"
else
if Period.time_unit_exists?(unit)
Counters::Period.new(@field, periods).count(unit)
else
raise ArgumentError, "`#{unit}' is an unknown time unit"
end
rescue Redis::BaseError => e
raise e if Von.config.raise_connection_errors
Expand All @@ -38,10 +38,10 @@ def per(unit)
def best(unit)
periods = Von.config.bests[@field]

if !Period.time_unit_exists?(unit)
raise ArgumentError, "`#{unit}' is an unknown time unit"
else
if Period.time_unit_exists?(unit)
Counters::Best.new(@field, periods).count(unit)
else
raise ArgumentError, "`#{unit}' is an unknown time unit"
end
rescue Redis::BaseError => e
raise e if Von.config.raise_connection_errors
Expand All @@ -50,10 +50,10 @@ def best(unit)
def this(unit)
periods = Von.config.currents[@field]

if !Period.time_unit_exists?(unit)
raise ArgumentError, "`#{unit}' is an unknown time unit"
else
if Period.time_unit_exists?(unit)
Counters::Current.new(@field, periods).count(unit)
else
raise ArgumentError, "`#{unit}' is an unknown time unit"
end
rescue Redis::BaseError => e
raise e if Von.config.raise_connection_errors
Expand Down
6 changes: 3 additions & 3 deletions lib/von/counters/best.rb
@@ -1,7 +1,7 @@
module Von
module Counters
class Best
include Von::Counters::Commands
include Commands

def initialize(field, periods = nil)
@field = field.to_sym
Expand Down Expand Up @@ -57,9 +57,9 @@ def count(time_unit)
_best_total = best_total(time_unit)

if _current_total > _best_total
{ :timestamp => _current_timestamp, :count => _current_total }
{ timestamp: _current_timestamp, count: _current_total }
else
{ :timestamp => _best_timestamp, :count => _best_total }
{ timestamp: _best_timestamp, count: _best_total }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/von/counters/current.rb
@@ -1,7 +1,7 @@
module Von
module Counters
class Current
include Von::Counters::Commands
include Commands

# Initialize a new Counter
#
Expand Down
9 changes: 4 additions & 5 deletions lib/von/counters/period.rb
@@ -1,7 +1,7 @@
module Von
module Counters
class Period
include Von::Counters::Commands
include Commands

def initialize(field, periods = nil)
@field = field.to_sym
Expand Down Expand Up @@ -44,17 +44,16 @@ def increment
def count(time_unit)
return if @periods.empty?

counts = []
this_period = nil
_period = @periods.select { |p| p.time_unit == time_unit }.first
counts = []
_period = @periods.select { |p| p.time_unit == time_unit }.first

_period.length.times do |i|
this_period = _period.prev(i)
counts.unshift(this_period)
end

keys = hgetall(hash_key(time_unit))
counts.map { |date| { :timestamp => date, :count => keys.fetch(date, 0).to_i }}
counts.map { |date| { timestamp: date, count: keys.fetch(date, 0).to_i }}
end

end
Expand Down
3 changes: 2 additions & 1 deletion lib/von/counters/total.rb
@@ -1,7 +1,8 @@
module Von
module Counters
class Total
include Von::Counters::Commands
include Commands

attr_reader :field

# Initialize a new Counter
Expand Down
15 changes: 8 additions & 7 deletions lib/von/period.rb
@@ -1,13 +1,14 @@
module Von
class Period
PERIOD_MAPPING = {
:minutely => :minute,
:hourly => :hour,
:daily => :day,
:weekly => :week,
:monthly => :month,
:yearly => :year
minutely: :minute,
hourly: :hour,
daily: :day,
weekly: :week,
monthly: :month,
yearly: :year
}

AVAILABLE_PERIODS = PERIOD_MAPPING.keys
AVAILABLE_TIME_UNITS = PERIOD_MAPPING.values

Expand Down Expand Up @@ -50,7 +51,7 @@ def minutes?

def beginning(time)
if minutes?
time.change(:seconds => 0)
time.change(seconds: 0)
else
time.send(:"beginning_of_#{time_unit}")
end
Expand Down
11 changes: 5 additions & 6 deletions test/config_test.rb
Expand Up @@ -14,13 +14,12 @@

it 'initializes a config and overloads it with a block' do
@config.namespace = 'something'

@config.namespace.must_equal 'something'
end

it "sets periods via counter method" do
Von.configure do |config|
config.counter 'bar', :monthly => 3, :daily => 6
config.counter 'bar', monthly: 3, daily: 6
end

Von.config.periods[:bar].length.must_equal 2
Expand All @@ -32,8 +31,8 @@

it "sets bests via counter method" do
Von.configure do |config|
config.counter 'bar', :best => :day
config.counter 'foo', :best => [ :month, :year ]
config.counter 'bar', best: :day
config.counter 'foo', best: [:month, :year]
end

Von.config.bests[:bar].first.must_be_instance_of Von::Period
Expand All @@ -46,8 +45,8 @@

it "sets currents via counter method" do
Von.configure do |config|
config.counter 'bar', :current => :day
config.counter 'foo', :current => [ :month, :year ]
config.counter 'bar', current: :day
config.counter 'foo', current: [:month, :year]
end

Von.config.currents[:bar].first.must_be_instance_of Von::Period
Expand Down
13 changes: 6 additions & 7 deletions test/counter_test.rb
Expand Up @@ -21,10 +21,9 @@
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
config.counter 'foo', monthly: 2
end

Von.increment('foo')
Expand All @@ -33,12 +32,12 @@
Timecop.freeze(Time.local(2013, 03))
Von.increment('foo')

Counter.new('foo').per(:month).must_equal [{:timestamp => "2013-02", :count => 1}, {:timestamp => "2013-03", :count => 1}]
Counter.new('foo').per(:month).must_equal [{ timestamp: "2013-02", count: 1 }, { timestamp: "2013-03", count: 1 }]
end

it "returns best count for a given period" do
Von.configure do |config|
config.counter 'foo', :best => [:minute, :week]
config.counter 'foo', best: [:minute, :week]
end

Von.increment('foo')
Expand All @@ -48,13 +47,13 @@
Timecop.freeze(Time.local(2013, 01, 20, 06, 10))
3.times { Von.increment('foo') }

Counter.new('foo').best(:minute).must_equal({:timestamp => "2013-01-13 06:05", :count => 4})
Counter.new('foo').best(:week).must_equal({:timestamp => "2013-01-07", :count => 4})
Counter.new('foo').best(:minute).must_equal({ timestamp: "2013-01-13 06:05", count: 4 })
Counter.new('foo').best(:week).must_equal({ timestamp: "2013-01-07", count: 4 })
end

it "returns current count for a given period" do
Von.configure do |config|
config.counter 'foo', :current => [:minute, :day]
config.counter 'foo', current: [:minute, :day]
end

4.times { Von.increment('foo') }
Expand Down
14 changes: 7 additions & 7 deletions test/counters/period_test.rb
Expand Up @@ -64,14 +64,14 @@
Timecop.freeze(Time.local(2013, 02, 01, 9))
counter.increment

counter.count(:month).must_equal [{:timestamp => "2013-02", :count => 2}]
counter.count(:month).must_equal [{ timestamp: "2013-02", count: 2 }]
counter.count(:hour).must_equal [
{ :timestamp => "2013-02-01 04:00", :count => 0 },
{ :timestamp => "2013-02-01 05:00", :count => 0 },
{ :timestamp => "2013-02-01 06:00", :count => 0 },
{ :timestamp => "2013-02-01 07:00", :count => 1 },
{ :timestamp => "2013-02-01 08:00", :count => 0 },
{ :timestamp => "2013-02-01 09:00", :count => 1 }
{ timestamp: "2013-02-01 04:00", count: 0 },
{ timestamp: "2013-02-01 05:00", count: 0 },
{ timestamp: "2013-02-01 06:00", count: 0 },
{ timestamp: "2013-02-01 07:00", count: 1 },
{ timestamp: "2013-02-01 08:00", count: 0 },
{ timestamp: "2013-02-01 09:00", count: 1 }
]
end

Expand Down
6 changes: 3 additions & 3 deletions test/von_test.rb
Expand Up @@ -22,7 +22,7 @@

it "increments period/best counters and counts them" do
Von.configure do |config|
config.counter 'foo', :monthly => 2, :best => :day
config.counter 'foo', monthly: 2, best: :day
end

Von.increment('foo')
Expand All @@ -32,8 +32,8 @@
Timecop.freeze(Time.local(2013, 03, 04))
Von.increment('foo')

Von.count('foo').best(:day).must_equal({:timestamp => "2013-02-03", :count => 2})
Von.count('foo').per(:month).must_equal [{:timestamp => "2013-02", :count => 2}, {:timestamp => "2013-03", :count => 1}]
Von.count('foo').best(:day).must_equal({ timestamp: "2013-02-03", count: 2 })
Von.count('foo').per(:month).must_equal [{ timestamp: "2013-02", count: 2 }, { timestamp: "2013-03", count: 1 }]
end

it "raises a Redis connection errors if raise_connection_errors is true" do
Expand Down

0 comments on commit d4c39b2

Please sign in to comment.