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