Skip to content

Commit

Permalink
most components use new Options helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
jimeh committed Mar 10, 2011
1 parent 3a00353 commit d39d5d8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 94 deletions.
1 change: 1 addition & 0 deletions lib/redistat/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.included(base)
base.extend(Database)
end
def db(ref = nil)
ref ||= @options[:connection_ref] if !@options.nil?
Redistat.connection(ref)
end
end
Expand Down
36 changes: 12 additions & 24 deletions lib/redistat/event.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
module Redistat
class Event
include Database
include Options

attr_reader :id
attr_reader :key
attr_reader :connection_ref

attr_accessor :stats
attr_accessor :meta
attr_accessor :options

def initialize(scope, label = nil, date = nil, stats = {}, options = {}, meta = {}, is_new = true)
@options = parse_options(options)
@connection_ref = @options[:connection_ref]
@key = Key.new(scope, label, date, @options)
@stats = stats ||= {}
@meta = meta ||= {}
@new = is_new
end

def db
super(@connection_ref)
end

def parse_options(options)
default_options.each do |opt, val|
options[opt] = val if options[opt].nil?
end
options
end

def default_options
{ :depth => :hour,
:store_event => false,
:connection_ref => nil,
:enable_grouping => true }
:enable_grouping => true,
:label_indexing => true }
end

def initialize(scope, label = nil, date = nil, stats = {}, opts = {}, meta = {}, is_new = true)
parse_options(opts)
@key = Key.new(scope, label, date, @options)
@stats = stats ||= {}
@meta = meta ||= {}
@new = is_new
end

def new?
Expand Down Expand Up @@ -75,7 +63,7 @@ def next_id

def save
return false if !self.new?
Summary.update_all(@key, @stats, depth_limit, @connection_ref, @options[:enable_grouping])
Summary.update_all(@key, @stats, depth_limit, @options)
if @options[:store_event]
@id = self.next_id
db.hmset("#{self.scope}#{KEY_EVENT}#{@id}",
Expand Down
17 changes: 8 additions & 9 deletions lib/redistat/key.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
module Redistat
class Key
include Redistat::Database
include Database
include Options

attr_accessor :options
def default_options
{ :depth => :hour }
end

def initialize(scope, label_name = nil, time_stamp = nil, options = {})
@options = default_options.merge(options || {})
def initialize(scope, label_name = nil, time_stamp = nil, opts = {})
parse_options(opts)
self.scope = scope
self.label = label_name if !label_name.nil?
self.date = time_stamp ||= Time.now
end

def default_options
{ :depth => :hour, :hashed_label => false }
end

def prefix
key = "#{@scope}"
key << "/#{label.name}" if !label.nil?
Expand All @@ -28,7 +27,7 @@ def date=(input)
attr_reader :date

def depth
@options[:depth]
options[:depth]
end

def scope
Expand Down
18 changes: 9 additions & 9 deletions lib/redistat/label.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
module Redistat
class Label
include Database
include Options

attr_reader :connection_ref
def default_options
{ :hashed_label => false }
end

def self.create(name, options = {})
self.new(name, options).save
def self.create(name, opts = {})
self.new(name, opts).save
end

def initialize(str, options = {})
@options = options
def initialize(str, opts = {})
parse_options(opts)
@raw = str.to_s
end

def to_s
@raw
end

def db
super(@options[:connection_ref])
end

def name
@options[:hashed_label] ? hash : self.to_s
Expand All @@ -35,6 +34,7 @@ def save
end

def saved?
return true unless @options[:hashed_label]
@saved ||= false
end

Expand Down
51 changes: 13 additions & 38 deletions lib/redistat/model.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Redistat
module Model
include Redistat::Database
include Database
include Options

def self.included(base)
base.extend(self)
end


#
# statistics store/fetch methods
#

def store(label, stats = {}, date = nil, meta = {}, opts = {})
def store(label, stats = {}, date = nil, opts = {}, meta = {})
Event.new(name, label, date, stats, options.merge(opts), meta).save
end
alias :event :store
Expand All @@ -27,47 +29,24 @@ def find(label, from, till, opts = {})
: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 hashed_label(boolean = nil)
if !boolean.nil?
options[:hashed_label] = boolean
else
options[:hashed_label] || nil
end
end
option_accessor :depth
option_accessor :class_name
option_accessor :store_event
option_accessor :hashed_label
option_accessor :label_indexing

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
else
options[:depth] || nil
end
def connect_to(opts = {})
Connection.create(opts.merge(:ref => name))
options[:connection_ref] = name
end

def store_event(boolean = nil)
if !boolean.nil?
options[:store_event] = boolean
else
options[:store_event] || nil
end
end

#
# resource access methods
Expand All @@ -78,10 +57,6 @@ def connection
end
alias :redis :connection

def options
@options ||= {}
end

def name
options[:class_name] || (@name ||= self.to_s)
end
Expand Down
19 changes: 13 additions & 6 deletions lib/redistat/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@ module Redistat
class Summary
include Database

def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil, enable_grouping = nil)
def self.default_options
{ :enable_grouping => true,
:label_indexing => true,
:connection_ref => nil }
end

def self.update_all(key, stats = {}, depth_limit = nil, opts = {})
stats ||= {}
return nil if stats.size == 0

options = default_options.merge((opts || {}).reject { |k,v| v.nil? })

depth_limit ||= key.depth
enable_grouping = true if enable_grouping.nil?

if enable_grouping
if options[:enable_grouping]
stats = inject_group_summaries(stats)
key.groups.each { |k|
update_key(k, stats, depth_limit, connection_ref)
k.update_index # TODO: add a label_indexing option
update_key(k, stats, depth_limit, options[:connection_ref])
k.update_index if options[:label_indexing]
}
else
update_key(key, stats, depth_limit, connection_ref)
update_key(key, stats, depth_limit, options[:connection_ref])
end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,31 @@
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}, @time.hours_ago(4))
ModelHelper3.store("sheep.black", {:count => 2, :weight => 156}, @time)
ModelHelper3.store("sheep.black", {:count => 6, :weight => 461}, @time.hours_ago(4), :label_indexing => false)
ModelHelper3.store("sheep.black", {:count => 2, :weight => 156}, @time, :label_indexing => false)

db.keys("*").should be_empty # FIXME: index_labels option needs to be added, and enabled here
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", @time.hours_ago(2), @time.hours_since(1))
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(2), @time.hours_since(1), :label_indexing => false)
stats.total["count"].should == 2
stats.total["weight"].should == 156
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
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", @time.hours_ago(5), @time.hours_since(1))
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
stats.total.should == {}

ModelHelper3.connect_to(:port => 8379, :db => 14)
ModelHelper3.redis.client.db.should == 14

stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
stats.total[:count].should == 8
stats.total[:weight].should == 617
end
Expand Down
2 changes: 1 addition & 1 deletion spec/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

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)
Redistat::Summary.update_all(@key, stats, :hour, {:enable_grouping => false})
summary = db.hgetall(@key.to_s(:hour))
summary.should have(3).items
summary["views"].should == "3"
Expand Down

0 comments on commit d39d5d8

Please sign in to comment.