mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
added proper support for hashed_label option, and
disabled it by default
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user