most components use new Options helper module

This commit is contained in:
2011-03-10 00:27:13 +00:00
parent 3a00353f83
commit d39d5d8dde
8 changed files with 66 additions and 96 deletions

View File

@@ -4,6 +4,7 @@ module Redistat
base.extend(Database)
end
def db(ref = nil)
ref ||= @options[:connection_ref] if !@options.nil?
Redistat.connection(ref)
end
end

View File

@@ -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?
@@ -75,7 +63,7 @@ module Redistat
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}",

View File

@@ -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?
@@ -28,7 +27,7 @@ module Redistat
attr_reader :date
def depth
@options[:depth]
options[:depth]
end
def scope

View File

@@ -1,25 +1,24 @@
module Redistat
class Label
include Database
include Options
attr_reader :connection_ref
def self.create(name, options = {})
self.new(name, options).save
def default_options
{ :hashed_label => false }
end
def initialize(str, options = {})
@options = options
def self.create(name, opts = {})
self.new(name, opts).save
end
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
@@ -35,6 +34,7 @@ module Redistat
end
def saved?
return true unless @options[:hashed_label]
@saved ||= false
end

View File

@@ -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
@@ -27,47 +29,24 @@ module Redistat
:till => till }.merge(options.merge(opts)) )
end
#
# options methods
#
option_accessor :depth
option_accessor :class_name
option_accessor :store_event
option_accessor :hashed_label
option_accessor :label_indexing
alias :scope :class_name
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
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
end
def store_event(boolean = nil)
if !boolean.nil?
options[:store_event] = boolean
else
options[:store_event] || nil
end
end
#
# resource access methods
@@ -78,10 +57,6 @@ module Redistat
end
alias :redis :connection
def options
@options ||= {}
end
def name
options[:class_name] || (@name ||= self.to_s)
end

View File

@@ -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
depth_limit ||= key.depth
enable_grouping = true if enable_grouping.nil?
options = default_options.merge((opts || {}).reject { |k,v| v.nil? })
if enable_grouping
depth_limit ||= key.depth
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