Clean up whitespace

This commit is contained in:
2012-04-17 13:03:38 +01:00
parent 4d5998af91
commit 7b14b9b5ab
43 changed files with 594 additions and 607 deletions

View File

@@ -39,58 +39,58 @@ require 'redistat/core_ext'
module Redistat
KEY_NEXT_ID = ".next_id"
KEY_EVENT = ".event:"
KEY_LABELS = "Redistat.labels:" # used for reverse label hash lookup
KEY_EVENT_IDS = ".event_ids"
LABEL_INDEX = ".label_index:"
GROUP_SEPARATOR = "/"
class InvalidOptions < ArgumentError; end
class RedisServerIsTooOld < Exception; end
class << self
def buffer
Buffer.instance
end
def buffer_size
buffer.size
end
def buffer_size=(size)
buffer.size = size
end
def thread_safe
Synchronize.thread_safe
end
def thread_safe=(value)
Synchronize.thread_safe = value
end
def connection(ref = nil)
Connection.get(ref)
end
alias :redis :connection
def connection=(connection)
Connection.add(connection)
end
alias :redis= :connection=
def connect(options)
Connection.create(options)
end
def flush
puts "WARNING: Redistat.flush is deprecated. Use Redistat.redis.flushdb instead."
connection.flushdb
end
end
end

View File

@@ -3,33 +3,33 @@ require 'redistat/core_ext/hash'
module Redistat
class Buffer
include Synchronize
def self.instance
@instance ||= self.new
end
def size
synchronize do
@size ||= 0
end
end
def size=(value)
synchronize do
@size = value
end
end
def count
@count ||= 0
end
def store(key, stats, depth_limit, opts)
return false unless should_buffer?
to_flush = {}
buffkey = buffer_key(key, opts)
synchronize do
if !queue.has_key?(buffkey)
queue[buffkey] = { :key => key,
@@ -37,19 +37,19 @@ module Redistat
:depth_limit => depth_limit,
:opts => opts }
end
queue[buffkey][:stats].merge_and_incr!(stats)
incr_count
# return items to be flushed if buffer size limit has been reached
to_flush = reset_queue
end
# flush any data that's been cleared from the queue
flush_data(to_flush)
true
end
def flush(force = false)
to_flush = {}
synchronize do
@@ -57,28 +57,28 @@ module Redistat
end
flush_data(to_flush)
end
private
# should always be called from within a synchronize block
def incr_count
@count ||= 0
@count += 1
end
def queue
@queue ||= {}
end
def should_buffer?
size > 1 # buffer size of 1 would be equal to not using buffer
end
# should always be called from within a synchronize block
def should_flush?
(!queue.blank? && count >= size)
end
# returns items to be flushed if buffer size limit has been reached
# should always be called from within a synchronize block
def reset_queue(force = false)
@@ -88,13 +88,13 @@ module Redistat
@count = 0
data
end
def flush_data(buffer_data)
buffer_data.each do |k, item|
Summary.update(item[:key], item[:stats], item[:depth_limit], item[:opts])
end
end
def buffer_key(key, opts)
# depth_limit is not needed as it's evident in key.to_s
opts_index = Summary.default_options.keys.sort { |a,b| a.to_s <=> b.to_s }.map do |k|
@@ -102,6 +102,6 @@ module Redistat
end
"#{key.to_s}:#{opts_index.join(':')}"
end
end
end

View File

@@ -1,20 +1,20 @@
module Redistat
class Collection < ::Array
attr_accessor :from
attr_accessor :till
attr_accessor :depth
attr_accessor :total
def initialize(options = {})
@from = options[:from] ||= nil
@till = options[:till] ||= nil
@depth = options[:depth] ||= nil
end
def total
@total ||= {}
end
end
end
end

View File

@@ -2,22 +2,22 @@ require 'monitor'
module Redistat
module Connection
REQUIRED_SERVER_VERSION = "1.3.10"
# TODO: Create a ConnectionPool instance object using Sychronize mixin to replace Connection class
class << self
# TODO: clean/remove all ref-less connections
def get(ref = nil)
ref ||= :default
synchronize do
connections[references[ref]] || create
end
end
def add(conn, ref = nil)
ref ||= :default
synchronize do
@@ -26,7 +26,7 @@ module Redistat
connections[conn.client.id] = conn
end
end
def create(options = {})
synchronize do
options = options.clone
@@ -37,39 +37,39 @@ module Redistat
conn
end
end
def connections
@connections ||= {}
end
def references
@references ||= {}
end
private
def monitor
@monitor ||= Monitor.new
end
def synchronize(&block)
monitor.synchronize(&block)
end
def connection(options)
check_redis_version(Redis.new(options))
end
def connection_id(options = {})
options = options.reverse_merge(default_options)
"redis://#{options[:host]}:#{options[:port]}/#{options[:db]}"
end
def check_redis_version(conn)
raise RedisServerIsTooOld if conn.info["redis_version"] < REQUIRED_SERVER_VERSION
conn
end
def default_options
{
:host => '127.0.0.1',
@@ -78,7 +78,7 @@ module Redistat
:timeout => 5
}
end
end
end
end
end

View File

@@ -2,4 +2,4 @@ require 'redistat/core_ext/bignum'
require 'redistat/core_ext/date'
require 'redistat/core_ext/fixnum'
require 'redistat/core_ext/hash'
require 'redistat/core_ext/time'
require 'redistat/core_ext/time'

View File

@@ -1,8 +1,8 @@
class Bignum
include Redistat::DateHelper
def to_time
Time.at(self)
end
end

View File

@@ -1,8 +1,8 @@
class Date
include Redistat::DateHelper
def to_time
Time.parse(self.to_s)
end
end

View File

@@ -1,8 +1,8 @@
class Fixnum
include Redistat::DateHelper
def to_time
Time.at(self)
end
end

View File

@@ -1,9 +1,9 @@
class Hash
def merge_and_incr(hash)
self.clone.merge_and_incr!(hash)
end
def merge_and_incr!(hash)
raise ArgumentError unless hash.is_a?(Hash)
hash.each do |key, value|
@@ -11,7 +11,7 @@ class Hash
end
self
end
def set_or_incr(key, value)
return false unless value.is_a?(Numeric)
self[key] = 0 unless self.has_key?(key)
@@ -19,5 +19,5 @@ class Hash
self[key] += value
true
end
end

View File

@@ -1,6 +1,6 @@
module Redistat
class Date
attr_accessor :year
attr_accessor :month
attr_accessor :day
@@ -9,9 +9,9 @@ module Redistat
attr_accessor :sec
attr_accessor :usec
attr_accessor :depth
DEPTHS = [:year, :month, :day, :hour, :min, :sec, :usec]
def initialize(input, depth = nil)
@depth = depth
if input.is_a?(::Time)
@@ -26,12 +26,12 @@ module Redistat
from_integer(input)
end
end
def to_t
::Time.local(@year, @month, @day, @hour, @min, @sec, @usec)
end
alias :to_time :to_t
def to_d
::Date.civil(@year, @month, @day)
end
@@ -41,7 +41,7 @@ module Redistat
to_time.to_i
end
alias :to_integer :to_i
def to_s(depth = nil)
depth ||= @depth ||= :sec
output = ""
@@ -57,9 +57,9 @@ module Redistat
output
end
alias :to_string :to_s
private
def from_time(input)
DEPTHS.each do |k|
send("#{k}=", input.send(k))
@@ -74,15 +74,15 @@ module Redistat
send("#{k}=", 0)
end
end
def from_integer(input)
from_time(::Time.at(input))
end
def from_string(input)
input += "19700101000000"[input.size..-1] if input =~ /^\d\d\d[\d]+$/i
from_time(::Time.parse(input))
end
end
end

View File

@@ -2,13 +2,13 @@ module Redistat
class Event
include Database
include Options
attr_reader :id
attr_reader :key
attr_accessor :stats
attr_accessor :meta
def default_options
{ :depth => :hour,
:store_event => false,
@@ -16,7 +16,7 @@ module Redistat
: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)
@@ -24,35 +24,35 @@ module Redistat
@meta = meta ||= {}
@new = is_new
end
def new?
@new
end
def date
@key.date
end
def date=(input)
@key.date = input
end
def scope
@key.scope
end
def scope=(input)
@key.scope = input
end
def label
@key.label
end
def label_hash
@key.label_hash
end
def label=(input)
@key.label = input
end
@@ -60,7 +60,7 @@ module Redistat
def next_id
db.incr("#{self.scope}#{KEY_NEXT_ID}")
end
def save
return false if !self.new?
Summary.update_all(@key, @stats, depth_limit, @options)
@@ -78,21 +78,21 @@ module Redistat
@new = false
self
end
def depth_limit
@options[:depth] ||= @key.depth
end
def self.create(*args)
self.new(*args).save
end
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["options"]), JSON.parse(event["meta"]), false )
end
end
end
end

View File

@@ -3,117 +3,117 @@ require 'redistat/finder/date_set'
module Redistat
class Finder
include Database
class << self
def find(*args)
new.find(*args)
end
def scope(scope)
new.scope(scope)
end
def label(label)
new.label(label)
end
def dates(from, till)
new.dates(from, till)
end
alias :date :dates
def from(date)
new.from(date)
end
def till(date)
new.till(date)
end
alias :untill :till
def depth(unit)
new.depth(unit)
end
def interval(unit)
new.interval(unit)
end
end
attr_reader :options
def initialize(opts = {})
set_options(opts)
end
def options
@options ||= {}
end
def all(reload = false)
@result = nil if reload
@result ||= find
end
def total
all.total
end
def each(&block)
all.each(&block)
end
def map(&block)
all.map(&block)
end
def each_with_index(&block)
all.each_with_index(&block)
end
def parent
@parent ||= self.class.new(options.merge(:label => options[:label].parent)) unless options[:label].nil?
end
def children
build_key.children.map { |key|
self.class.new(options.merge(:label => key.label.to_s))
}
end
def connection_ref(ref = nil)
return options[:connection_ref] if ref.nil?
reset! if options[:connection_ref] != ref
options[:connection_ref] = ref
self
end
def scope(input = nil)
return options[:scope] if input.nil?
reset! if !options[:scope].nil? && options[:scope].to_s != input.to_s
options[:scope] = Scope.new(input)
self
end
def label(input = nil)
return options[:label] if input.nil?
reset! if options.has_key?(:label) && options[:label].to_s != input.to_s
options[:label] = (!input.nil?) ? Label.new(input) : nil
self
end
def dates(start, finish)
from(start).till(finish)
end
alias :date :dates
def from(date = nil)
return options[:from] if date.nil?
reset! if options[:from] != date
options[:from] = date
self
end
def till(date = nil)
return options[:till] if date.nil?
reset! if options[:till] != date
@@ -121,21 +121,21 @@ module Redistat
self
end
alias :until :till
def depth(unit = nil)
return options[:depth] if unit.nil?
reset! if options[:depth] != unit
options[:depth] = unit
self
end
def interval(unit = nil)
return options[:interval] if unit.nil?
reset! if options[:interval] != unit
options[:interval] = unit
self
end
def find(opts = {})
set_options(opts)
raise InvalidOptions.new if !valid_options?
@@ -145,9 +145,9 @@ module Redistat
find_by_interval
end
end
private
def set_options(opts = {})
opts = opts.clone
opts.each do |key, value|
@@ -155,7 +155,7 @@ module Redistat
end
self.options.merge!(opts)
end
def find_by_interval
raise InvalidOptions.new if !valid_options?
key = build_key
@@ -174,7 +174,7 @@ module Redistat
end
col
end
def find_by_magic
raise InvalidOptions.new if !valid_options?
key = build_key
@@ -196,20 +196,20 @@ module Redistat
@result = nil
@parent = nil
end
def valid_options?
return true if !options[:scope].blank? && !options[:label].blank? && !options[:from].blank? && !options[:till].blank?
false
end
def build_date_sets
Finder::DateSet.new(options[:from], options[:till], options[:depth], options[:interval])
end
def build_key
Key.new(options[:scope], options[:label])
end
def summarize_add_keys(sets, key, sum)
sets.each do |date|
db.hgetall("#{key.prefix}#{date}").each do |k, v|
@@ -218,7 +218,7 @@ module Redistat
end
sum
end
def summarize_rem_keys(sets, key, sum)
sets.each do |date|
db.hgetall("#{key.prefix}#{date}").each do |k, v|
@@ -227,10 +227,10 @@ module Redistat
end
sum
end
def db
super(options[:connection_ref])
end
end
end
end

View File

@@ -1,7 +1,7 @@
module Redistat
class Finder
class DateSet < Array
def initialize(start_date = nil, end_date = nil, depth = nil, interval = false)
if !start_date.nil? && !end_date.nil?
find_date_sets(start_date, end_date, depth, interval)
@@ -71,7 +71,7 @@ module Redistat
end
{ :add => add, :rem => [] }
elsif has_nunit
{ :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
{ :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
:rem => end_date.map_beginning_of_each(unit, :include_start => !lowest_depth).until(end_date.end_of(nunit)) { |t| t.to_rs.to_s(unit) } }
else
{ :add => [], :rem => [] }
@@ -93,7 +93,7 @@ module Redistat
{ :add => [], :rem => [] }
end
end
end
end
end
end

View File

@@ -2,56 +2,56 @@ module Redistat
class Key
include Database
include Options
def default_options
{ :depth => :hour }
end
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 prefix
key = "#{@scope}"
key << "/#{label.name}" if !label.nil?
key << ":"
key
end
def date=(input)
@date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date
end
attr_reader :date
def depth
options[:depth]
end
# def scope
# @scope.to_s
# end
def scope=(input)
@scope = (input.instance_of?(Redistat::Scope)) ? input : Scope.new(input)
end
attr_reader :scope
def label=(input)
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
end
attr_reader :label
def label_hash
@label.hash
end
def parent
@parent ||= self.class.new(self.scope, @label.parent, self.date, @options) unless @label.parent.nil?
end
def children
members = db.smembers("#{scope}#{LABEL_INDEX}#{@label}") || [] # older versions of Redis returns nil
members.map { |member|
@@ -59,26 +59,26 @@ module Redistat
self.class.new(self.scope, child_label.join(GROUP_SEPARATOR), self.date, @options)
}
end
def update_index
@label.groups.each do |label|
parent = (label.parent || "")
db.sadd("#{scope}#{LABEL_INDEX}#{parent}", label.me)
end
end
def groups
@groups ||= @label.groups.map do |label|
self.class.new(@scope, label, self.date, @options)
end
end
def to_s(depth = nil)
depth ||= @options[:depth]
key = self.prefix
key << @date.to_s(depth)
key
end
end
end
end

View File

@@ -2,55 +2,55 @@ module Redistat
class Label
include Database
include Options
def default_options
{ :hashed_label => false }
end
def self.create(name, opts = {})
self.new(name, opts).save
end
def self.join(*args)
args = args.map {|i| i.to_s}
self.new(args.reject {|i| i.blank? }.join(GROUP_SEPARATOR))
end
def initialize(str, opts = {})
parse_options(opts)
@raw = str.to_s
end
def to_s
@raw
end
def name
@options[:hashed_label] ? hash : self.to_s
end
def hash
@hash ||= Digest::SHA1.hexdigest(self.to_s)
end
def save
@saved = db.hset(KEY_LABELS, hash, self.to_s) if @options[:hashed_label]
self
end
def saved?
return true unless @options[:hashed_label]
@saved ||= false
end
def parent
@parent ||= groups[1] if groups.size > 1
end
def me
self.to_s.split(GROUP_SEPARATOR).last
end
def groups
return @groups unless @groups.nil?
@groups = []
@@ -64,6 +64,6 @@ module Redistat
end
@groups.reverse!
end
end
end
end

View File

@@ -8,4 +8,4 @@ module Redistat
Redistat.connection(ref)
end
end
end
end

View File

@@ -5,4 +5,4 @@ module Redistat
end
alias :to_rs :to_redistat
end
end
end

View File

@@ -1,10 +1,10 @@
module Redistat
module Options
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def option_accessor(*opts)
opts.each do |option|
@@ -18,24 +18,24 @@ module Redistat
end
end
end
def parse_options(opts)
opts ||= {}
@raw_options = opts
@options = default_options.merge(opts.reject { |k,v| v.nil? })
end
def default_options
{}
end
def options
@options ||= {}
end
def raw_options
@raw_options ||= {}
end
end
end
end

View File

@@ -2,42 +2,42 @@ require 'monitor'
module Redistat
module Synchronize
class << self
def included(base)
base.send(:include, InstanceMethods)
end
def monitor
@monitor ||= Monitor.new
end
def thread_safe
monitor.synchronize do
@thread_safe ||= false
end
end
def thread_safe=(value)
monitor.synchronize do
@thread_safe = value
end
end
end # << self
module InstanceMethods
def thread_safe
Synchronize.thread_safe
end
def thread_safe=(value)
Synchronize.thread_safe = value
end
def monitor
Synchronize.monitor
end
def synchronize(&block)
if thread_safe
monitor.synchronize(&block)
@@ -46,6 +46,6 @@ module Redistat
end
end
end # InstanceMethods
end
end
end

View File

@@ -2,16 +2,16 @@ module Redistat
module Model
include Database
include Options
def self.included(base)
base.extend(self)
end
#
# statistics store/fetch methods
#
#
def store(label, stats = {}, date = nil, opts = {}, meta = {})
Event.new(self.name, label, date, stats, options.merge(opts), meta).save
end
@@ -28,42 +28,42 @@ module Redistat
:from => from,
:till => till }.merge(options.merge(opts)) )
end
def find_event(event_id)
Event.find(self.name, event_id)
end
#
# options methods
#
option_accessor :depth
option_accessor :scope
option_accessor :store_event
option_accessor :hashed_label
option_accessor :label_indexing
alias :class_name :scope
def connect_to(opts = {})
Connection.create(opts.merge(:ref => name))
options[:connection_ref] = name
end
#
# resource access methods
#
def connection
db(options[:connection_ref])
end
alias :redis :connection
def name
options[:scope] || (@name ||= self.to_s)
end
end
end
end

View File

@@ -2,17 +2,17 @@ require 'active_support/core_ext/hash/indifferent_access'
module Redistat
class Result < HashWithIndifferentAccess
attr_accessor :from
attr_accessor :till
alias :date :from
alias :date= :from=
def initialize(options = {})
@from = options[:from] ||= nil
@till = options[:till] ||= nil
end
end
end

View File

@@ -1,18 +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
end

View File

@@ -1,34 +1,34 @@
module Redistat
class Summary
include Database
class << self
def default_options
{ :enable_grouping => true,
:label_indexing => true,
:connection_ref => nil }
end
def buffer
Redistat.buffer
end
def update_all(key, stats = {}, depth_limit = nil, opts = {})
stats ||= {}
return if stats.empty?
options = default_options.merge((opts || {}).reject { |k,v| v.nil? })
depth_limit ||= key.depth
update_through_buffer(key, stats, depth_limit, options)
end
def update_through_buffer(*args)
update(*args) unless buffer.store(*args)
end
def update(key, stats, depth_limit, opts)
if opts[:enable_grouping]
stats = inject_group_summaries(stats)
@@ -40,22 +40,22 @@ module Redistat
update_key(key, stats, depth_limit, opts[:connection_ref])
end
end
private
def update_key(key, stats, depth_limit, connection_ref)
Date::DEPTHS.each do |depth|
update_fields(key, stats, depth, connection_ref)
break if depth == depth_limit
end
end
def update_fields(key, stats, depth, connection_ref = nil)
stats.each do |field, value|
db(connection_ref).hincrby key.to_s(depth), field, value
end
end
def inject_group_summaries!(stats)
summaries = {}
stats.each do |key, value|
@@ -72,11 +72,11 @@ module Redistat
end
stats.merge_and_incr!(summaries)
end
def inject_group_summaries(stats)
inject_group_summaries!(stats.clone)
end
end
end
end
end