lots of changes and cleanup, among others created

a Redistat::Scope object
This commit is contained in:
2010-07-23 21:46:14 +03:00
parent 22515337f6
commit 9c9b784a8a
8 changed files with 106 additions and 41 deletions

View File

@@ -11,13 +11,14 @@ require "redistat/event"
require "redistat/key"
require "redistat/label"
require "redistat/date"
require "redistat/scope"
module Redistat
KEY_NEXT_EVENT_ID = "Redistat.next_event_id"
KEY_EVENT_PREFIX = "Redistat.event:"
KEY_LEBELS_PREFIX = "Redistat.lables:"
KEY_EVENT_IDS_SUFFIX = ".event_ids"
KEY_NEXT_ID = ".next_id"
KEY_EVENT = ".event:"
KEY_LEBELS = "Redistat.lables:"
KEY_EVENT_IDS = ".event_ids"
# Provides access to the Redis database. This is shared accross all models and instances.
def redis

View File

@@ -28,19 +28,19 @@ module Redistat
def save
return false if !self.new?
@id = self.class.next_id
#TODO store sumarized stats
if @options[:store_event]
db.hmset("#{KEY_EVENT_PREFIX}#{@id}",
:scope, self.scope,
:label, self.label,
:date, self.date.to_time.to_s,
:stats, self.stats.to_json,
:meta, self.meta.to_json,
:options, self.options.to_json)
db.sadd "#{self.scope}#{KEY_EVENT_IDS_SUFFIX}", @id
@id = self.next_id
db.hmset("#{self.scope}#{KEY_EVENT}#{@id}",
"scope", self.scope,
"label", self.label,
"date", self.date.to_time.to_s,
"stats", self.stats.to_json,
"meta", self.meta.to_json,
"options", self.options.to_json)
db.sadd "#{self.scope}#{KEY_EVENT_IDS}", @id
end
@new = false
self
@@ -50,15 +50,15 @@ module Redistat
self.new(*args).save
end
def self.next_id
db.incr(KEY_NEXT_EVENT_ID)
end
def self.find(id)
event = db.hgetall "#{KEY_EVENT_PREFIX}#{id}"
def self.find(scope, id)
event = db.hgetall "#{scope}#{KEY_EVENT}#{id}"
return nil if event.size == 0
self.new( event["scope"], event["label"], event["date"], JSON.parse(event["stats"]),
JSON.parse(event["meta"]), JSON.parse(event["options"]), false )
JSON.parse(event["meta"]), JSON.parse(event["options"]), false )
end
def next_id
db.incr("#{self.scope}#{KEY_NEXT_ID}")
end
def date

View File

@@ -11,7 +11,7 @@ module Redistat
end
def save
@saved = (db.set("#{KEY_LEBELS_PREFIX}#{@hash}", @name) == "OK")
@saved = (db.set("#{KEY_LEBELS}#{@hash}", @name) == "OK")
self
end

18
lib/redistat/scope.rb Normal file
View File

@@ -0,0 +1,18 @@
module Redistat
class Scope
include Database
def initialize(name)
@name = name.to_s
end
def to_s
@name
end
def next_id
db.incr("#{@name}#{KEY_NEXT_ID}")
end
end
end

View File

@@ -2,6 +2,10 @@ require "spec_helper"
describe Redistat do
before(:all) do
db.flushdb
end
it "should have a valid redis client instance" do
db.should_not be_nil
end

View File

@@ -2,6 +2,10 @@ require "spec_helper"
describe Redistat::Event do
before(:all) do
db.flushdb
end
before(:each) do
@scope = "PageViews"
@label = "about_us"
@@ -14,6 +18,7 @@ describe Redistat::Event do
end
it "should initialize properly" do
@event.id.should be_nil
@event.scope.should == @scope
@event.label.should == @label
@event.label_hash.should == @label_hash
@@ -40,9 +45,11 @@ describe Redistat::Event do
end
it "should increment next_id" do
Redistat::Event.next_id.should == 1
Redistat::Event.next_id.should == 2
Redistat::Event.next_id.should == 3
event = Redistat::Event.new("VisitorCount", @label, @date, @stats, @meta, @options)
@event.next_id.should == 1
event.next_id.should == 1
@event.next_id.should == 2
event.next_id.should == 2
end
it "should store event properly" do
@@ -51,13 +58,13 @@ describe Redistat::Event do
@event.save
@event.new?.should be_false
keys = db.keys "*"
keys.should include("#{Redistat::KEY_EVENT_PREFIX}#{@event.id}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT_IDS_SUFFIX}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT}#{@event.id}")
keys.should include("#{@event.scope}#{Redistat::KEY_EVENT_IDS}")
end
it "should find event by id" do
@event = Redistat::Event.new(@scope, @label, @date, @stats, @meta, @options.merge({:store_event => true})).save
fetched = Redistat::Event.find(@event.id)
fetched = Redistat::Event.find(@scope, @event.id)
@event.scope.should == fetched.scope
@event.label.should == fetched.label
@event.date.to_s.should == fetched.date.to_s

View File

@@ -2,24 +2,29 @@ require "spec_helper"
describe Redistat::Label do
it "should initialize and SHA1 hash the label name" do
name = "/about/us"
label = Redistat::Label.new(name)
label.name.should == name
label.hash.should == Digest::SHA1.hexdigest(name)
before(:all) do
db.flushdb
end
before(:each) do
@name = "about_us"
@label = Redistat::Label.new(@name)
end
it "should initialize properly and SHA1 hash the label name" do
@label.name.should == @name
@label.hash.should == Digest::SHA1.hexdigest(@name)
end
it "should store a label hash lookup key" do
name = "/about/us"
label = Redistat::Label.new(name)
label.save
label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS_PREFIX}#{label.hash}").should == name
@label.save
@label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
name = "/contact/us"
label = Redistat::Label.create(name)
label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS_PREFIX}#{label.hash}").should == name
@name = "contact_us"
@label = Redistat::Label.create(@name)
@label.saved?.should be_true
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
end
def db

30
spec/scope_spec.rb Normal file
View File

@@ -0,0 +1,30 @@
require "spec_helper"
describe Redistat::Scope do
before(:all) do
db.flushdb
end
before(:each) do
@name = "PageViews"
@scope = Redistat::Scope.new(@name)
end
it "should initialize properly" do
@scope.to_s.should == @name
end
it "should increment next_id" do
scope = Redistat::Scope.new("Visitors")
@scope.next_id.should == 1
scope.next_id.should == 1
@scope.next_id.should == 2
scope.next_id.should == 2
end
def db
Redistat.redis
end
end