mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 05:16:39 +00:00
Merge branch 'release/v0.0.2'
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -23,3 +23,4 @@ pkg
|
||||
.yardoc/*
|
||||
spec/db/*
|
||||
doc
|
||||
redistat.gemspec
|
||||
|
||||
4
Rakefile
4
Rakefile
@@ -5,8 +5,8 @@ begin
|
||||
require 'jeweler'
|
||||
Jeweler::Tasks.new do |gem|
|
||||
gem.name = 'redistat'
|
||||
gem.summary = 'TODO: one-line summary of your gem'
|
||||
gem.description = 'TODO: longer description of your gem'
|
||||
gem.summary = 'A Redis-backed statistics storage and querying library written in Ruby.'
|
||||
gem.description = 'A Redis-backed statistics storage and querying library written in Ruby.'
|
||||
gem.email = 'contact@jimeh.me'
|
||||
gem.homepage = 'http://github.com/jimeh/redistat'
|
||||
gem.authors = ['Jim Myhrberg']
|
||||
|
||||
@@ -5,20 +5,20 @@ module Redistat
|
||||
attr_accessor :date
|
||||
attr_accessor :options
|
||||
|
||||
def initialize(scope, label = nil, date = nil, options = {})
|
||||
def initialize(scope, label_name = nil, time_stamp = nil, options = {})
|
||||
@options = default_options.merge(options || {})
|
||||
@scope = scope
|
||||
self.label = label if !label.nil?
|
||||
self.date = date ||= Time.now
|
||||
@options = default_options.merge(options ||= {})
|
||||
self.label = label_name if !label_name.nil?
|
||||
self.date = time_stamp ||= Time.now
|
||||
end
|
||||
|
||||
def default_options
|
||||
{ :depth => :day }
|
||||
{ :depth => :hour, :hashed_label => false }
|
||||
end
|
||||
|
||||
def prefix
|
||||
key = "#{@scope}"
|
||||
key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil?
|
||||
key << "/#{label}" if !label.nil?
|
||||
key << ":"
|
||||
key
|
||||
end
|
||||
@@ -40,7 +40,7 @@ module Redistat
|
||||
end
|
||||
|
||||
def label=(input)
|
||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input)
|
||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
||||
end
|
||||
|
||||
def to_s(depth = nil)
|
||||
|
||||
@@ -2,16 +2,23 @@ module Redistat
|
||||
class Label
|
||||
include Database
|
||||
|
||||
attr_reader :name
|
||||
attr_reader :hash
|
||||
attr_reader :raw
|
||||
|
||||
def initialize(str)
|
||||
@name = str.to_s
|
||||
@hash = Digest::SHA1.hexdigest(@name)
|
||||
def initialize(str, options = {})
|
||||
@options = options
|
||||
@raw = str.to_s
|
||||
end
|
||||
|
||||
def name
|
||||
@options[:hashed_label] ? hash : @raw
|
||||
end
|
||||
|
||||
def hash
|
||||
@hash ||= Digest::SHA1.hexdigest(@raw)
|
||||
end
|
||||
|
||||
def save
|
||||
@saved = (db.set("#{KEY_LEBELS}#{@hash}", @name) == "OK")
|
||||
@saved = (db.set("#{KEY_LEBELS}#{hash}", @raw) == "OK") if @options[:hashed_label]
|
||||
self
|
||||
end
|
||||
|
||||
@@ -19,8 +26,8 @@ module Redistat
|
||||
@saved ||= false
|
||||
end
|
||||
|
||||
def self.create(name)
|
||||
self.new(name).save
|
||||
def self.create(name, options = {})
|
||||
self.new(name, options).save
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -20,6 +20,14 @@ module Redistat
|
||||
end
|
||||
alias :find :fetch
|
||||
|
||||
def hashed_label(boolean = nil)
|
||||
if !boolean.nil?
|
||||
options[:hashed_label] = boolean
|
||||
else
|
||||
options[:hashed_label] || nil
|
||||
end
|
||||
end
|
||||
|
||||
def depth(depth = nil)
|
||||
if !depth.nil?
|
||||
options[:depth] = depth
|
||||
|
||||
@@ -19,24 +19,24 @@ describe Redistat::Key do
|
||||
end
|
||||
|
||||
it "should convert to string properly" do
|
||||
@key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
|
||||
@key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
|
||||
props = [:year, :month, :day, :hour, :min, :sec]
|
||||
props.each do
|
||||
@key.to_s(props.last).should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(props.last)}"
|
||||
@key.to_s(props.last).should == "#{@scope}/#{@label}:#{@key.date.to_s(props.last)}"
|
||||
props.pop
|
||||
end
|
||||
end
|
||||
|
||||
it "should abide to hash_label option" do
|
||||
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => true})
|
||||
it "should abide to hashed_label option" do
|
||||
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => true})
|
||||
@key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
|
||||
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => false})
|
||||
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => false})
|
||||
@key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
|
||||
end
|
||||
|
||||
it "should have default depth option" do
|
||||
@key = Redistat::Key.new(@scope, @label, @date)
|
||||
@key.depth.should == :day
|
||||
@key.depth.should == :hour
|
||||
end
|
||||
|
||||
it "should allow changing attributes" do
|
||||
|
||||
@@ -15,14 +15,14 @@ describe Redistat::Label do
|
||||
end
|
||||
|
||||
it "should store a label hash lookup key" do
|
||||
@label.save
|
||||
@label.saved?.should be_true
|
||||
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
|
||||
label = Redistat::Label.new(@name, {:hashed_label => true}).save
|
||||
label.saved?.should be_true
|
||||
db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == @name
|
||||
|
||||
@name = "contact_us"
|
||||
@label = Redistat::Label.create(@name)
|
||||
@label.saved?.should be_true
|
||||
db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name
|
||||
name = "contact_us"
|
||||
label = Redistat::Label.create(name, {:hashed_label => true})
|
||||
label.saved?.should be_true
|
||||
db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name
|
||||
end
|
||||
|
||||
end
|
||||
@@ -11,5 +11,6 @@ class ModelHelper2
|
||||
|
||||
depth :day
|
||||
store_event true
|
||||
hashed_label true
|
||||
|
||||
end
|
||||
@@ -16,17 +16,23 @@ describe Redistat::Model do
|
||||
it "should listen to model-defined options" do
|
||||
ModelHelper2.depth.should == :day
|
||||
ModelHelper2.store_event.should == true
|
||||
ModelHelper2.hashed_label.should == true
|
||||
|
||||
ModelHelper.depth.should == nil
|
||||
ModelHelper.store_event.should == nil
|
||||
ModelHelper.hashed_label.should == nil
|
||||
ModelHelper.depth(:hour)
|
||||
ModelHelper.depth.should == :hour
|
||||
ModelHelper.store_event(true)
|
||||
ModelHelper.store_event.should == true
|
||||
ModelHelper.hashed_label(true)
|
||||
ModelHelper.hashed_label.should == true
|
||||
ModelHelper.options[:depth] = nil
|
||||
ModelHelper.options[:store_event] = nil
|
||||
ModelHelper.options[:hashed_label] = nil
|
||||
ModelHelper.depth.should == nil
|
||||
ModelHelper.store_event.should == nil
|
||||
ModelHelper.hashed_label.should == nil
|
||||
end
|
||||
|
||||
it "should store and fetch stats" do
|
||||
|
||||
Reference in New Issue
Block a user