From 9c9b784a8a56ab868248fd810b53a5e4df5c1d74 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 23 Jul 2010 21:46:14 +0300 Subject: [PATCH] lots of changes and cleanup, among others created a Redistat::Scope object --- lib/redistat.rb | 9 +++++---- lib/redistat/event.rb | 32 ++++++++++++++++---------------- lib/redistat/label.rb | 2 +- lib/redistat/scope.rb | 18 ++++++++++++++++++ spec/_redistat_spec.rb | 4 ++++ spec/event_spec.rb | 19 +++++++++++++------ spec/label_spec.rb | 33 +++++++++++++++++++-------------- spec/scope_spec.rb | 30 ++++++++++++++++++++++++++++++ 8 files changed, 106 insertions(+), 41 deletions(-) create mode 100644 lib/redistat/scope.rb create mode 100644 spec/scope_spec.rb diff --git a/lib/redistat.rb b/lib/redistat.rb index 981dec9..01120f5 100644 --- a/lib/redistat.rb +++ b/lib/redistat.rb @@ -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 diff --git a/lib/redistat/event.rb b/lib/redistat/event.rb index 1d1615f..fee3cce 100644 --- a/lib/redistat/event.rb +++ b/lib/redistat/event.rb @@ -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 diff --git a/lib/redistat/label.rb b/lib/redistat/label.rb index 4ce940f..1a9c53d 100644 --- a/lib/redistat/label.rb +++ b/lib/redistat/label.rb @@ -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 diff --git a/lib/redistat/scope.rb b/lib/redistat/scope.rb new file mode 100644 index 0000000..d64554b --- /dev/null +++ b/lib/redistat/scope.rb @@ -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 \ No newline at end of file diff --git a/spec/_redistat_spec.rb b/spec/_redistat_spec.rb index 26240ab..b28057e 100644 --- a/spec/_redistat_spec.rb +++ b/spec/_redistat_spec.rb @@ -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 diff --git a/spec/event_spec.rb b/spec/event_spec.rb index 8fea8b4..e7368bd 100644 --- a/spec/event_spec.rb +++ b/spec/event_spec.rb @@ -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 diff --git a/spec/label_spec.rb b/spec/label_spec.rb index 54fdf63..158d353 100644 --- a/spec/label_spec.rb +++ b/spec/label_spec.rb @@ -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 diff --git a/spec/scope_spec.rb b/spec/scope_spec.rb new file mode 100644 index 0000000..8f3fcb6 --- /dev/null +++ b/spec/scope_spec.rb @@ -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 \ No newline at end of file