mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d5170bc26 | |||
| 4692780d1e | |||
| f8ec626762 | |||
| ec54385192 | |||
| 4808a97d19 | |||
| 1dce2780e0 | |||
|
|
cab7ed5633 | ||
| 861d040187 | |||
| 3267ee1eb9 | |||
| 6309e4b217 | |||
| 776ee8ac97 | |||
| 3b346e88e0 | |||
| c3fe861b10 | |||
| bc5034b6bb | |||
| 66510fe344 | |||
| 745473862f | |||
| a5c8fc6fbf | |||
| 115b223d7c | |||
| 0597b587fd | |||
| 89932759ef | |||
| 55e0687837 | |||
|
|
93360dbeb9 | ||
|
|
6a66605e0b | ||
|
|
d9ce0daade | ||
| f6ec2e97b2 | |||
| 67dc9433c7 | |||
| f0fcd2110d | |||
| 24112e4705 | |||
| b9752ff92f | |||
| 14a093d79b | |||
| 84a05363dd |
44
Gemfile.lock
44
Gemfile.lock
@@ -1,44 +0,0 @@
|
||||
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:
|
||||
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)
|
||||
system_timer (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.6)
|
||||
redis (>= 2.1.1)
|
||||
redistat!
|
||||
rspec (>= 2.1.0)
|
||||
system_timer (>= 1.0.0)
|
||||
time_ext (>= 0.2.8)
|
||||
yard (>= 0.6.3)
|
||||
@@ -14,6 +14,8 @@ 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.
|
||||
|
||||
## Usage
|
||||
|
||||
The simplest way to use Redistat is through the model wrapper.
|
||||
|
||||
@@ -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'
|
||||
@@ -13,6 +13,7 @@ require 'redistat/collection'
|
||||
require 'redistat/connection'
|
||||
require 'redistat/database'
|
||||
require 'redistat/date'
|
||||
require 'redistat/date_helper'
|
||||
require 'redistat/event'
|
||||
require 'redistat/finder'
|
||||
require 'redistat/finder/date_set'
|
||||
@@ -22,10 +23,12 @@ require 'redistat/model'
|
||||
require 'redistat/result'
|
||||
require 'redistat/scope'
|
||||
require 'redistat/summary'
|
||||
require 'redistat/version'
|
||||
|
||||
require 'redistat/core_ext/date'
|
||||
require 'redistat/core_ext/time'
|
||||
require 'redistat/core_ext/fixnum'
|
||||
require 'redistat/core_ext/bignum'
|
||||
|
||||
module Redistat
|
||||
|
||||
@@ -54,7 +57,7 @@ module Redistat
|
||||
end
|
||||
|
||||
def flush
|
||||
puts "WARNING: Redistat.flush is deprecated. Use Redistat.redis.flush instead."
|
||||
puts "WARNING: Redistat.flush is deprecated. Use Redistat.redis.flushdb instead."
|
||||
connection.flushdb
|
||||
end
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ module Redistat
|
||||
end
|
||||
|
||||
def create(options = {})
|
||||
#TODO clean/remove all ref-less connections
|
||||
ref = options.delete(:ref) || :default
|
||||
options.reverse_merge!(default_options)
|
||||
conn = (connections[connection_id(options)] ||= connection(options))
|
||||
@@ -26,15 +27,11 @@ module Redistat
|
||||
end
|
||||
|
||||
def connections
|
||||
threaded[:connections] ||= {}
|
||||
@connections ||= {}
|
||||
end
|
||||
|
||||
def references
|
||||
threaded[:references] ||= {}
|
||||
end
|
||||
|
||||
def threaded
|
||||
Thread.current[:redistat] ||= {}
|
||||
@references ||= {}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
8
lib/redistat/core_ext/bignum.rb
Executable file
8
lib/redistat/core_ext/bignum.rb
Executable file
@@ -0,0 +1,8 @@
|
||||
class Bignum
|
||||
include Redistat::DateHelper
|
||||
|
||||
def to_time
|
||||
Time.at(self)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -22,6 +22,8 @@ module Redistat
|
||||
from_string(input)
|
||||
elsif input.is_a?(::Fixnum)
|
||||
from_integer(input)
|
||||
elsif input.is_a?(::Bignum)
|
||||
from_integer(input)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -83,11 +85,4 @@ module Redistat
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module DateHelper
|
||||
def to_redistat(depth = nil)
|
||||
Redistat::Date.new(self, depth)
|
||||
end
|
||||
alias :to_rs :to_redistat
|
||||
end
|
||||
end
|
||||
|
||||
8
lib/redistat/date_helper.rb
Normal file
8
lib/redistat/date_helper.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module Redistat
|
||||
module DateHelper
|
||||
def to_redistat(depth = nil)
|
||||
Redistat::Date.new(self, depth)
|
||||
end
|
||||
alias :to_rs :to_redistat
|
||||
end
|
||||
end
|
||||
@@ -42,7 +42,7 @@ module Redistat
|
||||
return find_start_year_for(start_date, end_date, lowest_depth) if unit == :year
|
||||
index = Date::DEPTHS.index(unit)
|
||||
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
|
||||
if start_date < start_date.round(nunit) || start_date.next(nunit).beginning_of(nunit) > end_date.beginning_of(nunit)
|
||||
if start_date < start_date.beginning_of_closest(nunit) || start_date.next(nunit).beginning_of(nunit) > end_date.beginning_of(nunit)
|
||||
add = []
|
||||
start_date.beginning_of_each(unit, :include_start => lowest_depth).until(start_date.end_of(nunit)) do |t|
|
||||
add << t.to_rs.to_s(unit) if t < end_date.beginning_of(unit)
|
||||
@@ -59,7 +59,7 @@ module Redistat
|
||||
index = Date::DEPTHS.index(unit)
|
||||
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
|
||||
has_nunit = end_date.prev(nunit).beginning_of(nunit) >= start_date.beginning_of(nunit)
|
||||
nearest_nunit = end_date.round(nunit)
|
||||
nearest_nunit = end_date.beginning_of_closest(nunit)
|
||||
if end_date >= nearest_nunit && has_nunit
|
||||
add = []
|
||||
end_date.beginning_of(nunit).beginning_of_each(unit, :include_start => true, :include_end => lowest_depth).until(end_date) do |t|
|
||||
|
||||
@@ -39,6 +39,15 @@ module Redistat
|
||||
end
|
||||
end
|
||||
|
||||
def class_name(class_name = nil)
|
||||
if !class_name.nil?
|
||||
options[:class_name] = class_name
|
||||
else
|
||||
options[:class_name] || nil
|
||||
end
|
||||
end
|
||||
alias :scope :class_name
|
||||
|
||||
def depth(depth = nil)
|
||||
if !depth.nil?
|
||||
options[:depth] = depth
|
||||
@@ -62,7 +71,7 @@ module Redistat
|
||||
private
|
||||
|
||||
def name
|
||||
@name ||= self.to_s
|
||||
options[:class_name] || (@name ||= self.to_s)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
module Redistat
|
||||
class Result < ::ActiveSupport::HashWithIndifferentAccess
|
||||
class Result < HashWithIndifferentAccess
|
||||
|
||||
attr_accessor :from
|
||||
attr_accessor :till
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Redistat
|
||||
VERSION = "0.0.5"
|
||||
end
|
||||
VERSION = "0.0.9"
|
||||
end
|
||||
|
||||
@@ -19,10 +19,9 @@ 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 '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 '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'
|
||||
|
||||
@@ -20,7 +20,7 @@ describe Redistat::Event do
|
||||
@event.scope.should == @scope
|
||||
@event.label.should == @label
|
||||
@event.label_hash.should == @label_hash
|
||||
@event.date.to_time.should == @date
|
||||
@event.date.to_time.to_s.should == @date.to_s
|
||||
@event.stats.should == @stats
|
||||
@event.meta.should == @meta
|
||||
@event.options.should == @event.default_options.merge(@options)
|
||||
@@ -28,10 +28,10 @@ describe Redistat::Event do
|
||||
|
||||
it "should allow changing attributes" do
|
||||
# date
|
||||
@event.date.to_time.should == @date
|
||||
@event.date.to_time.to_s.should == @date.to_s
|
||||
@date = Time.now
|
||||
@event.date = @date
|
||||
@event.date.to_time.should == @date
|
||||
@event.date.to_time.to_s.should == @date.to_s
|
||||
# label
|
||||
@event.label.should == @label
|
||||
@event.label_hash.should == @label_hash
|
||||
|
||||
@@ -46,10 +46,10 @@ describe Redistat::Key do
|
||||
@key.scope = @scope
|
||||
@key.scope.should == @scope
|
||||
# date
|
||||
@key.date.to_time.should == @date
|
||||
@key.date.to_time.to_s.should == @date.to_s
|
||||
@date = Time.now
|
||||
@key.date = @date
|
||||
@key.date.to_time.should == @date
|
||||
@key.date.to_time.to_s.should == @date.to_s
|
||||
# label
|
||||
@key.label.should == @label
|
||||
@key.label_hash == @label_hash
|
||||
|
||||
@@ -20,4 +20,11 @@ class ModelHelper3
|
||||
|
||||
connect_to :port => 8379, :db => 14
|
||||
|
||||
end
|
||||
|
||||
class ModelHelper4
|
||||
include Redistat::Model
|
||||
|
||||
class_name "FancyHelper"
|
||||
|
||||
end
|
||||
@@ -8,6 +8,7 @@ describe Redistat::Model do
|
||||
ModelHelper1.redis.flushdb
|
||||
ModelHelper2.redis.flushdb
|
||||
ModelHelper3.redis.flushdb
|
||||
ModelHelper4.redis.flushdb
|
||||
end
|
||||
|
||||
it "should should name itself correctly" do
|
||||
@@ -19,6 +20,7 @@ describe Redistat::Model do
|
||||
ModelHelper2.depth.should == :day
|
||||
ModelHelper2.store_event.should == true
|
||||
ModelHelper2.hashed_label.should == true
|
||||
ModelHelper2.class_name.should be_nil
|
||||
|
||||
ModelHelper1.depth.should == nil
|
||||
ModelHelper1.store_event.should == nil
|
||||
@@ -35,6 +37,9 @@ describe Redistat::Model do
|
||||
ModelHelper1.depth.should == nil
|
||||
ModelHelper1.store_event.should == nil
|
||||
ModelHelper1.hashed_label.should == nil
|
||||
|
||||
ModelHelper4.class_name.should == "FancyHelper"
|
||||
ModelHelper4.send(:name).should == "FancyHelper"
|
||||
end
|
||||
|
||||
it "should store and fetch stats" do
|
||||
|
||||
@@ -8,5 +8,5 @@ require 'rspec'
|
||||
require 'rspec/autorun'
|
||||
|
||||
# use the test Redistat instance
|
||||
Redistat.connect(:port => 8379, :db => 15)
|
||||
Redistat.connect(:port => 8379, :db => 15, :thread_safe => true)
|
||||
Redistat.redis.flushdb
|
||||
|
||||
39
spec/thread_safety_spec.rb
Normal file
39
spec/thread_safety_spec.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe "Thread-Safety" do
|
||||
include Redistat::Database
|
||||
|
||||
before(:each) do
|
||||
db.flushdb
|
||||
end
|
||||
|
||||
#TODO should have more comprehensive thread-safe tests
|
||||
|
||||
it "should incr in multiple threads" do
|
||||
threads = []
|
||||
50.times do
|
||||
threads << Thread.new {
|
||||
db.incr("spec:incr")
|
||||
}
|
||||
end
|
||||
threads.each { |t| t.join }
|
||||
db.get("spec:incr").should == "50"
|
||||
end
|
||||
|
||||
it "should store event in multiple threads" do
|
||||
class ThreadSafetySpec
|
||||
include Redistat::Model
|
||||
end
|
||||
threads = []
|
||||
50.times do
|
||||
threads << Thread.new {
|
||||
ThreadSafetySpec.store("spec:threadsafe", {:count => 1, :rand => rand(5)})
|
||||
}
|
||||
end
|
||||
threads.each { |t| t.join }
|
||||
result = ThreadSafetySpec.fetch("spec:threadsafe", 5.hours.ago, 5.hours.from_now)
|
||||
result.total[:count].should == 50
|
||||
result.total[:rand].should <= 250
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user