mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
initial import
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.DS_Store
|
||||
spec/db/*.pid
|
||||
34
Rakefile
Normal file
34
Rakefile
Normal file
@@ -0,0 +1,34 @@
|
||||
require "rubygems"
|
||||
|
||||
require "spec"
|
||||
require "spec/rake/spectask"
|
||||
|
||||
REDIS_DIR = File.expand_path(File.join("..", "spec"), __FILE__)
|
||||
REDIS_CNF = File.join(REDIS_DIR, "redis-test.conf")
|
||||
REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
|
||||
|
||||
|
||||
task :default => [:start, :spec, :stop]
|
||||
|
||||
|
||||
# define the spec task
|
||||
Spec::Rake::SpecTask.new do |t|
|
||||
t.spec_opts = %w(--format specdoc --colour)
|
||||
t.libs = ["spec"]
|
||||
end
|
||||
|
||||
|
||||
desc "Start the Redis test server"
|
||||
task :start do
|
||||
unless File.exists?(REDIS_PID)
|
||||
system "redis-server #{REDIS_CNF}"
|
||||
end
|
||||
end
|
||||
|
||||
desc "Stop the Redis test server"
|
||||
task :stop do
|
||||
if File.exists?(REDIS_PID)
|
||||
system "kill #{File.read(REDIS_PID)}"
|
||||
system "rm #{REDIS_PID}"
|
||||
end
|
||||
end
|
||||
83
lib/redistat.rb
Normal file
83
lib/redistat.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
require "redis"
|
||||
require "yaml"
|
||||
require "time"
|
||||
require "digest/sha1"
|
||||
|
||||
require "redistat/extensions/date_time"
|
||||
require "redistat/database"
|
||||
require "redistat/model"
|
||||
require "redistat/event"
|
||||
require "redistat/key"
|
||||
require "redistat/label"
|
||||
require "redistat/date"
|
||||
|
||||
module Redistat
|
||||
|
||||
# Provides access to the Redis database. This is shared accross all models and instances.
|
||||
def redis
|
||||
threaded[:redis] ||= connection(*options)
|
||||
end
|
||||
|
||||
def redis=(connection)
|
||||
threaded[:redis] = connection
|
||||
end
|
||||
|
||||
def threaded
|
||||
Thread.current[:redistat] ||= {}
|
||||
end
|
||||
|
||||
# Connect to a redis database.
|
||||
#
|
||||
# @param options [Hash] options to create a message with.
|
||||
# @option options [#to_s] :host ('127.0.0.1') Host of the redis database.
|
||||
# @option options [#to_s] :port (6379) Port number.
|
||||
# @option options [#to_s] :db (0) Database number.
|
||||
# @option options [#to_s] :timeout (0) Database timeout in seconds.
|
||||
# @example Connect to a database in port 6380.
|
||||
# Ohm.connect(:port => 6380)
|
||||
def connect(*options)
|
||||
self.redis = nil
|
||||
@options = options
|
||||
end
|
||||
|
||||
# Return a connection to Redis.
|
||||
#
|
||||
# This is a wapper around Redis.new(options)
|
||||
def connection(*options)
|
||||
Redis.new(*options)
|
||||
end
|
||||
|
||||
def options
|
||||
@options = [] unless defined? @options
|
||||
@options
|
||||
end
|
||||
|
||||
# Clear the database.
|
||||
def flush
|
||||
redis.flushdb
|
||||
end
|
||||
|
||||
module_function :connect, :connection, :flush, :redis, :redis=, :options, :threaded
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
7
lib/redistat/database.rb
Normal file
7
lib/redistat/database.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Redistat
|
||||
module Database
|
||||
def db
|
||||
Redistat.redis
|
||||
end
|
||||
end
|
||||
end
|
||||
50
lib/redistat/date.rb
Normal file
50
lib/redistat/date.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
module Redistat
|
||||
class Date
|
||||
|
||||
attr_accessor :year
|
||||
attr_accessor :month
|
||||
attr_accessor :day
|
||||
attr_accessor :hour
|
||||
attr_accessor :min
|
||||
attr_accessor :sec
|
||||
|
||||
def initialize(input)
|
||||
if input.is_a?(::Time)
|
||||
from_time(input)
|
||||
elsif input.is_a?(::Date)
|
||||
from_date(input)
|
||||
elsif input.is_a?(::String)
|
||||
from_string(input)
|
||||
end
|
||||
end
|
||||
|
||||
def from_time(input)
|
||||
[:year, :month, :day, :hour, :min, :sec].each do |k|
|
||||
self.send("#{k}=", input.send(k))
|
||||
end
|
||||
end
|
||||
|
||||
def from_date(input)
|
||||
[:year, :month, :day].each do |k|
|
||||
self.send("#{k}=", input.send(k))
|
||||
end
|
||||
end
|
||||
|
||||
def from_string(input)
|
||||
from_time(Time.parse(input))
|
||||
end
|
||||
|
||||
def to_s(depth = :sec)
|
||||
output = ""
|
||||
[:year, :month, :day, :hour, :min, :sec].each_with_index do |current, i|
|
||||
break if self.send(current).nil?
|
||||
output << self.send(current).to_s.rjust((i <= 0) ? 4 : 2, '0')
|
||||
break if current == depth
|
||||
end
|
||||
output
|
||||
end
|
||||
|
||||
alias :to_string :to_s
|
||||
|
||||
end
|
||||
end
|
||||
18
lib/redistat/event.rb
Normal file
18
lib/redistat/event.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Redistat
|
||||
class Event
|
||||
|
||||
attr_accessor :label
|
||||
attr_reader :options
|
||||
|
||||
def initialize(scope, options, data = {}, label = nil, time = nil)
|
||||
key = [scope]
|
||||
key << Digest::SHA1.hexdigest(label) if !label.nil?
|
||||
|
||||
time ||= Time.now
|
||||
key << time.to_redistat(options ||= nil)
|
||||
|
||||
puts key.inspect
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
11
lib/redistat/extensions/date_time.rb
Normal file
11
lib/redistat/extensions/date_time.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class Date
|
||||
def to_redistat(depth = nil)
|
||||
Redistat::Date.new(self).to_s(depth)
|
||||
end
|
||||
end
|
||||
|
||||
class Time
|
||||
def to_redistat(depth = nil)
|
||||
Redistat::Date.new(self).to_s(depth)
|
||||
end
|
||||
end
|
||||
28
lib/redistat/key.rb
Normal file
28
lib/redistat/key.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
module Redistat
|
||||
class Key
|
||||
|
||||
attr_accessor :scope
|
||||
attr_accessor :label
|
||||
attr_accessor :date
|
||||
|
||||
def initialize(scope, label = nil, date = nil, options = {})
|
||||
@scope = scope
|
||||
@label = Label.create(label) if !label.nil?
|
||||
@date = date ||= Time.now
|
||||
@options = options
|
||||
end
|
||||
|
||||
def label
|
||||
if !@label.nil?
|
||||
(options[:hash_label] ||= true) ? @label.hash : @label.name
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
key = "#{@scope}"
|
||||
key << "/#{label}" if !label.nil?
|
||||
key << ":#{@date.to_redistat(@options[:depth] ||= nil)}"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
28
lib/redistat/label.rb
Normal file
28
lib/redistat/label.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
module Redistat
|
||||
class Label
|
||||
include Database
|
||||
|
||||
attr_reader :name
|
||||
attr_reader :hash
|
||||
|
||||
def initialize(str)
|
||||
@name = str
|
||||
@hash = Digest::SHA1.hexdigest(@name)
|
||||
end
|
||||
|
||||
def save
|
||||
db.set("Redistat:lables:#{@hash}", @name)
|
||||
@saved = true
|
||||
self
|
||||
end
|
||||
|
||||
def saved?
|
||||
@saved ||= false
|
||||
end
|
||||
|
||||
def self.create(name)
|
||||
self.new(name).save
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
13
lib/redistat/model.rb
Normal file
13
lib/redistat/model.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Redistat
|
||||
class Model
|
||||
|
||||
def self.create(*args)
|
||||
Event.new(self.name, self.options, *args)
|
||||
end
|
||||
|
||||
def self.options
|
||||
@options ||= {}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
10
spec/_redistat_spec.rb
Normal file
10
spec/_redistat_spec.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe Redistat do
|
||||
|
||||
it "should create a valid redis connection to correct server" do
|
||||
Redistat.redis.should_not be_nil
|
||||
Redistat.redis.client.port.should == 8379
|
||||
end
|
||||
|
||||
end
|
||||
42
spec/date_spec.rb
Normal file
42
spec/date_spec.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe Redistat::Date do
|
||||
|
||||
it "should initialize from Time object" do
|
||||
now = Time.now
|
||||
rdate = Redistat::Date.new(now)
|
||||
[:year, :month, :day, :hour, :min, :sec].each { |k| rdate.send(k).should == now.send(k) }
|
||||
end
|
||||
|
||||
it "should initialize from Date object" do
|
||||
today = Date.today
|
||||
rdate = Redistat::Date.new(today)
|
||||
[:year, :month, :day].each { |k| rdate.send(k).should == today.send(k) }
|
||||
[:hour, :min, :sec].each { |k| rdate.send(k).should == nil }
|
||||
end
|
||||
|
||||
it "should initialize from String object" do
|
||||
now = Time.now
|
||||
rdate = Redistat::Date.new(now.to_s)
|
||||
[:year, :month, :day, :hour, :min, :sec].each { |k| rdate.send(k).should == now.send(k) }
|
||||
end
|
||||
|
||||
it "should convert to string with correct depths" do
|
||||
today = Date.today
|
||||
rdate = Redistat::Date.new(today)
|
||||
props = [:year, :month, :day, nil]
|
||||
props.each do
|
||||
rdate.to_s(props.last).should == props.map { |k| today.send(k).to_s.rjust(2, '0') if !k.nil? }.join
|
||||
props.pop
|
||||
end
|
||||
|
||||
now = Time.now
|
||||
rdate = Redistat::Date.new(now)
|
||||
props = [:year, :month, :day, :hour, :min, :sec, nil]
|
||||
props.each do
|
||||
rdate.to_s(props.last).should == props.map { |k| now.send(k).to_s.rjust(2, '0') if !k.nil? }.join
|
||||
props.pop
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
1
spec/db/dump.rdb
Normal file
1
spec/db/dump.rdb
Normal file
@@ -0,0 +1 @@
|
||||
REDIS0001<EFBFBD>
|
||||
7
spec/key_spec.rb
Normal file
7
spec/key_spec.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe Redistat::Key do
|
||||
|
||||
|
||||
|
||||
end
|
||||
12
spec/label_spec.rb
Normal file
12
spec/label_spec.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
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)
|
||||
end
|
||||
|
||||
end
|
||||
9
spec/redis-test.conf
Normal file
9
spec/redis-test.conf
Normal file
@@ -0,0 +1,9 @@
|
||||
daemonize yes
|
||||
dir ./spec/db
|
||||
pidfile ./redis.pid
|
||||
port 8379
|
||||
bind 127.0.0.1
|
||||
timeout 300
|
||||
loglevel debug
|
||||
logfile stdout
|
||||
databases 16
|
||||
5
spec/spec_helper.rb
Normal file
5
spec/spec_helper.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
require "rubygems"
|
||||
require File.dirname(__FILE__) + "/../lib/redistat"
|
||||
|
||||
Redistat.connect({:port => 8379, :db => 15})
|
||||
Redistat.flush
|
||||
Reference in New Issue
Block a user