From b6aa9c65ac534a41bc4ab305f6a4f8d4115f9c15 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 15 Oct 2010 01:47:16 +0100 Subject: [PATCH] finder and it's specs work! --- lib/redistat/finder.rb | 57 +++++++++++++++++++++++++++++++++++++++--- spec/finder_spec.rb | 27 ++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/redistat/finder.rb b/lib/redistat/finder.rb index 1eee4bb..10d2d19 100644 --- a/lib/redistat/finder.rb +++ b/lib/redistat/finder.rb @@ -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) diff --git a/spec/finder_spec.rb b/spec/finder_spec.rb index 54f9504..5ac6e50 100644 --- a/spec/finder_spec.rb +++ b/spec/finder_spec.rb @@ -1,6 +1,11 @@ require "spec_helper" describe Redistat::Finder do + include Redistat::Database + + before(:each) do + db.flushdb + end it "should initialize properly" do two_hours_ago = 2.hours.ago @@ -33,4 +38,26 @@ describe Redistat::Finder do end + it "should fetch stats properly" do + # pending "needs reimplementation" + + @scope = "PageViews" + @label = "about_us" + @date = Time.now + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day}) + @stats = {"views" => 3, "visitors" => 2} + + key = Redistat::Key.new(@scope, @label, 2.hours.ago) + Redistat::Summary.update(key, @stats, :hour) + key = Redistat::Key.new(@scope, @label, 1.hours.ago) + Redistat::Summary.update(key, @stats, :hour) + key = Redistat::Key.new(@scope, @label, 24.minutes.ago) + Redistat::Summary.update(key, @stats, :hour) + + # stats = Redistat::Summary.find(key, 3.hours.ago, 2.hour.from_now, :hour) + stats = Redistat::Finder.find({:from => 3.hours.ago, :till => 2.hours.from_now, :scope => @scope, :label => @label, :depth => :hour}) + stats.should == { "views" => 9, "visitors" => 6 } + + end + end \ No newline at end of file