mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 05:16:39 +00:00
finder and it's specs work!
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
module Redistat
|
||||
class Finder
|
||||
include Database
|
||||
|
||||
attr_reader :options
|
||||
|
||||
@@ -7,16 +8,64 @@ module Redistat
|
||||
@options = options
|
||||
end
|
||||
|
||||
def builder
|
||||
|
||||
end
|
||||
|
||||
def valid_options?
|
||||
return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank?
|
||||
false
|
||||
end
|
||||
|
||||
def find(options = {})
|
||||
@options.merge!(options)
|
||||
return nil if !valid_options?
|
||||
sets = Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval])
|
||||
key = Key.new(@options[:scope], @options[:label])
|
||||
sum = {}
|
||||
sets.each do |set|
|
||||
set_sum = summarize_add_keys(set[:add], key, {})
|
||||
set_sum = summarize_sub_keys(set[:sub], key, set_sum)
|
||||
set_sum.each do |k, v|
|
||||
if sum.has_key?(k)
|
||||
sum[k] += v.to_i
|
||||
else
|
||||
sum[k] = v.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def summarize_add_keys(the_sets, key, sum)
|
||||
the_sets.each do |date|
|
||||
stat = db.hgetall("#{key.prefix}#{date}")
|
||||
stat.each do |k, v|
|
||||
if sum.has_key?(k)
|
||||
sum[k] += v.to_i
|
||||
else
|
||||
sum[k] = v.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def summarize_sub_keys(the_sets, key, sum)
|
||||
the_sets.each do |date|
|
||||
stat = db.hgetall("#{key.prefix}#{date}")
|
||||
stat.each do |k, v|
|
||||
if sum.has_key?(k)
|
||||
sum[k] -= v.to_i
|
||||
else
|
||||
sum[k] = -v.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
def find(*args)
|
||||
new.find(*args)
|
||||
end
|
||||
|
||||
def scope(scope)
|
||||
new.scope(scope)
|
||||
|
||||
Reference in New Issue
Block a user