mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
Added support for connection_ref's down throughout
the code, so models can connect to specific Redis servers. I believe a lot of the code needs some restructuring at some point down the line to handle multiple connections in a cleaner way, but for now it'll do.
This commit is contained in:
@@ -3,8 +3,8 @@ module Redistat
|
||||
def self.included(base)
|
||||
base.extend(Database)
|
||||
end
|
||||
def db
|
||||
Redistat.connection
|
||||
def db(ref = nil)
|
||||
Redistat.connection(ref)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,21 +4,34 @@ module Redistat
|
||||
|
||||
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 = default_options.merge(options)
|
||||
@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 }
|
||||
{ :depth => :hour, :store_event => false, :connection_ref => nil }
|
||||
end
|
||||
|
||||
def new?
|
||||
@@ -59,7 +72,7 @@ module Redistat
|
||||
|
||||
def save
|
||||
return false if !self.new?
|
||||
Summary.update_all(@key, @stats, depth_limit)
|
||||
Summary.update_all(@key, @stats, depth_limit, @connection_ref)
|
||||
if @options[:store_event]
|
||||
@id = self.next_id
|
||||
db.hmset("#{self.scope}#{KEY_EVENT}#{@id}",
|
||||
|
||||
@@ -8,6 +8,10 @@ module Redistat
|
||||
@options = options
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
end
|
||||
|
||||
def valid_options?
|
||||
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
|
||||
false
|
||||
|
||||
@@ -3,11 +3,16 @@ module Redistat
|
||||
include Database
|
||||
|
||||
attr_reader :raw
|
||||
attr_reader :connection_ref
|
||||
|
||||
def initialize(str, options = {})
|
||||
@options = options
|
||||
@raw = str.to_s
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
end
|
||||
|
||||
def name
|
||||
@options[:hashed_label] ? hash : @raw
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
module Redistat
|
||||
module Model
|
||||
include Redistat::Database
|
||||
|
||||
def self.included(base)
|
||||
base.extend(self)
|
||||
@@ -10,6 +11,16 @@ module Redistat
|
||||
end
|
||||
alias :event :store
|
||||
|
||||
def connect_to(opts = {})
|
||||
Connection.create(opts.merge(:ref => name))
|
||||
options[:connection_ref] = name
|
||||
end
|
||||
|
||||
def connection
|
||||
db(options[:connection_ref])
|
||||
end
|
||||
alias :redis :connection
|
||||
|
||||
def fetch(label, from, till, opts = {})
|
||||
Finder.find({
|
||||
:scope => name,
|
||||
|
||||
@@ -2,21 +2,21 @@ module Redistat
|
||||
class Summary
|
||||
include Database
|
||||
|
||||
def self.update_all(key, stats = {}, depth_limit = nil)
|
||||
def self.update_all(key, stats = {}, depth_limit = nil, connection_ref = nil)
|
||||
stats ||= {}
|
||||
depth_limit ||= key.depth
|
||||
return nil if stats.size == 0
|
||||
Date::DEPTHS.each do |depth|
|
||||
update(key, stats, depth)
|
||||
update(key, stats, depth, connection_ref)
|
||||
break if depth == depth_limit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.update(key, stats, depth)
|
||||
def self.update(key, stats, depth, connection_ref = nil)
|
||||
stats.each do |field, value|
|
||||
db.hincrby key.to_s(depth), field, value
|
||||
db(connection_ref).hincrby key.to_s(depth), field, value
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user