-
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.
- Loading branch information
blahed
committed
Jan 24, 2013
0 parents
commit 30f7d7b
Showing
13 changed files
with
516 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
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,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in von.gemspec | ||
gemspec |
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,22 @@ | ||
Copyright (c) 2013 blahed | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,47 @@ | ||
# Von | ||
|
||
TODO: Write a gem description | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'von' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install von | ||
|
||
## Usage | ||
|
||
# Configuration and Expiration | ||
Von.configure do | ||
# Set the redis namespace (all keys are prefixed with this) | ||
self.namespace = 'forward:stats' | ||
|
||
# Set the format of monthly stored keys | ||
self.monthly_format = '%M %Y' | ||
|
||
# Keep daily stats for 30 days | ||
counter 'something:foo', :daily => 30 | ||
|
||
# Keep monthly stats for 3 months and yearly stats for 2 years | ||
counter 'something', :monthly => 3, :yearly => 2 | ||
end | ||
|
||
# Single keys | ||
Von.increment('something') # bumps 'something' | ||
Von.increment('something:else') # bumps 'something' (total only) and 'something:else' | ||
Von.increment('foo') # bumps 'foo' | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
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,8 @@ | ||
#!/usr/bin/env rake | ||
require 'rake/testtask' | ||
require 'bundler/gem_tasks' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
t.test_files = FileList['test/*_test.rb'] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'von/config' | ||
require 'von/period' | ||
require 'von/version' | ||
|
||
require 'active_support/all' | ||
require 'redis' | ||
|
||
module Von | ||
def self.connection | ||
@connection ||= Redis.current | ||
end | ||
|
||
def self.config | ||
Config | ||
end | ||
|
||
def self.increment(counter) | ||
periods = config.counter_periods(counter) | ||
|
||
connection.hincrby("#{config.namespace}:#{counter}", 'all', 1) | ||
|
||
periods.each do |period, length| | ||
period = Period.new(counter, period) | ||
connection.hincrby(period.hash, period.key, 1) | ||
connection.rpush(period.list, period.key) unless connection.lrange(period.list, 0, -1).include?(period.key) | ||
|
||
if connection.llen(period.list) > length | ||
expired_counter = connection.lpop(period.list) | ||
connection.hdel(period.hash, expired_counter) | ||
end | ||
end | ||
|
||
increment_parents(counter) if counter =~ /:[^:]+\z/ | ||
end | ||
|
||
def self.increment_parents(counter) | ||
parents = counter.sub(/:[^:]+\z/, '') | ||
|
||
until parents.empty? do | ||
connection.hincrby("#{config.namespace}:#{parents}", 'all', 1) | ||
parents.sub!(/:?[^:]+\z/, '') | ||
end | ||
end | ||
|
||
def self.count(counter, period = nil) | ||
if period.nil? | ||
connection.hget("#{config.namespace}:#{counter}", 'all') | ||
else | ||
count = [] | ||
_period = Period.new(counter, period) | ||
length = config.counter_periods(counter)[period.to_sym] | ||
now = DateTime.now.beginning_of_hour | ||
|
||
length.times do | ||
this_period = now.strftime(_period.format) | ||
count.unshift(this_period) | ||
now = _period.time_unit == :hour ? now.ago(3600) : now.send(:"prev_#{_period.time_unit}") | ||
end | ||
|
||
counter = "#{config.namespace}:#{counter}:#{period}" | ||
keys = connection.hgetall(counter) | ||
count.map { |date| { date => keys.fetch(date, 0) }} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'yaml' | ||
|
||
module Von | ||
module Config | ||
extend self | ||
|
||
attr_accessor :namespace | ||
attr_accessor :yearly_format | ||
attr_accessor :monthly_format | ||
attr_accessor :weekly_format | ||
attr_accessor :daily_format | ||
attr_accessor :hourly_format | ||
|
||
def reset! | ||
@counter_options = {} | ||
|
||
self.namespace = 'von' | ||
# 2013 | ||
self.yearly_format = '%Y' | ||
# 2013-01 | ||
self.monthly_format = '%Y-%m' | ||
# 2013-01-02 | ||
self.weekly_format = '%Y-%m-%d' | ||
# 2013-01-02 | ||
self.daily_format = '%Y-%m-%d' | ||
# 2013-01-02 12:00 | ||
self.hourly_format = '%Y-%m-%d %H:00' | ||
end | ||
|
||
def counter(key, options = {}) | ||
@counter_options[key] = options | ||
end | ||
|
||
def counter_options(counter) | ||
@counter_options[counter] ||= {} | ||
end | ||
|
||
def counter_periods(counter) | ||
counter_options(counter).select do |k| | ||
Period::AVAILABLE_PERIODS.include?(k) | ||
end | ||
end | ||
|
||
def from_hash!(attributes_hash = {}) | ||
return if attributes_hash.empty? | ||
|
||
attributes_hash.each do |key, value| | ||
send(:"#{key}=", value) | ||
end | ||
end | ||
|
||
def configure(&block) | ||
instance_eval &block | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Von | ||
class Period | ||
AVAILABLE_PERIODS = [ :hourly, :daily, :weekly, :monthly, :yearly ] | ||
|
||
def initialize(counter, period) | ||
@counter = counter | ||
@period = period | ||
@now = Time.now | ||
end | ||
|
||
def time_unit | ||
case @period | ||
when :hourly | ||
:hour | ||
when :daily | ||
:day | ||
when :weekly | ||
:week | ||
when :monthly | ||
:month | ||
when :yearly | ||
:year | ||
end | ||
end | ||
|
||
def format | ||
Von.config.send(:"#{@period}_format") | ||
end | ||
|
||
def hash | ||
"#{Von.config.namespace}:#{@counter}:#{@period}" | ||
end | ||
|
||
def list | ||
"#{Von.config.namespace}:lists:#{@counter}:#{@period}" | ||
end | ||
|
||
def key | ||
@now.strftime(format) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Von | ||
VERSION = '0.0.1' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'test_helper' | ||
|
||
require 'test_helper' | ||
|
||
describe Von::Config do | ||
|
||
before :each do | ||
@config = Von::Config | ||
@config.reset! | ||
end | ||
|
||
it 'intiializes a config with defaults' do | ||
@config.namespace.must_equal 'von' | ||
@config.hourly_format.must_equal '%Y-%m-%d %H:00' | ||
end | ||
|
||
it 'initializes a config with a hash' do | ||
attributes = { | ||
:namespace => 'foo', | ||
} | ||
|
||
@config.from_hash!(attributes) | ||
|
||
attributes.each do |key, value| | ||
@config.send(key).must_equal value | ||
end | ||
end | ||
|
||
it 'initializes a config and overloads it with a block' do | ||
@config.configure do | ||
self.namespace = 'something' | ||
end | ||
|
||
@config.namespace.must_equal 'something' | ||
end | ||
|
||
it 'stores counter options per key and retrieves them' do | ||
options = { :monthly => 3, :total => false } | ||
|
||
@config.configure do | ||
counter 'bar', options | ||
end | ||
|
||
@config.namespace.must_equal 'von' | ||
@config.counter_options('bar').must_equal options | ||
end | ||
|
||
it 'stores counter options per key and retrieves them' do | ||
options = { :monthly => 3, :total => false } | ||
|
||
@config.configure do | ||
counter 'bar', options | ||
end | ||
|
||
@config.namespace.must_equal 'von' | ||
@config.counter_periods('bar').must_equal :monthly => 3 | ||
end | ||
|
||
end |
Oops, something went wrong.