mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dc3816f691 | |||
| 57d8fddd23 | |||
| c5e9c02c84 | |||
| e0eac61a59 | |||
| 33e9477552 | |||
| 7e8e1dacc7 | |||
| 06cd30a20c | |||
| f481554fc9 | |||
| a9b6f2c99a | |||
| 8710f4a51f | |||
| 96e9b0a736 | |||
| 102fb41a6b | |||
| b0a44a6abc | |||
| f8dfb034af | |||
| 15904e8a94 | |||
| fe221c3f31 | |||
| 7b1feda061 | |||
| 968ef47ac5 | |||
| e3c4a5da9a | |||
| b215c6d56c | |||
| 8d5c73a539 | |||
| 0d5170bc26 | |||
| 4692780d1e | |||
| f8ec626762 | |||
| ec54385192 | |||
| 4808a97d19 | |||
| 1dce2780e0 | |||
|
|
cab7ed5633 | ||
| 861d040187 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -19,6 +19,7 @@ rdoc
|
||||
pkg/*
|
||||
*.gem
|
||||
.bundle
|
||||
Gemfile.lock
|
||||
|
||||
## PROJECT::SPECIFIC
|
||||
.yardoc/*
|
||||
|
||||
41
Gemfile.lock
41
Gemfile.lock
@@ -1,41 +0,0 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
redistat (0.0.8)
|
||||
activesupport (>= 2.3.0)
|
||||
json (>= 1.4.0)
|
||||
redis (>= 2.1.0)
|
||||
time_ext (>= 0.2.8)
|
||||
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
activesupport (3.0.3)
|
||||
diff-lcs (1.1.2)
|
||||
i18n (0.4.2)
|
||||
json (1.4.6)
|
||||
redis (2.1.1)
|
||||
rspec (2.1.0)
|
||||
rspec-core (~> 2.1.0)
|
||||
rspec-expectations (~> 2.1.0)
|
||||
rspec-mocks (~> 2.1.0)
|
||||
rspec-core (2.1.0)
|
||||
rspec-expectations (2.1.0)
|
||||
diff-lcs (~> 1.1.2)
|
||||
rspec-mocks (2.1.0)
|
||||
time_ext (0.2.8)
|
||||
activesupport (>= 2.3.0)
|
||||
i18n (>= 0.4.2)
|
||||
yard (0.6.3)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
activesupport (>= 2.3.0)
|
||||
json (>= 1.4.0)
|
||||
redis (>= 2.1.0)
|
||||
redistat!
|
||||
rspec (>= 2.1.0)
|
||||
time_ext (>= 0.2.8)
|
||||
yard (>= 0.6.3)
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2010 Jim Myhrberg.
|
||||
Copyright (c) 2011 Jim Myhrberg.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
@@ -14,7 +14,7 @@ Redis fits perfectly with all of these requirements. It has atomic operations li
|
||||
|
||||
gem install redistat
|
||||
|
||||
If you are using Ruby 1.8.x, it's recommended you also install the `system_timer` gem, as the Redis gem will otherwise complain.
|
||||
If you are using Ruby 1.8.x, it's recommended you also install the `SystemTimer` gem, as the Redis gem will otherwise complain.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -79,7 +79,7 @@ When retrieving statistics for a given date range, Redistat figures out how to d
|
||||
|
||||
## License and Copyright
|
||||
|
||||
Copyright (c) 2010 Jim Myhrberg.
|
||||
Copyright (c) 2011 Jim Myhrberg.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
1
Rakefile
1
Rakefile
@@ -14,6 +14,7 @@ end
|
||||
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
||||
spec.pattern = 'spec/**/*_spec.rb'
|
||||
spec.rcov = true
|
||||
spec.rcov_opts = ['--exclude', 'spec']
|
||||
end
|
||||
|
||||
task :default => [:start, :spec, :stop]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
require 'active_support/hash_with_indifferent_access' if !Hash.respond_to?(:with_indifferent_access) # Active Support 2.x and 3.x
|
||||
require 'active_support/hash_with_indifferent_access' if !{}.respond_to?(:with_indifferent_access) # Active Support 2.x and 3.x
|
||||
require 'redis'
|
||||
require 'date'
|
||||
require 'time'
|
||||
@@ -36,6 +36,7 @@ module Redistat
|
||||
KEY_EVENT = ".event:"
|
||||
KEY_LEBELS = "Redistat.lables:"
|
||||
KEY_EVENT_IDS = ".event_ids"
|
||||
GROUP_SEPARATOR = "/"
|
||||
|
||||
class InvalidOptions < ArgumentError; end
|
||||
class RedisServerIsTooOld < Exception; end
|
||||
|
||||
@@ -31,7 +31,10 @@ module Redistat
|
||||
end
|
||||
|
||||
def default_options
|
||||
{ :depth => :hour, :store_event => false, :connection_ref => nil }
|
||||
{ :depth => :hour,
|
||||
:store_event => false,
|
||||
:connection_ref => nil,
|
||||
:enable_grouping => true }
|
||||
end
|
||||
|
||||
def new?
|
||||
@@ -72,7 +75,7 @@ module Redistat
|
||||
|
||||
def save
|
||||
return false if !self.new?
|
||||
Summary.update_all(@key, @stats, depth_limit, @connection_ref)
|
||||
Summary.update_all(@key, @stats, depth_limit, @connection_ref, @options[:enable_grouping])
|
||||
if @options[:store_event]
|
||||
@id = self.next_id
|
||||
db.hmset("#{self.scope}#{KEY_EVENT}#{@id}",
|
||||
|
||||
@@ -2,97 +2,7 @@ module Redistat
|
||||
class Finder
|
||||
include Database
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize(options = {})
|
||||
@options = options
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
end
|
||||
|
||||
def valid_options?
|
||||
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
|
||||
false
|
||||
end
|
||||
|
||||
def find(options = {})
|
||||
@options.merge!(options)
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
if @options[:interval].nil? || !@options[:interval]
|
||||
find_by_magic
|
||||
else
|
||||
find_by_interval
|
||||
end
|
||||
end
|
||||
|
||||
def find_by_interval(options = {})
|
||||
@options.merge!(options)
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = build_key
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
build_date_sets.each do |set|
|
||||
set[:add].each do |date|
|
||||
result = Result.new
|
||||
result.date = Date.new(date).to_time
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
result[k] = v
|
||||
col.total.set_or_incr(k, v.to_i)
|
||||
end
|
||||
col << result
|
||||
end
|
||||
end
|
||||
col
|
||||
end
|
||||
|
||||
def find_by_magic(options = {})
|
||||
@options.merge!(options)
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = Key.new(@options[:scope], @options[:label])
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
col << col.total
|
||||
build_date_sets.each do |set|
|
||||
sum = Result.new
|
||||
sum = summarize_add_keys(set[:add], key, sum)
|
||||
sum = summarize_rem_keys(set[:rem], key, sum)
|
||||
sum.each do |k, v|
|
||||
col.total.set_or_incr(k, v.to_i)
|
||||
end
|
||||
end
|
||||
col
|
||||
end
|
||||
|
||||
def build_date_sets
|
||||
Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
|
||||
end
|
||||
|
||||
def build_key
|
||||
Key.new(@options[:scope], @options[:label])
|
||||
end
|
||||
|
||||
def summarize_add_keys(sets, key, sum)
|
||||
sets.each do |date|
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
sum.set_or_incr(k, v.to_i)
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def summarize_rem_keys(sets, key, sum)
|
||||
sets.each do |date|
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
sum.set_or_incr(k, -v.to_i)
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
def find(*args)
|
||||
new.find(*args)
|
||||
end
|
||||
@@ -126,15 +36,49 @@ module Redistat
|
||||
def interval(unit)
|
||||
new.interval(unit)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize(options = {})
|
||||
@options = options
|
||||
end
|
||||
|
||||
def all(reload = false)
|
||||
@result = nil if reload
|
||||
@result ||= find
|
||||
end
|
||||
|
||||
def total
|
||||
all.total
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
all.each(&block)
|
||||
end
|
||||
|
||||
def map(&block)
|
||||
all.map(&block)
|
||||
end
|
||||
|
||||
def each_with_index(&block)
|
||||
all.each_with_index(&block)
|
||||
end
|
||||
|
||||
def connection_ref(ref)
|
||||
reset! if @options[:connection_ref] != ref
|
||||
@options[:connection_ref] = ref
|
||||
self
|
||||
end
|
||||
|
||||
def scope(scope)
|
||||
reset! if @options[:scope] != scope
|
||||
@options[:scope] = scope
|
||||
self
|
||||
end
|
||||
|
||||
def label(label)
|
||||
reset! if @options[:label] != label
|
||||
@options[:label] = label
|
||||
self
|
||||
end
|
||||
@@ -145,25 +89,124 @@ module Redistat
|
||||
alias :date :dates
|
||||
|
||||
def from(date)
|
||||
reset! if @options[:from] != date
|
||||
@options[:from] = date
|
||||
self
|
||||
end
|
||||
|
||||
def till(date)
|
||||
reset! if @options[:till] != date
|
||||
@options[:till] = date
|
||||
self
|
||||
end
|
||||
alias :until :till
|
||||
|
||||
def depth(unit)
|
||||
reset! if @options[:depth] != unit
|
||||
@options[:depth] = unit
|
||||
self
|
||||
end
|
||||
|
||||
def interval(unit)
|
||||
reset! if @options[:interval] != unit
|
||||
@options[:interval] = unit
|
||||
self
|
||||
end
|
||||
|
||||
def find(options = {})
|
||||
set_options(options)
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
if @options[:interval].nil? || !@options[:interval]
|
||||
find_by_magic
|
||||
else
|
||||
find_by_interval
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_options(opts = {})
|
||||
opts = opts.clone
|
||||
opts.each do |key, value|
|
||||
self.send(key, opts.delete(key)) if self.respond_to?(key)
|
||||
end
|
||||
@options.merge!(opts)
|
||||
end
|
||||
|
||||
def find_by_interval(options = {})
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = build_key
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
build_date_sets.each do |set|
|
||||
set[:add].each do |date|
|
||||
result = Result.new
|
||||
result.date = Date.new(date).to_time
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
result[k] = v
|
||||
col.total.set_or_incr(k, v.to_i)
|
||||
end
|
||||
col << result
|
||||
end
|
||||
end
|
||||
col
|
||||
end
|
||||
|
||||
def find_by_magic(options = {})
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = Key.new(@options[:scope], @options[:label])
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
col << col.total
|
||||
build_date_sets.each do |set|
|
||||
sum = Result.new
|
||||
sum = summarize_add_keys(set[:add], key, sum)
|
||||
sum = summarize_rem_keys(set[:rem], key, sum)
|
||||
sum.each do |k, v|
|
||||
col.total.set_or_incr(k, v.to_i)
|
||||
end
|
||||
end
|
||||
col
|
||||
end
|
||||
|
||||
def reset!
|
||||
@result = nil
|
||||
end
|
||||
|
||||
def valid_options?
|
||||
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
|
||||
false
|
||||
end
|
||||
|
||||
def build_date_sets
|
||||
Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
|
||||
end
|
||||
|
||||
def build_key
|
||||
Key.new(@options[:scope], @options[:label])
|
||||
end
|
||||
|
||||
def summarize_add_keys(sets, key, sum)
|
||||
sets.each do |date|
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
sum.set_or_incr(k, v.to_i)
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def summarize_rem_keys(sets, key, sum)
|
||||
sets.each do |date|
|
||||
db.hgetall("#{key.prefix}#{date}").each do |k, v|
|
||||
sum.set_or_incr(k, -v.to_i)
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -39,10 +39,20 @@ module Redistat
|
||||
@label.hash
|
||||
end
|
||||
|
||||
def label_groups
|
||||
@label.groups
|
||||
end
|
||||
|
||||
def label=(input)
|
||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
||||
end
|
||||
|
||||
def groups
|
||||
@groups ||= label_groups.map do |label_name|
|
||||
self.class.new(@scope, label_name, self.date, @options)
|
||||
end
|
||||
end
|
||||
|
||||
def to_s(depth = nil)
|
||||
depth ||= @options[:depth]
|
||||
key = self.prefix
|
||||
|
||||
@@ -5,6 +5,10 @@ module Redistat
|
||||
attr_reader :raw
|
||||
attr_reader :connection_ref
|
||||
|
||||
def self.create(name, options = {})
|
||||
self.new(name, options).save
|
||||
end
|
||||
|
||||
def initialize(str, options = {})
|
||||
@options = options
|
||||
@raw = str.to_s
|
||||
@@ -31,8 +35,18 @@ module Redistat
|
||||
@saved ||= false
|
||||
end
|
||||
|
||||
def self.create(name, options = {})
|
||||
self.new(name, options).save
|
||||
def groups
|
||||
return @groups if @groups
|
||||
@groups = []
|
||||
parent = ""
|
||||
@raw.split(GROUP_SEPARATOR).each do |part|
|
||||
if !part.blank?
|
||||
group = ((parent.blank?) ? "" : "#{parent}/") + part
|
||||
@groups << group
|
||||
parent = group
|
||||
end
|
||||
end
|
||||
@groups.reverse!
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -6,31 +6,36 @@ module Redistat
|
||||
base.extend(self)
|
||||
end
|
||||
|
||||
#
|
||||
# statistics store/fetch methods
|
||||
#
|
||||
|
||||
def store(label, stats = {}, date = nil, meta = {}, opts = {})
|
||||
Event.new(name, label, date, stats, options.merge(opts), meta).save
|
||||
end
|
||||
alias :event :store
|
||||
|
||||
def fetch(label, from, till, opts = {})
|
||||
find(label, from, till, opts).all
|
||||
end
|
||||
alias :lookup :fetch
|
||||
|
||||
def find(label, from, till, opts = {})
|
||||
Finder.new( { :scope => name,
|
||||
:label => label,
|
||||
:from => from,
|
||||
:till => till }.merge(options.merge(opts)) )
|
||||
end
|
||||
|
||||
#
|
||||
# options methods
|
||||
#
|
||||
|
||||
def connect_to(opts = {})
|
||||
Connection.create(opts.merge(:ref => name))
|
||||
options[:connection_ref] = name
|
||||
end
|
||||
|
||||
def connection
|
||||
db(options[:connection_ref])
|
||||
end
|
||||
alias :redis :connection
|
||||
|
||||
def fetch(label, from, till, opts = {})
|
||||
Finder.find({
|
||||
:scope => name,
|
||||
:label => label,
|
||||
:from => from,
|
||||
:till => till
|
||||
}.merge(options.merge(opts)))
|
||||
end
|
||||
alias :lookup :fetch
|
||||
|
||||
def hashed_label(boolean = nil)
|
||||
if !boolean.nil?
|
||||
options[:hashed_label] = boolean
|
||||
@@ -64,15 +69,22 @@ module Redistat
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# resource access methods
|
||||
#
|
||||
|
||||
def connection
|
||||
db(options[:connection_ref])
|
||||
end
|
||||
alias :redis :connection
|
||||
|
||||
def options
|
||||
@options ||= {}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def name
|
||||
options[:class_name] || (@name ||= self.to_s)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
module Redistat
|
||||
class Result < ::ActiveSupport::HashWithIndifferentAccess
|
||||
class Result < HashWithIndifferentAccess
|
||||
|
||||
attr_accessor :from
|
||||
attr_accessor :till
|
||||
|
||||
@@ -2,23 +2,57 @@ module Redistat
|
||||
class Summary
|
||||
include Database
|
||||
|
||||
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil)
|
||||
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil, enable_grouping = nil)
|
||||
stats ||= {}
|
||||
depth_limit ||= key.depth
|
||||
return nil if stats.size == 0
|
||||
|
||||
depth_limit ||= key.depth
|
||||
enable_grouping = true if enable_grouping.nil?
|
||||
|
||||
if enable_grouping
|
||||
stats = inject_group_summaries(stats)
|
||||
key.groups.each { |k|
|
||||
update_key(k, stats, depth_limit, connection_ref)
|
||||
}
|
||||
else
|
||||
update_key(key, stats, depth_limit, connection_ref)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.update_key(key, stats, depth_limit, connection_ref)
|
||||
Date::DEPTHS.each do |depth|
|
||||
update(key, stats, depth, connection_ref)
|
||||
break if depth == depth_limit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.update(key, stats, depth, connection_ref = nil)
|
||||
stats.each do |field, value|
|
||||
db(connection_ref).hincrby key.to_s(depth), field, value
|
||||
end
|
||||
end
|
||||
|
||||
def self.inject_group_summaries!(stats)
|
||||
stats.each do |key, value|
|
||||
parts = key.to_s.split(GROUP_SEPARATOR)
|
||||
parts.pop
|
||||
if parts.size > 0
|
||||
sum_parts = []
|
||||
parts.each do |part|
|
||||
sum_parts << part
|
||||
sum_key = sum_parts.join(GROUP_SEPARATOR)
|
||||
(stats.has_key?(sum_key)) ? stats[sum_key] += value : stats[sum_key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
stats
|
||||
end
|
||||
|
||||
def self.inject_group_summaries(stats)
|
||||
inject_group_summaries!(stats.clone)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,3 +1,3 @@
|
||||
module Redistat
|
||||
VERSION = "0.0.8"
|
||||
VERSION = "0.1.1"
|
||||
end
|
||||
|
||||
@@ -19,11 +19,12 @@ Gem::Specification.new do |s|
|
||||
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 'activesupport', '>= 2.3.6'
|
||||
s.add_runtime_dependency 'json', '>= 1.4.0'
|
||||
s.add_runtime_dependency 'redis', '>= 2.1.0'
|
||||
s.add_runtime_dependency 'time_ext', '>= 0.2.8'
|
||||
|
||||
s.add_development_dependency 'rspec', '>= 2.1.0'
|
||||
s.add_development_dependency 'rcov', '>= 0.9.9'
|
||||
s.add_development_dependency 'yard', '>= 0.6.3'
|
||||
end
|
||||
|
||||
@@ -19,6 +19,10 @@ describe Redistat::Finder do
|
||||
|
||||
finder = Redistat::Finder.new(options)
|
||||
finder.options.should == options
|
||||
|
||||
finder = Redistat::Finder.new
|
||||
finder.send(:set_options, options)
|
||||
finder.options.should == options
|
||||
|
||||
finder = Redistat::Finder.dates(two_hours_ago, one_hour_ago).scope("PageViews").label("Label").depth(:hour).interval(:hour)
|
||||
finder.options.should == options
|
||||
@@ -85,6 +89,65 @@ describe Redistat::Finder do
|
||||
lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions)
|
||||
end
|
||||
|
||||
describe "Lazy-Loading" do
|
||||
|
||||
before(:each) do
|
||||
@first_stat, @last_stat = create_example_stats
|
||||
|
||||
@finder = Redistat::Finder.new
|
||||
@finder.from(@first_stat).till(@last_stat).scope(@scope).label(@label).depth(:hour)
|
||||
|
||||
@match = [{}, {"visitors"=>"4", "views"=>"6"},
|
||||
{"visitors"=>"2", "views"=>"3"},
|
||||
{"visitors"=>"2", "views"=>"3"}, {}]
|
||||
end
|
||||
|
||||
it "should lazy-load" do
|
||||
|
||||
@finder.instance_variable_get("@result").should be_nil
|
||||
stats = @finder.all
|
||||
@finder.instance_variable_get("@result").should_not be_nil
|
||||
|
||||
stats.should == @finder.find # find method directly fetches results
|
||||
stats.total.should == @finder.total
|
||||
stats.total.should == { "views" => 12, "visitors" => 8 }
|
||||
stats.total.from.should == @first_stat
|
||||
stats.total.till.should == @last_stat
|
||||
stats.first.should == stats.total
|
||||
|
||||
@finder.all.object_id.should == stats.object_id
|
||||
@finder.from(@first_stat + 2.hours)
|
||||
@finder.instance_variable_get("@result").should be_nil
|
||||
@finder.all.object_id.should_not == stats.object_id
|
||||
stats = @finder.all
|
||||
stats.total.should == { "views" => 6, "visitors" => 4 }
|
||||
end
|
||||
|
||||
it "should handle #map" do
|
||||
@finder.interval(:hour)
|
||||
@finder.map { |r| r }.should == @match
|
||||
end
|
||||
|
||||
it "should handle #each" do
|
||||
@finder.interval(:hour)
|
||||
|
||||
res = []
|
||||
@finder.each { |r| res << r }
|
||||
res.should == @match
|
||||
end
|
||||
|
||||
it "should handle #each_with_index" do
|
||||
@finder.interval(:hour)
|
||||
|
||||
res = {}
|
||||
match = {}
|
||||
@finder.each_with_index { |r, i| res[i] = r }
|
||||
@match.each_with_index { |r, i| match[i] = r }
|
||||
res.should == match
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
# helper methods
|
||||
|
||||
@@ -93,7 +156,7 @@ describe Redistat::Finder do
|
||||
Redistat::Summary.update(key, @stats, :hour)
|
||||
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 13:53"))
|
||||
Redistat::Summary.update(key, @stats, :hour)
|
||||
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 14:32"))
|
||||
key = Redistat::Key.new(@scope, @label, Time.parse("2010-05-14 14:52"))
|
||||
Redistat::Summary.update(key, @stats, :hour)
|
||||
key = Redistat::Key.new(@scope, @label, (last = Time.parse("2010-05-14 15:02")))
|
||||
Redistat::Summary.update(key, @stats, :hour)
|
||||
|
||||
@@ -14,6 +14,7 @@ describe Redistat::Key do
|
||||
@key.scope.should == @scope
|
||||
@key.label.should == @label
|
||||
@key.label_hash.should == @label_hash
|
||||
@key.label_groups.should == @key.instance_variable_get("@label").groups
|
||||
@key.date.should be_instance_of(Redistat::Date)
|
||||
@key.date.to_time.to_s.should == @date.to_s
|
||||
end
|
||||
@@ -60,4 +61,16 @@ describe Redistat::Key do
|
||||
@key.label_hash == @label_hash
|
||||
end
|
||||
|
||||
it "should create a group of keys from label group" do
|
||||
label = 'message/public/offensive'
|
||||
result = [ "message/public/offensive",
|
||||
"message/public",
|
||||
"message" ]
|
||||
|
||||
key = Redistat::Key.new(@scope, label, @date, {:depth => :hour})
|
||||
|
||||
key.label_groups.should == result
|
||||
key.groups.map { |k| k.label }.should == result
|
||||
end
|
||||
|
||||
end
|
||||
@@ -25,4 +25,24 @@ describe Redistat::Label do
|
||||
db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name
|
||||
end
|
||||
|
||||
it "should separate label names into groups" do
|
||||
name = "message/public/offensive"
|
||||
label = Redistat::Label.new(name)
|
||||
label.name.should == name
|
||||
label.groups.should == [ "message/public/offensive",
|
||||
"message/public",
|
||||
"message" ]
|
||||
|
||||
name = "/message/public/"
|
||||
label = Redistat::Label.new(name)
|
||||
label.name.should == name
|
||||
label.groups.should == [ "message/public",
|
||||
"message" ]
|
||||
|
||||
name = "message"
|
||||
label = Redistat::Label.new(name)
|
||||
label.name.should == name
|
||||
label.groups.should == [ "message" ]
|
||||
end
|
||||
|
||||
end
|
||||
@@ -5,6 +5,7 @@ describe Redistat::Model do
|
||||
include Redistat::Database
|
||||
|
||||
before(:each) do
|
||||
@time = Time.utc(2010, 8, 28, 12, 0, 0)
|
||||
ModelHelper1.redis.flushdb
|
||||
ModelHelper2.redis.flushdb
|
||||
ModelHelper3.redis.flushdb
|
||||
@@ -16,6 +17,17 @@ describe Redistat::Model do
|
||||
ModelHelper2.send(:name).should == "ModelHelper2"
|
||||
end
|
||||
|
||||
it "should return a Finder" do
|
||||
two_hours_ago = 2.hours.ago
|
||||
one_hour_ago = 1.hour.ago
|
||||
finder = ModelHelper1.find('label', two_hours_ago, one_hour_ago)
|
||||
finder.should be_a(Redistat::Finder)
|
||||
finder.options[:scope].should == 'ModelHelper1'
|
||||
finder.options[:label].should == 'label'
|
||||
finder.options[:from].should == two_hours_ago
|
||||
finder.options[:till].should == one_hour_ago
|
||||
end
|
||||
|
||||
it "should listen to model-defined options" do
|
||||
ModelHelper2.depth.should == :day
|
||||
ModelHelper2.store_event.should == true
|
||||
@@ -43,28 +55,28 @@ describe Redistat::Model do
|
||||
end
|
||||
|
||||
it "should store and fetch stats" do
|
||||
ModelHelper1.store("sheep.black", {:count => 6, :weight => 461}, 4.hours.ago)
|
||||
ModelHelper1.store("sheep.black", {:count => 2, :weight => 156})
|
||||
ModelHelper1.store("sheep.black", {:count => 6, :weight => 461}, @time.hours_ago(4))
|
||||
ModelHelper1.store("sheep.black", {:count => 2, :weight => 156}, @time)
|
||||
|
||||
stats = ModelHelper1.fetch("sheep.black", 2.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(2), @time.hours_since(1))
|
||||
stats.total["count"].should == 2
|
||||
stats.total["weight"].should == 156
|
||||
stats.first.should == stats.total
|
||||
|
||||
stats = ModelHelper1.fetch("sheep.black", 5.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
|
||||
stats.total[:count].should == 8
|
||||
stats.total[:weight].should == 617
|
||||
stats.first.should == stats.total
|
||||
|
||||
ModelHelper1.store("sheep.white", {:count => 5, :weight => 393}, 4.hours.ago)
|
||||
ModelHelper1.store("sheep.white", {:count => 4, :weight => 316})
|
||||
ModelHelper1.store("sheep.white", {:count => 5, :weight => 393}, @time.hours_ago(4))
|
||||
ModelHelper1.store("sheep.white", {:count => 4, :weight => 316}, @time)
|
||||
|
||||
stats = ModelHelper1.fetch("sheep.white", 2.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper1.fetch("sheep.white", @time.hours_ago(2), @time.hours_since(1))
|
||||
stats.total[:count].should == 4
|
||||
stats.total[:weight].should == 316
|
||||
stats.first.should == stats.total
|
||||
|
||||
stats = ModelHelper1.fetch("sheep.white", 5.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper1.fetch("sheep.white", @time.hours_ago(5), @time.hours_since(1))
|
||||
stats.total[:count].should == 9
|
||||
stats.total[:weight].should == 709
|
||||
stats.first.should == stats.total
|
||||
@@ -73,31 +85,31 @@ describe Redistat::Model do
|
||||
it "should connect to different Redis servers on a per-model basis" do
|
||||
ModelHelper3.redis.client.db.should == 14
|
||||
|
||||
ModelHelper3.store("sheep.black", {:count => 6, :weight => 461}, 4.hours.ago)
|
||||
ModelHelper3.store("sheep.black", {:count => 2, :weight => 156})
|
||||
ModelHelper3.store("sheep.black", {:count => 6, :weight => 461}, @time.hours_ago(4))
|
||||
ModelHelper3.store("sheep.black", {:count => 2, :weight => 156}, @time)
|
||||
|
||||
db.keys("*").should be_empty
|
||||
ModelHelper1.redis.keys("*").should be_empty
|
||||
db("ModelHelper3").keys("*").should have(5).items
|
||||
ModelHelper3.redis.keys("*").should have(5).items
|
||||
|
||||
stats = ModelHelper3.fetch("sheep.black", 2.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(2), @time.hours_since(1))
|
||||
stats.total["count"].should == 2
|
||||
stats.total["weight"].should == 156
|
||||
stats = ModelHelper3.fetch("sheep.black", 5.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
|
||||
stats.total[:count].should == 8
|
||||
stats.total[:weight].should == 617
|
||||
|
||||
ModelHelper3.connect_to(:port => 8379, :db => 13)
|
||||
ModelHelper3.redis.client.db.should == 13
|
||||
|
||||
stats = ModelHelper3.fetch("sheep.black", 5.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
|
||||
stats.total.should == {}
|
||||
|
||||
ModelHelper3.connect_to(:port => 8379, :db => 14)
|
||||
ModelHelper3.redis.client.db.should == 14
|
||||
|
||||
stats = ModelHelper3.fetch("sheep.black", 5.hours.ago, 1.hour.from_now)
|
||||
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
|
||||
stats.total[:count].should == 8
|
||||
stats.total[:weight].should == 617
|
||||
end
|
||||
|
||||
@@ -46,4 +46,78 @@ describe Redistat::Summary do
|
||||
end
|
||||
end
|
||||
|
||||
it "should inject stats key grouping summaries" do
|
||||
hash = { "count/hello" => 3, "count/world" => 7,
|
||||
"death/bomb" => 4, "death/unicorn" => 3,
|
||||
:"od/sugar" => 7, :"od/meth" => 8 }
|
||||
res = Redistat::Summary.send(:inject_group_summaries, hash)
|
||||
res.should == { "count" => 10, "count/hello" => 3, "count/world" => 7,
|
||||
"death" => 7, "death/bomb" => 4, "death/unicorn" => 3,
|
||||
"od" => 15, :"od/sugar" => 7, :"od/meth" => 8 }
|
||||
end
|
||||
|
||||
it "should properly store key group summaries" do
|
||||
stats = {"views" => 3, "visitors/eu" => 2, "visitors/us" => 4}
|
||||
Redistat::Summary.update_all(@key, stats, :hour)
|
||||
summary = db.hgetall(@key.to_s(:hour))
|
||||
summary.should have(4).items
|
||||
summary["views"].should == "3"
|
||||
summary["visitors"].should == "6"
|
||||
summary["visitors/eu"].should == "2"
|
||||
summary["visitors/us"].should == "4"
|
||||
end
|
||||
|
||||
it "should not store key group summaries when option is disabled" do
|
||||
stats = {"views" => 3, "visitors/eu" => 2, "visitors/us" => 4}
|
||||
Redistat::Summary.update_all(@key, stats, :hour, nil, false)
|
||||
summary = db.hgetall(@key.to_s(:hour))
|
||||
summary.should have(3).items
|
||||
summary["views"].should == "3"
|
||||
summary["visitors/eu"].should == "2"
|
||||
summary["visitors/us"].should == "4"
|
||||
end
|
||||
|
||||
it "should store label-based grouping enabled stats" do
|
||||
stats = {"views" => 3, "visitors/eu" => 2, "visitors/us" => 4}
|
||||
label = "views/about_us"
|
||||
key = Redistat::Key.new(@scope, label, @date)
|
||||
Redistat::Summary.update_all(key, stats, :hour)
|
||||
|
||||
key.groups[0].label.should == "views/about_us"
|
||||
key.groups[1].label.should == "views"
|
||||
child1 = key.groups[0]
|
||||
parent = key.groups[1]
|
||||
|
||||
label = "views/contact"
|
||||
key = Redistat::Key.new(@scope, label, @date)
|
||||
Redistat::Summary.update_all(key, stats, :hour)
|
||||
|
||||
key.groups[0].label.should == "views/contact"
|
||||
key.groups[1].label.should == "views"
|
||||
child2 = key.groups[0]
|
||||
|
||||
summary = db.hgetall(child1.to_s(:hour))
|
||||
summary["views"].should == "3"
|
||||
summary["visitors/eu"].should == "2"
|
||||
summary["visitors/us"].should == "4"
|
||||
|
||||
summary = db.hgetall(child2.to_s(:hour))
|
||||
summary["views"].should == "3"
|
||||
summary["visitors/eu"].should == "2"
|
||||
summary["visitors/us"].should == "4"
|
||||
|
||||
summary = db.hgetall(parent.to_s(:hour))
|
||||
summary["views"].should == "6"
|
||||
summary["visitors/eu"].should == "4"
|
||||
summary["visitors/us"].should == "8"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user