mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
drastic change in label indexing
This commit is contained in:
@@ -40,8 +40,12 @@ module Redistat
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize(options = {})
|
||||
@options = options
|
||||
def initialize(opts = {})
|
||||
set_options(opts)
|
||||
end
|
||||
|
||||
def options
|
||||
@options ||= {}
|
||||
end
|
||||
|
||||
def all(reload = false)
|
||||
@@ -65,21 +69,27 @@ module Redistat
|
||||
all.each_with_index(&block)
|
||||
end
|
||||
|
||||
def children
|
||||
build_key.children.map { |key|
|
||||
self.class.new(options.merge(:label => key.label.to_s))
|
||||
}
|
||||
end
|
||||
|
||||
def connection_ref(ref)
|
||||
reset! if @options[:connection_ref] != ref
|
||||
@options[:connection_ref] = ref
|
||||
reset! if options[:connection_ref] != ref
|
||||
options[:connection_ref] = ref
|
||||
self
|
||||
end
|
||||
|
||||
def scope(scope)
|
||||
reset! if @options[:scope].to_s != scope
|
||||
@options[:scope] = Scope.new(scope)
|
||||
reset! if !options[:scope].nil? && options[:scope].to_s != scope
|
||||
options[:scope] = Scope.new(scope)
|
||||
self
|
||||
end
|
||||
|
||||
def label(label)
|
||||
reset! if @options[:label].raw != label
|
||||
@options[:label] = Label.new(label)
|
||||
reset! if !options[:label].nil? && options[:label].to_s != label
|
||||
options[:label] = Label.new(label)
|
||||
self
|
||||
end
|
||||
|
||||
@@ -89,34 +99,34 @@ module Redistat
|
||||
alias :date :dates
|
||||
|
||||
def from(date)
|
||||
reset! if @options[:from] != date
|
||||
@options[:from] = date
|
||||
reset! if options[:from] != date
|
||||
options[:from] = date
|
||||
self
|
||||
end
|
||||
|
||||
def till(date)
|
||||
reset! if @options[:till] != date
|
||||
@options[:till] = date
|
||||
reset! if options[:till] != date
|
||||
options[:till] = date
|
||||
self
|
||||
end
|
||||
alias :until :till
|
||||
|
||||
def depth(unit)
|
||||
reset! if @options[:depth] != unit
|
||||
@options[:depth] = unit
|
||||
reset! if options[:depth] != unit
|
||||
options[:depth] = unit
|
||||
self
|
||||
end
|
||||
|
||||
def interval(unit)
|
||||
reset! if @options[:interval] != unit
|
||||
@options[:interval] = unit
|
||||
reset! if options[:interval] != unit
|
||||
options[:interval] = unit
|
||||
self
|
||||
end
|
||||
|
||||
def find(options = {})
|
||||
set_options(options)
|
||||
def find(opts = {})
|
||||
set_options(opts)
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
if @options[:interval].nil? || !@options[:interval]
|
||||
if options[:interval].nil? || !options[:interval]
|
||||
find_by_magic
|
||||
else
|
||||
find_by_interval
|
||||
@@ -130,14 +140,14 @@ module Redistat
|
||||
opts.each do |key, value|
|
||||
self.send(key, opts.delete(key)) if self.respond_to?(key)
|
||||
end
|
||||
@options.merge!(opts)
|
||||
self.options.merge!(opts)
|
||||
end
|
||||
|
||||
def find_by_interval(options = {})
|
||||
def find_by_interval
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = build_key
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
col = Collection.new(options)
|
||||
col.total = Result.new(options)
|
||||
build_date_sets.each do |set|
|
||||
set[:add].each do |date|
|
||||
result = Result.new
|
||||
@@ -152,11 +162,11 @@ module Redistat
|
||||
col
|
||||
end
|
||||
|
||||
def find_by_magic(options = {})
|
||||
def find_by_magic
|
||||
raise InvalidOptions.new if !valid_options?
|
||||
key = build_key
|
||||
col = Collection.new(@options)
|
||||
col.total = Result.new(@options)
|
||||
col = Collection.new(options)
|
||||
col.total = Result.new(options)
|
||||
col << col.total
|
||||
build_date_sets.each do |set|
|
||||
sum = Result.new
|
||||
@@ -174,16 +184,16 @@ module Redistat
|
||||
end
|
||||
|
||||
def valid_options?
|
||||
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
|
||||
return true if !options[:scope].blank? && !options[:label].blank? && !options[:from].blank? && !options[:till].blank?
|
||||
false
|
||||
end
|
||||
|
||||
def build_date_sets
|
||||
Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
|
||||
Finder::DateSet.new(options[:from], options[:till], options[:depth], options[:interval])
|
||||
end
|
||||
|
||||
def build_key
|
||||
Key.new(@options[:scope], @options[:label])
|
||||
Key.new(options[:scope], options[:label])
|
||||
end
|
||||
|
||||
def summarize_add_keys(sets, key, sum)
|
||||
@@ -205,7 +215,7 @@ module Redistat
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
super(options[:connection_ref])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module Redistat
|
||||
class Key
|
||||
include Redistat::Database
|
||||
|
||||
attr_accessor :date
|
||||
attr_accessor :options
|
||||
|
||||
def initialize(scope, label_name = nil, time_stamp = nil, options = {})
|
||||
@@ -17,7 +17,7 @@ module Redistat
|
||||
|
||||
def prefix
|
||||
key = "#{@scope}"
|
||||
key << "/#{label}" if !label.nil?
|
||||
key << "/#{label.name}" if !label.nil?
|
||||
key << ":"
|
||||
key
|
||||
end
|
||||
@@ -25,27 +25,12 @@ module Redistat
|
||||
def date=(input)
|
||||
@date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date
|
||||
end
|
||||
attr_reader :date
|
||||
|
||||
def depth
|
||||
@options[:depth]
|
||||
end
|
||||
|
||||
def label
|
||||
@label.name
|
||||
end
|
||||
|
||||
def label=(input)
|
||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
||||
end
|
||||
|
||||
def label_hash
|
||||
@label.hash
|
||||
end
|
||||
|
||||
def label_groups
|
||||
@label.groups
|
||||
end
|
||||
|
||||
def scope
|
||||
@scope.to_s
|
||||
end
|
||||
@@ -54,18 +39,36 @@ module Redistat
|
||||
@scope = (input.instance_of?(Redistat::Scope)) ? input : Scope.new(input)
|
||||
end
|
||||
|
||||
def groups
|
||||
@groups ||= label_groups.map do |label_name|
|
||||
self.class.new(@scope, label_name, self.date, @options)
|
||||
def label=(input)
|
||||
@label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options)
|
||||
end
|
||||
attr_reader :label
|
||||
|
||||
def label_hash
|
||||
@label.hash
|
||||
end
|
||||
|
||||
def parent
|
||||
@parent ||= self.class.new(self.scope, @label.parent, self.date, @options) unless @label.parent.nil?
|
||||
end
|
||||
|
||||
def children
|
||||
db.smembers("#{scope}#{LABEL_INDEX}#{@label}").map { |member|
|
||||
self.class.new(self.scope, "#{@label}#{GROUP_SEPARATOR}#{member}", self.date, @options)
|
||||
}
|
||||
end
|
||||
|
||||
def update_index
|
||||
@label.groups.each do |label|
|
||||
break if label.parent.nil?
|
||||
db.sadd("#{scope}#{LABEL_INDEX}#{label.parent}", label.me)
|
||||
end
|
||||
end
|
||||
|
||||
def parent_group
|
||||
@label.parent_group
|
||||
end
|
||||
|
||||
def update_label_index
|
||||
@label.update_index
|
||||
def groups # TODO: Is this useless?
|
||||
@groups ||= @label.groups.map do |label|
|
||||
self.class.new(@scope, label, self.date, @options)
|
||||
end
|
||||
end
|
||||
|
||||
def to_s(depth = nil)
|
||||
|
||||
@@ -2,7 +2,6 @@ module Redistat
|
||||
class Label
|
||||
include Database
|
||||
|
||||
attr_reader :raw
|
||||
attr_reader :connection_ref
|
||||
|
||||
def self.create(name, options = {})
|
||||
@@ -13,22 +12,25 @@ module Redistat
|
||||
@options = options
|
||||
@raw = str.to_s
|
||||
end
|
||||
|
||||
def to_s
|
||||
@raw
|
||||
end
|
||||
|
||||
def db
|
||||
super(@options[:connection_ref])
|
||||
end
|
||||
|
||||
def name
|
||||
@options[:hashed_label] ? hash : @raw
|
||||
@options[:hashed_label] ? hash : self.to_s
|
||||
end
|
||||
|
||||
def hash
|
||||
@hash ||= Digest::SHA1.hexdigest(@raw)
|
||||
@hash ||= Digest::SHA1.hexdigest(self.to_s)
|
||||
end
|
||||
|
||||
def save
|
||||
@saved = (db.set("#{KEY_LEBELS}#{hash}", @raw) == "OK") if @options[:hashed_label]
|
||||
update_index if groups.size > 1 # TODO: add a label_indexing option
|
||||
@saved = (db.set("#{KEY_LEBELS}#{hash}", self.to_s) == "OK") if @options[:hashed_label]
|
||||
self
|
||||
end
|
||||
|
||||
@@ -37,44 +39,26 @@ module Redistat
|
||||
end
|
||||
|
||||
def parent
|
||||
@parent ||= self.class.new(parent_group)
|
||||
@parent ||= groups[1] if groups.size > 1
|
||||
end
|
||||
|
||||
def parent_group
|
||||
groups[1]
|
||||
end
|
||||
|
||||
def group
|
||||
@raw.split(GROUP_SEPARATOR).last
|
||||
def me
|
||||
self.to_s.split(GROUP_SEPARATOR).last
|
||||
end
|
||||
|
||||
def groups
|
||||
return @groups if @groups
|
||||
return @groups unless @groups.nil?
|
||||
@groups = []
|
||||
parent = ""
|
||||
@raw.split(GROUP_SEPARATOR).each do |part|
|
||||
self.to_s.split(GROUP_SEPARATOR).each do |part|
|
||||
if !part.blank?
|
||||
group = ((parent.blank?) ? "" : "#{parent}/") + part
|
||||
@groups << group
|
||||
group = ((parent.blank?) ? "" : "#{parent}#{GROUP_SEPARATOR}") + part
|
||||
@groups << Label.new(group)
|
||||
parent = group
|
||||
end
|
||||
end
|
||||
@groups.reverse!
|
||||
end
|
||||
|
||||
def sub_labels
|
||||
db.smembers("#{LABEL_INDEX}#{parent_group}").map { |member|
|
||||
self.class.new("#{parent_group}#{GROUP_SEPARATOR}#{member}")
|
||||
}
|
||||
end
|
||||
|
||||
def update_index
|
||||
groups.each do |group|
|
||||
label = self.class.new(group)
|
||||
break if label.parent_group.nil?
|
||||
db.sadd("#{LABEL_INDEX}#{label.parent_group}", label.group) == "OK" ? true : false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -13,7 +13,7 @@ module Redistat
|
||||
stats = inject_group_summaries(stats)
|
||||
key.groups.each { |k|
|
||||
update_key(k, stats, depth_limit, connection_ref)
|
||||
k.update_label_index # TODO: add a label_indexing option
|
||||
k.update_index # TODO: add a label_indexing option
|
||||
}
|
||||
else
|
||||
update_key(key, stats, depth_limit, connection_ref)
|
||||
|
||||
Reference in New Issue
Block a user