15 Commits

Author SHA1 Message Date
65e7745419 Merge branch 'release/v0.0.4' 2010-11-24 13:51:03 +00:00
490356ee96 Bumped version to 0.0.4 2010-11-24 13:50:54 +00:00
a6c4600aa5 Merge branch 'feature/new_gemspec' into dev 2010-11-24 13:49:51 +00:00
85ba61b2cc removed jeweler's VERSION file 2010-11-24 13:48:48 +00:00
8f6a4a6820 fixed a typo 2010-11-24 13:47:51 +00:00
81ee2ec0b6 added console rake task 2010-11-24 13:46:11 +00:00
20280f2c5d switched to using bundle's own gemspec format
instead of jeweler
2010-11-24 13:46:03 +00:00
bf29696c46 Merge branch 'release/v0.0.3' into dev 2010-11-24 00:38:38 +00:00
92375b229a Merge branch 'release/v0.0.3' 2010-11-24 00:38:32 +00:00
e362c93d9a Version bump to 0.0.3 2010-11-24 00:38:19 +00:00
0f5b7449b0 updated readme 2010-11-24 00:37:28 +00:00
ea732b4734 updated gemfile and rakefile 2010-11-24 00:37:10 +00:00
62c3492c93 some whitespace cleanup 2010-11-24 00:36:44 +00:00
c5f52455cc replaced Redistat::Model#find alias to #fetch with
#lookup instead to avoid conflicts with
ActiveRecord
2010-11-24 00:36:25 +00:00
1226f8b89a Merge branch 'release/v0.0.2' into dev 2010-11-22 13:30:19 +00:00
12 changed files with 140 additions and 101 deletions

8
.gitignore vendored
View File

@@ -16,11 +16,11 @@ tmtags
## PROJECT::GENERAL
coverage
rdoc
pkg
pkg/*
*.gem
.bundle
## PROJECT::SPECIFIC
.bundle/*
.yardoc/*
spec/db/*
doc
redistat.gemspec
doc/*

11
Gemfile
View File

@@ -1,12 +1,9 @@
source 'http://rubygems.org/'
gem 'activesupport', '>= 2.3.0'
gem 'json', '>= 1.0.0'
gem 'redis', '>= 2.0.0'
gem 'time_ext', '>= 0.2.6'
# Specify your gem's dependencies in redistat.gemspec
gemspec
group :development do
gem 'rspec', '>= 2.0.1'
gem 'yard', '>= 0.6.1'
gem 'i18n'
gem 'rspec', '>= 2.1.0'
gem 'yard', '>= 0.6.3'
end

View File

@@ -1,3 +1,13 @@
PATH
remote: .
specs:
redistat (0.0.4)
activesupport (>= 2.3.0)
json (>= 1.4.6)
redis (>= 2.1.1)
system_timer (>= 1.0.0)
time_ext (>= 0.2.8)
GEM
remote: http://rubygems.org/
specs:
@@ -14,8 +24,10 @@ GEM
rspec-expectations (2.1.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.1.0)
time_ext (0.2.6)
system_timer (1.0)
time_ext (0.2.8)
activesupport (>= 2.3.0)
i18n (>= 0.4.2)
yard (0.6.3)
PLATFORMS
@@ -23,9 +35,10 @@ PLATFORMS
DEPENDENCIES
activesupport (>= 2.3.0)
i18n
json (>= 1.0.0)
redis (>= 2.0.0)
rspec (>= 2.0.1)
time_ext (>= 0.2.6)
yard (>= 0.6.1)
json (>= 1.4.6)
redis (>= 2.1.1)
redistat!
rspec (>= 2.1.0)
system_timer (>= 1.0.0)
time_ext (>= 0.2.8)
yard (>= 0.6.3)

View File

@@ -2,11 +2,66 @@
A Redis-backed statistics storage and querying library written in Ruby.
## Early Beta
Redistat was originally created to replace a small hacked together statistics collection solution which was MySQL-based. When I started I had a short list of requirements:
Currently this is an early beta release. Readme and documentation is forthcoming.
* Store and increment/decrement integer values (counters, etc)
* Up to the second statistics available at all times
* Screamingly fast
For now, please check `spec/model_spec.rb` and `spec/model_helper.rb` to get started with how to use Redistat.
Redis fits perfectly with all of these requirements. It has atomic operations like increment, and it's lightning fast, meaning if the data is structured well, the initial stats reporting call will store data in a format that's instantly retrievable just as fast.
## Installation
gem install redistat
## Usage
The simplest way to use Redistat is through the model wrapper.
class VisitorStats
include Redistat::Model
end
Before any of you Rails-purists start complaining about the model name being plural, I want to point out that it makes sense with Redistat, cause a model doesn't exactly return a specific row or object. But I'm getting ahead of myself.
To store statistics we essentially tell Redistat that an event has occurred with a label of X, and statistics of Y. So let's say we want to store a page view event on the `/about` page on a site:
VisitorStats.store('/about', {:views => 1})
In the above case "`/about`" is the label under which the stats are grouped, and the statistics associated with the event is simply a normal Ruby hash, except all values need to be integers, or Redis' increment calls won't work.
To later retrieve statistics, we use the `fetch` method:
stats = VisitorStats.fetch('/about', 2.hour.ago, Time.now)
# stats => [{:views => 1}]
# stats.total => {:views => 1}
The fetch method requires 3 arguments, a label, a start time, and an end time. Fetch returns a `Redistat::Collection` object, which is normal Ruby Array with a couple of added methods, like total shown above.
For more detailed usage, please check spec files, and source code till I have time to write up a complete readme.
## Some Technical Details
To give a brief look into how Redistat works internally to store statistics, I'm going to use the examples above. The store method accepts a Ruby Hash with statistics to store. Redistat stores all statistics as hashes in Redis. It stores summaries of the stats for the specific time when it happened and all it's parent time groups (second, minute, hour, day, month, year). The default depth Redistat goes to is hour, unless the `depth` option is passed to `store` or `fetch`.
In short, the above call to `store` creates the following keys in Redis:
VisitorStats//about:2010
VisitorStats//about:201011
VisitorStats//about:20101124
VisitorStats//about:2010112401
Each of these keys in Redis are a hash, containing the sums of each statistic point reported for the time frame the key represents. In this case there's two slashes, cause the label we used was “`/about`”, and the scope (class name when used through the model wrapper) and the label are separated with a slash.
When retrieving statistics for a given date range, Redistat figures out how to do the least number of calls to Redis to fetch all relevant data. For example, if you want the sum of stats from the 4th till the last of November, the full month of November would first be fetched, then the first 3 days of November would be fetched and removed from the full month stats.
## Todo
* Proper/complete readme.
* Documentation.
* Anything else that becomes apparent after real-world use.
## Note on Patches/Pull Requests

View File

@@ -1,29 +1,11 @@
require 'rubygems'
require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = 'redistat'
gem.summary = 'A Redis-backed statistics storage and querying library written in Ruby.'
gem.description = 'A Redis-backed statistics storage and querying library written in Ruby.'
gem.email = 'contact@jimeh.me'
gem.homepage = 'http://github.com/jimeh/redistat'
gem.authors = ['Jim Myhrberg']
gem.add_dependency 'activesupport', '>= 2.3.0'
gem.add_dependency 'json', '>= 1.0.0'
gem.add_dependency 'redis', '>= 2.0.0'
gem.add_dependency 'time_ext', '>= 0.2.6'
gem.add_development_dependency 'rspec', '>= 2.0.1'
gem.add_development_dependency 'yard', '>= 0.6.1'
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end
require 'bundler'
Bundler::GemHelper.install_tasks
#
# Rspec
#
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
@@ -34,12 +16,13 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.rcov = true
end
task :spec => :check_dependencies
task :default => [:start, :spec, :stop]
#
# Start/stop Redis test server
#
REDIS_DIR = File.expand_path(File.join("..", "spec"), __FILE__)
REDIS_CNF = File.join(REDIS_DIR, "redis-test.conf")
REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
@@ -60,7 +43,10 @@ task :stop do
end
# YARD Documentation
#
# Yard
#
begin
require 'yard'
YARD::Rake::YardocTask.new
@@ -69,3 +55,14 @@ rescue LoadError
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
end
end
#
# Misc.
#
desc "Start an irb console with Redistat pre-loaded."
task :console do
exec "irb -r spec/spec_helper"
end
task :c => :console

View File

@@ -1 +0,0 @@
0.0.2

View File

@@ -82,23 +82,3 @@ module Redistat
module_function :connect, :connection, :flush, :redis, :redis=, :options, :threaded
end

View File

@@ -18,7 +18,7 @@ module Redistat
:till => till
}.merge(options.merge(opts)))
end
alias :find :fetch
alias :lookup :fetch
def hashed_label(boolean = nil)
if !boolean.nil?

3
lib/redistat/version.rb Normal file
View File

@@ -0,0 +1,3 @@
module Redistat
VERSION = "0.0.4"
end

27
redistat.gemspec Normal file
View File

@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "redistat/version"
Gem::Specification.new do |s|
s.name = "redistat"
s.version = Redistat::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Jim Myhrberg"]
s.email = ["contact@jimeh.me"]
s.homepage = "http://github.com/jimeh/redistat"
s.summary = %q{A Redis-backed statistics storage and querying library written in Ruby.}
s.description = %q{A Redis-backed statistics storage and querying library written in Ruby.}
s.rubyforge_project = "redistat"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.add_runtime_dependency 'activesupport', '>= 2.3.0'
s.add_runtime_dependency 'json', '>= 1.4.6'
s.add_runtime_dependency 'redis', '>= 2.1.1'
s.add_runtime_dependency 'system_timer', '>= 1.0.0'
s.add_runtime_dependency 'time_ext', '>= 0.2.8'
end

View File

@@ -101,12 +101,3 @@ describe Redistat::Finder do
end
end

View File

@@ -47,26 +47,3 @@ describe Redistat::Summary do
end
end