Replaced KeySelection class with a more logical

Finder class.
This commit is contained in:
2010-08-24 13:17:18 +03:00
parent f98472c0ae
commit be464ec298
7 changed files with 447 additions and 214 deletions

View File

@@ -11,8 +11,9 @@ require 'digest/sha1'
require 'redistat/database'
require 'redistat/date'
require 'redistat/event'
require 'redistat/finder'
require 'redistat/finder/date_set'
require 'redistat/key'
require 'redistat/key_selection'
require 'redistat/label'
require 'redistat/model'
require 'redistat/scope'

90
lib/redistat/finder.rb Normal file
View File

@@ -0,0 +1,90 @@
module Redistat
class Finder
attr_reader :options
def initialize(options = {})
@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
class << self
def scope(scope)
new.scope(scope)
end
def label(label)
new.label(label)
end
def dates(from, till)
new.dates(from, till)
end
alias :date :dates
def from(date)
new.from(date)
end
def till(date)
new.till(date)
end
alias :untill :till
def depth(unit)
new.depth(unit)
end
def interval(unit)
new.interval(unit)
end
end
def scope(scope)
@options[:scope] = scope
self
end
def label(label)
@options[:label] = label
self
end
def dates(from, till)
from(from).till(till)
end
alias :date :dates
def from(date)
@options[:from] = date
self
end
def till(date)
@options[:till] = date
self
end
alias :until :till
def depth(unit)
@options[:depth] = unit
self
end
def interval(unit)
@options[:interval] = unit
self
end
end
end

View File

@@ -0,0 +1,95 @@
module Redistat
class Finder
class DateSet < Array
def initialize(start_date = nil, end_date = nil, depth = nil, interval = false)
if !start_date.nil? && !end_date.nil?
find_date_sets(start_date, end_date, depth, interval)
end
end
def find_date_sets(start_date, end_date, depth = nil, interval = false)
start_date = start_date.to_time if start_date.is_a?(::Date)
end_date = end_date.to_time if end_date.is_a?(::Date)
if !interval
find_date_sets_by_magic(start_date, end_date, depth)
else
find_date_sets_by_interval(start_date, end_date, depth)
end
end
private
def find_date_sets_by_magic(start_date, end_date, depth = nil)
depth ||= :hour
depths = Date::DEPTHS[Date::DEPTHS.index(:year), Date::DEPTHS.index(depth)+1].reverse
depths.each_with_index do |d, i|
sets = [find_start_keys_for(d, start_date, end_date, (i == 0))]
sets << find_end_keys_for(d, start_date, end_date, (i == 0))
sets.each do |set|
self << set if set != { :add => [], :sub => [] }
end
end
self
end
def find_date_sets_by_interval(start_date, end_date, depth, inclusive = true)
depth ||= :hour
self << { :add => start_date.map_beginning_of_each(depth, :include_start => inclusive, :include_end => inclusive).until(end_date) { |t| t.to_rs.to_s(depth) }, :sub => [] }
end
def find_start_keys_for(unit, start_date, end_date, lowest_depth = false)
return find_start_year_for(start_date, end_date, lowest_depth) if unit == :year
index = Date::DEPTHS.index(unit)
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
if start_date < start_date.round(nunit) || start_date.next(nunit).beginning_of(nunit) > end_date.beginning_of(nunit)
add = []
start_date.beginning_of_each(unit, :include_start => lowest_depth).until(start_date.end_of(nunit)) do |t|
add << t.to_rs.to_s(unit) if t < end_date.beginning_of(unit)
end
{ :add => add, :sub => [] }
else
{ :add => [start_date.beginning_of(nunit).to_rs.to_s(nunit)],
:sub => start_date.beginning_of(nunit).map_beginning_of_each(unit, :include_start => true, :include_end => !lowest_depth).until(start_date) { |t| t.to_rs.to_s(unit) } }
end
end
def find_end_keys_for(unit, start_date, end_date, lowest_depth = false)
return find_end_year_for(start_date, end_date, lowest_depth) if unit == :year
index = Date::DEPTHS.index(unit)
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
has_nunit = end_date.prev(nunit).beginning_of(nunit) >= start_date.beginning_of(nunit)
nearest_nunit = end_date.round(nunit)
if end_date >= nearest_nunit && has_nunit
add = []
end_date.beginning_of(nunit).beginning_of_each(unit, :include_start => true, :include_end => lowest_depth).until(end_date) do |t|
add << t.to_rs.to_s(unit) if t > start_date.beginning_of(unit)
end
{ :add => add, :sub => [] }
elsif has_nunit
{ :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
:sub => end_date.map_beginning_of_each(unit, :include_start => !lowest_depth).until(end_date.end_of(nunit)) { |t| t.to_rs.to_s(unit) } }
else
{ :add => [], :sub => [] }
end
end
def find_start_year_for(start_date, end_date, lowest_depth = false)
if start_date.years_since(1).beginning_of_year < end_date.beginning_of_year
{ :add => start_date.map_beginning_of_each_year(:include_end => lowest_depth).until(end_date) { |t| t.to_rs.to_s(:year) }, :sub => [] }
else
{ :add => [], :sub => [] }
end
end
def find_end_year_for(start_date, end_date, lowest_depth = false)
if lowest_depth
{ :add => [end_date.beginning_of_year.to_rs.to_s(:year)], :sub => [] }
else
{ :add => [], :sub => [] }
end
end
end
end
end

View File

@@ -16,6 +16,13 @@ module Redistat
{ :depth => :day }
end
def prefix
key = "#{@scope}"
key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil?
key << ":"
key
end
def date=(input)
@date = (input.instance_of?(Redistat::Date)) ? input : Date.new(input) # Redistat::Date, not ::Date
end
@@ -38,9 +45,9 @@ module Redistat
def to_s(depth = nil)
depth ||= @options[:depth]
key = "#{@scope}"
key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil?
key << ":#{@date.to_s(depth)}"
key = self.prefix
key << @date.to_s(depth)
key
end
end

View File

@@ -1,93 +0,0 @@
module Redistat
class KeySelection
attr_reader :sets
def initialize(start_date, end_date, depth = nil, interval = false)
@sets = self.class.find_date_sets(start_date, end_date, depth, interval)
end
def self.find_date_sets(start_date, end_date, depth = nil, interval = false)
start_date = start_date.to_time if start_date.is_a?(::Date)
end_date = end_date.to_time if end_date.is_a?(::Date)
if !interval
find_date_sets_by_magic(start_date, end_date, depth)
else
find_date_sets_by_interval(start_date, end_date, depth)
end
end
private
def self.find_date_sets_by_magic(start_date, end_date, depth = nil)
depth ||= :hour
depths = Date::DEPTHS[Date::DEPTHS.index(:year), Date::DEPTHS.index(depth)+1].reverse
sets = []
depths.each_with_index do |d, i|
sets << find_start_keys_for(d, start_date, end_date, (i == 0))
sets << find_end_keys_for(d, start_date, end_date, (i == 0))
end
sets
end
def self.find_date_sets_by_interval(start_date, end_date, depth)
depth ||= :hour
[{ :add => start_date.map_beginning_of_each(depth, :include_start => true, :include_end => true).until(end_date) { |t| t.to_rs.to_s(depth) },
:sub => [] }]
end
def self.find_start_keys_for(unit, start_date, end_date, lowest_depth = false)
return find_start_year_for(start_date, end_date, lowest_depth) if unit == :year
index = Date::DEPTHS.index(unit)
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
if start_date < start_date.round(nunit) || start_date.next(nunit).beginning_of(nunit) > end_date.beginning_of(nunit)
add = []
start_date.beginning_of_each(unit, :include_start => lowest_depth).until(start_date.end_of(nunit)) do |t|
add << t.to_rs.to_s(unit) if t < end_date.beginning_of(unit)
end
{ :add => add, :sub => [] }
else
{ :add => [start_date.beginning_of(nunit).to_rs.to_s(nunit)],
:sub => start_date.beginning_of(nunit).map_beginning_of_each(unit, :include_start => true, :include_end => !lowest_depth).until(start_date) { |t| t.to_rs.to_s(unit) } }
end
end
def self.find_end_keys_for(unit, start_date, end_date, lowest_depth = false)
return find_end_year_for(start_date, end_date, lowest_depth) if unit == :year
index = Date::DEPTHS.index(unit)
nunit = Date::DEPTHS[(index > 0) ? index-1 : index]
has_nunit = end_date.prev(nunit).beginning_of(nunit) >= start_date.beginning_of(nunit)
nearest_nunit = end_date.round(nunit)
if end_date >= nearest_nunit
add = []
end_date.beginning_of(nunit).beginning_of_each(unit, :include_start => true, :include_end => lowest_depth).until(end_date) do |t|
add << t.to_rs.to_s(unit) if t > start_date.beginning_of(unit)
end
{ :add => add, :sub => [] }
elsif has_nunit
{ :add => [end_date.beginning_of(nunit).to_rs.to_s(nunit)],
:sub => end_date.map_beginning_of_each(unit, :include_start => !lowest_depth).until(end_date.end_of(nunit)) { |t| t.to_rs.to_s(unit) } }
else
{ :add => [], :sub => [] }
end
end
def self.find_start_year_for(start_date, end_date, lowest_depth = false)
if start_date.years_since(1).beginning_of_year < end_date.beginning_of_year
{ :add => start_date.map_beginning_of_each_year(:include_end => lowest_depth).until(end_date) { |t| t.to_rs.to_s(:year) }, :sub => [] }
else
{ :add => [], :sub => [] }
end
end
def self.find_end_year_for(start_date, end_date, lowest_depth = false)
if lowest_depth
{ :add => [end_date.beginning_of_year.to_rs.to_s(:year)], :sub => [] }
else
{ :add => [], :sub => [] }
end
end
end
end

View File

@@ -1,12 +1,15 @@
require "spec_helper"
describe Redistat::KeySelection do
include Redistat::Database
describe Redistat::Finder::DateSet do
it "should find date sets" do
before(:all) do
@finder = Redistat::Finder::DateSet.new
end
it "should initialize properly" do
t_start = Time.utc(2010, 8, 28, 22, 54, 57)
t_end = Time.utc(2013, 12, 4, 22, 52, 3)
result = Redistat::KeySelection.new(t_start, t_end).sets
result = Redistat::Finder::DateSet.new(t_start, t_end)
result.should == [
{ :add => ["2010082822", "2010082823"], :sub => [] },
{ :add => ["20131204"], :sub => ["2013120423"] },
@@ -14,8 +17,7 @@ describe Redistat::KeySelection do
{ :add => ["20131201", "20131202", "20131203"], :sub => [] },
{ :add => ["201009", "201010", "201011", "201012"], :sub => [] },
{ :add => ["2013"], :sub => ["201312"] },
{ :add => ["2011", "2012"], :sub => [] },
{ :add => [], :sub => [] }
{ :add => ["2011", "2012"], :sub => [] }
]
end
@@ -23,12 +25,12 @@ describe Redistat::KeySelection do
t_start = Time.utc(2010, 8, 28, 18, 54, 57)
t_end = t_start + 4.hours
result = Redistat::KeySelection.find_date_sets(t_start, t_end, :hour, true)
result = Redistat::Finder::DateSet.new.find_date_sets(t_start, t_end, :hour, true)
result[0][:add].should == ["2010082818", "2010082819", "2010082820", "2010082821", "2010082822"]
result[0][:sub].should == []
t_end = t_start + 4.days
result = Redistat::KeySelection.find_date_sets(t_start, t_end, :day, true)
result = Redistat::Finder::DateSet.new.find_date_sets(t_start, t_end, :day, true)
result[0][:add].should == ["20100828", "20100829", "20100830", "20100831", "20100901"]
result[0][:sub].should == []
end
@@ -42,28 +44,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2010, 8, 26, 22, 54, 57)
t_end = Time.utc(2013, 12, 14, 22, 52, 3)
result = Redistat::KeySelection.send(:find_start_keys_for, :sec, t_start, t_end)
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100826225458", "20100826225459"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262255", "201008262256", "201008262257", "201008262258", "201008262259"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010082623"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100827", "20100828", "20100829", "20100830", "20100831"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :month, t_start, t_end)
result = @finder.send(:find_start_keys_for, :month, t_start, t_end)
result[:add].should == ["201009", "201010", "201011", "201012"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :year, t_start, t_end)
result = @finder.send(:find_start_keys_for, :year, t_start, t_end)
result[:add].should == ["2011", "2012"]
result[:sub].should == []
@@ -74,28 +76,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2010, 4, 4, 5, 6, 4)
t_end = Time.utc(2011, 2, 14, 22, 52, 3)
result = Redistat::KeySelection.send(:find_start_keys_for, :sec, t_start, t_end)
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end)
result[:add].should == ["201004040506"]
result[:sub].should == ["20100404050600", "20100404050601", "20100404050602", "20100404050603", "20100404050604"]
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["2010040405"]
result[:sub].should == ["201004040500", "201004040501", "201004040502", "201004040503", "201004040504", "201004040505", "201004040506"]
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["20100404"]
result[:sub].should == ["2010040400", "2010040401", "2010040402", "2010040403", "2010040404", "2010040405"]
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["201004"]
result[:sub].should == ["20100401", "20100402", "20100403", "20100404"]
result = Redistat::KeySelection.send(:find_start_keys_for, :month, t_start, t_end)
result = @finder.send(:find_start_keys_for, :month, t_start, t_end)
result[:add].should == ["2010"]
result[:sub].should == ["201001", "201002", "201003", "201004"]
result = Redistat::KeySelection.send(:find_start_keys_for, :year, t_start, t_end)
result = @finder.send(:find_start_keys_for, :year, t_start, t_end)
result[:add].should == []
result[:sub].should == []
@@ -110,28 +112,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2007, 12, 26, 22, 4, 4)
t_end = Time.utc(2010, 5, 7, 5, 6, 3)
result = Redistat::KeySelection.send(:find_end_keys_for, :sec, t_start, t_end)
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100507050600", "20100507050601", "20100507050602"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["201005070500", "201005070501", "201005070502", "201005070503", "201005070504", "201005070505"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010050700", "2010050701", "2010050702", "2010050703", "2010050704"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end)
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == ["20100501", "20100502", "20100503", "20100504", "20100505", "20100506"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :month, t_start, t_end)
result = @finder.send(:find_end_keys_for, :month, t_start, t_end)
result[:add].should == ["201001", "201002", "201003", "201004"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :year, t_start, t_end)
result = @finder.send(:find_end_keys_for, :year, t_start, t_end)
result[:add].should == []
result[:sub].should == []
@@ -142,28 +144,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2009, 12, 26, 22, 4, 4)
t_end = Time.utc(2010, 10, 27, 22, 56, 57)
result = Redistat::KeySelection.send(:find_end_keys_for, :sec, t_start, t_end)
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end)
result[:add].should == ["201010272256"]
result[:sub].should == ["20101027225657", "20101027225658", "20101027225659"]
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010102722"]
result[:sub].should == ["201010272256", "201010272257", "201010272258", "201010272259"]
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["20101027"]
result[:sub].should == ["2010102722", "2010102723"]
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end)
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == ["201010"]
result[:sub].should == ["20101027", "20101028", "20101029", "20101030", "20101031"]
result = Redistat::KeySelection.send(:find_end_keys_for, :month, t_start, t_end)
result = @finder.send(:find_end_keys_for, :month, t_start, t_end)
result[:add].should == ["2010"]
result[:sub].should == ["201010", "201011", "201012"]
result = Redistat::KeySelection.send(:find_end_keys_for, :year, t_start, t_end)
result = @finder.send(:find_end_keys_for, :year, t_start, t_end)
result[:add].should == []
result[:sub].should == []
@@ -175,99 +177,217 @@ describe Redistat::KeySelection do
# Simple fetching with Limits
#
t_start = Time.utc(2010, 8, 26, 20, 54)
# seconds
t_start = Time.utc(2010, 8, 26, 20, 54, 45)
t_end = t_start + 4.seconds
result = Redistat::KeySelection.send(:find_start_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100826205401", "20100826205402", "20100826205403"]
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100826205446", "20100826205447", "20100826205448"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100826205401", "20100826205402", "20100826205403"]
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end)
result[:add].should == []
result[:sub].should == []
t_start = Time.utc(2010, 8, 26, 20, 54, 4)
t_end = t_start + 4.seconds
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end)
result[:add].should == ["20100826205405", "20100826205406", "20100826205407"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end)
result[:add].should == []
result[:sub].should == []
# minutes
t_start = Time.utc(2010, 8, 26, 20, 54)
t_end = t_start + 4.minutes
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262055", "201008262056", "201008262057"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == []
result[:sub].should == []
t_start = Time.utc(2010, 8, 26, 20, 4)
t_end = t_start + 4.minutes
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262005", "201008262006", "201008262007"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == []
result[:sub].should == []
# hours
t_start = Time.utc(2010, 8, 26, 20, 54)
t_end = t_start + 2.hours
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262055", "201008262056", "201008262057", "201008262058", "201008262059"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010082621"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010082622"]
result[:sub].should == ["201008262254", "201008262255", "201008262256", "201008262257", "201008262258", "201008262259"]
t_start = Time.utc(2010, 8, 26, 4, 54)
t_end = t_start + 5.hours
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008260455", "201008260456", "201008260457", "201008260458", "201008260459"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010082605", "2010082606", "2010082607", "2010082608"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010082609"]
result[:sub].should == ["201008260954", "201008260955", "201008260956", "201008260957", "201008260958", "201008260959"]
# days
t_start = Time.utc(2010, 8, 26, 20, 54)
t_end = t_start + 2.day
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262055", "201008262056", "201008262057", "201008262058", "201008262059"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010082621", "2010082622", "2010082623"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100827"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end)
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["20100828"]
result[:sub].should == ["2010082820", "2010082821", "2010082822", "2010082823"]
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010082820"]
result[:sub].should == ["201008282054", "201008282055", "201008282056", "201008282057", "201008282058", "201008282059"]
# months
t_end = t_start + 3.months
t_start = Time.utc(2010, 8, 6, 20, 54)
t_end = t_start + 2.day
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100827", "20100828", "20100829", "20100830", "20100831"]
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008062055", "201008062056", "201008062057", "201008062058", "201008062059"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :month, t_start, t_end)
result[:add].should == ["201009", "201010"]
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010080621", "2010080622", "2010080623"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :month, t_start, t_end)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100807"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["20100808"]
result[:sub].should == ["2010080820", "2010080821", "2010080822", "2010080823"]
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010080820"]
result[:sub].should == ["201008082054", "201008082055", "201008082056", "201008082057", "201008082058", "201008082059"]
# months
t_start = Time.utc(2010, 8, 26, 20, 54)
t_end = t_start + 3.months
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201008262055", "201008262056", "201008262057", "201008262058", "201008262059"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010082621", "2010082622", "2010082623"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100827", "20100828", "20100829", "20100830", "20100831"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :month, t_start, t_end)
result[:add].should == ["201009", "201010"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :month, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == ["201011"]
result[:sub].should == ["20101126", "20101127", "20101128", "20101129", "20101130"]
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["20101126"]
result[:sub].should == ["2010112620", "2010112621", "2010112622", "2010112623"]
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010112620"]
result[:sub].should == ["201011262054", "201011262055", "201011262056", "201011262057", "201011262058", "201011262059"]
t_start = Time.utc(2010, 4, 26, 20, 54)
t_end = t_start + 3.months
result = @finder.send(:find_start_keys_for, :min, t_start, t_end)
result[:add].should == ["201004262055", "201004262056", "201004262057", "201004262058", "201004262059"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end)
result[:add].should == ["2010042621", "2010042622", "2010042623"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :day, t_start, t_end)
result[:add].should == ["20100427", "20100428", "20100429", "20100430"]
result[:sub].should == []
result = @finder.send(:find_start_keys_for, :month, t_start, t_end)
result[:add].should == ["201005", "201006"]
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :month, t_start, t_end)
result[:add].should == []
result[:sub].should == []
result = @finder.send(:find_end_keys_for, :day, t_start, t_end)
result[:add].should == ["201007"]
result[:sub].should == ["20100726", "20100727", "20100728", "20100729", "20100730", "20100731"]
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end)
result[:add].should == ["20100726"]
result[:sub].should == ["2010072620", "2010072621", "2010072622", "2010072623"]
result = @finder.send(:find_end_keys_for, :min, t_start, t_end)
result[:add].should == ["2010072620"]
result[:sub].should == ["201007262054", "201007262055", "201007262056", "201007262057", "201007262058", "201007262059"]
end
it "should find inclusive keys on lowest depth" do
@@ -279,28 +399,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2010, 8, 26, 22, 54, 57)
t_end = Time.utc(2013, 12, 14, 22, 52, 3)
result = Redistat::KeySelection.send(:find_start_keys_for, :sec, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end, true)
result[:add].should == ["20100826225457", "20100826225458", "20100826225459"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end, true)
result[:add].should == ["201008262254", "201008262255", "201008262256", "201008262257", "201008262258", "201008262259"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end, true)
result[:add].should == ["2010082622", "2010082623"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end, true)
result[:add].should == ["20100826", "20100827", "20100828", "20100829", "20100830", "20100831"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :month, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :month, t_start, t_end, true)
result[:add].should == ["201008", "201009", "201010", "201011", "201012"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_start_keys_for, :year, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :year, t_start, t_end, true)
result[:add].should == ["2011", "2012", "2013"]
result[:sub].should == []
@@ -311,28 +431,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2010, 4, 4, 5, 6, 4)
t_end = Time.utc(2013, 2, 14, 22, 52, 3)
result = Redistat::KeySelection.send(:find_start_keys_for, :sec, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :sec, t_start, t_end, true)
result[:add].should == ["201004040506"]
result[:sub].should == ["20100404050600", "20100404050601", "20100404050602", "20100404050603"]
result = Redistat::KeySelection.send(:find_start_keys_for, :min, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :min, t_start, t_end, true)
result[:add].should == ["2010040405"]
result[:sub].should == ["201004040500", "201004040501", "201004040502", "201004040503", "201004040504", "201004040505"]
result = Redistat::KeySelection.send(:find_start_keys_for, :hour, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :hour, t_start, t_end, true)
result[:add].should == ["20100404"]
result[:sub].should == ["2010040400", "2010040401", "2010040402", "2010040403", "2010040404"]
result = Redistat::KeySelection.send(:find_start_keys_for, :day, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :day, t_start, t_end, true)
result[:add].should == ["201004"]
result[:sub].should == ["20100401", "20100402", "20100403"]
result = Redistat::KeySelection.send(:find_start_keys_for, :month, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :month, t_start, t_end, true)
result[:add].should == ["2010"]
result[:sub].should == ["201001", "201002", "201003"]
result = Redistat::KeySelection.send(:find_start_keys_for, :year, t_start, t_end, true)
result = @finder.send(:find_start_keys_for, :year, t_start, t_end, true)
result[:add].should == ["2011", "2012", "2013"]
result[:sub].should == []
@@ -343,28 +463,28 @@ describe Redistat::KeySelection do
t_start = Time.utc(2007, 12, 26, 22, 4, 4)
t_end = Time.utc(2010, 5, 7, 5, 6, 3)
result = Redistat::KeySelection.send(:find_end_keys_for, :sec, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end, true)
result[:add].should == ["20100507050600", "20100507050601", "20100507050602", "20100507050603"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end, true)
result[:add].should == ["201005070500", "201005070501", "201005070502", "201005070503", "201005070504", "201005070505", "201005070506"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end, true)
result[:add].should == ["2010050700", "2010050701", "2010050702", "2010050703", "2010050704", "2010050705"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :day, t_start, t_end, true)
result[:add].should == ["20100501", "20100502", "20100503", "20100504", "20100505", "20100506", "20100507"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :month, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :month, t_start, t_end, true)
result[:add].should == ["201001", "201002", "201003", "201004", "201005"]
result[:sub].should == []
result = Redistat::KeySelection.send(:find_end_keys_for, :year, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :year, t_start, t_end, true)
result[:add].should == ["2010"]
result[:sub].should == []
@@ -375,54 +495,31 @@ describe Redistat::KeySelection do
t_start = Time.utc(2009, 12, 26, 22, 4, 4)
t_end = Time.utc(2010, 10, 27, 22, 56, 57)
result = Redistat::KeySelection.send(:find_end_keys_for, :sec, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :sec, t_start, t_end, true)
result[:add].should == ["201010272256"]
result[:sub].should == ["20101027225658", "20101027225659"]
result = Redistat::KeySelection.send(:find_end_keys_for, :min, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :min, t_start, t_end, true)
result[:add].should == ["2010102722"]
result[:sub].should == ["201010272257", "201010272258", "201010272259"]
result = Redistat::KeySelection.send(:find_end_keys_for, :hour, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :hour, t_start, t_end, true)
result[:add].should == ["20101027"]
result[:sub].should == ["2010102723"]
result = Redistat::KeySelection.send(:find_end_keys_for, :day, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :day, t_start, t_end, true)
result[:add].should == ["201010"]
result[:sub].should == ["20101028", "20101029", "20101030", "20101031"]
result = Redistat::KeySelection.send(:find_end_keys_for, :month, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :month, t_start, t_end, true)
result[:add].should == ["2010"]
result[:sub].should == ["201011", "201012"]
result = Redistat::KeySelection.send(:find_end_keys_for, :year, t_start, t_end, true)
result = @finder.send(:find_end_keys_for, :year, t_start, t_end, true)
result[:add].should == ["2010"]
result[:sub].should == []
end
end
end

36
spec/finder_spec.rb Normal file
View File

@@ -0,0 +1,36 @@
require "spec_helper"
describe Redistat::Finder do
it "should initialize properly" do
two_hours_ago = 2.hours.ago
one_hour_ago = 1.hour.ago
options = {:scope => "PageViews", :label => "Label", :from => two_hours_ago, :till => one_hour_ago, :depth => :hour, :interval => :hour}
finder = Redistat::Finder.new(options)
finder.options.should == options
finder = Redistat::Finder.dates(two_hours_ago, one_hour_ago).scope("PageViews").label("Label").depth(:hour).interval(:hour)
finder.options.should == options
finder = Redistat::Finder.scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour)
finder.options.should == options
finder = Redistat::Finder.label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews")
finder.options.should == options
finder = Redistat::Finder.from(two_hours_ago).till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews").label("Label")
finder.options.should == options
finder = Redistat::Finder.till(one_hour_ago).depth(:hour).interval(:hour).scope("PageViews").label("Label").from(two_hours_ago)
finder.options.should == options
finder = Redistat::Finder.depth(:hour).interval(:hour).scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago)
finder.options.should == options
finder = Redistat::Finder.interval(:hour).scope("PageViews").label("Label").from(two_hours_ago).till(one_hour_ago).depth(:hour)
finder.options.should == options
end
end