Skip to content
Browse files

Added "duration_since" aka "age_since" aka "elapsed" to cubicle DSL t…

…o allow easy specification of
  • Loading branch information...
1 parent 818b463 commit 6385ac0e60551005c7f507659e14d0d550dd5941 @PlasticLizard committed Mar 29, 2010
View
7 CHANGELOG.rdoc
@@ -1,3 +1,10 @@
+==0.1.5
+ * Added duration_since (aliased as elapsed, age_since) to calculate the time elapsed since any
+ given timestamp and the current execution time of the query.
+
+== 0.1.4
+ * Cached aggregations were being stored on disk, but not properly utilized. This is now fixed.
+
== 0.1.3
* Formalized flat (Cubicle::Data::Table) and hierarchical (Cubicle::Data::Hierarchy) data results from cubicle queries,
and added client side support for rolling up measure values in hierarchical data so that no matter how a given
View
4 cubicle.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{cubicle}
- s.version = "0.1.4"
+ s.version = "0.1.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Nathan Stults"]
- s.date = %q{2010-03-25}
+ s.date = %q{2010-03-29}
s.description = %q{Cubicle provides a dsl and aggregation caching framework for automating the generation, execution and caching of map reduce queries when using MongoDB in Ruby. Cubicle also includes a MongoMapper plugin for quickly performing ad-hoc, multi-level group-by queries against a MongoMapper model.}
s.email = %q{hereiam@sonic.net}
s.extra_rdoc_files = [
View
9 lib/cubicle/aggregation/dsl.rb
@@ -80,6 +80,15 @@ def total_duration(*args)
duration(*(args<<options))
end
+ def duration_since(*args)
+ options = args.extract_options!
+ ms1 = args.length > 1 ? args.delete_at(1) : args.shift
+ options[ms1] = :now
+ duration(*(args<<options))
+ end
+ alias age_since duration_since
+ alias elapsed duration_since
+
def durations_in(unit_of_time = nil)
return (@duration_unit ||= :seconds) unless unit_of_time
@duration_unit = unit_of_time.to_s.pluralize.to_sym
View
2 lib/cubicle/duration.rb
@@ -23,7 +23,7 @@ def expression
cond = " && (#{self.condition})" unless self.condition.blank?
#prefix these names for the expression
prefix = "#{self.timestamp_prefix}#{self.timestamp_prefix.blank? ? '' : '.'}"
- ms1,ms2 = [self.begin_milestone,self.end_milestone].map{|ms|"this.#{prefix}#{ms}"}
+ ms1,ms2 = [self.begin_milestone,self.end_milestone].map{|ms|ms.to_s=='now' ? "new Date(#{Time.now.to_i*1000})" : "this.#{prefix}#{ms}"}
@expression = "(#{ms1} && #{ms2}#{cond}) ? (#{ms2}-#{ms1})/#{denominator} : null"
end
View
2 lib/cubicle/version.rb
@@ -1,3 +1,3 @@
module Cubicle
- VERSION = '0.1.4'
+ VERSION = '0.1.5'
end
View
26 test/cubicle/duration_test.rb
@@ -23,7 +23,7 @@ class DurationTest < ActiveSupport::TestCase
assert_equal 2 * 60 * 60 * 24, a2["ms2_to_ms3_sum"]
assert_equal 4, a2["total_duration"]
end
- should "correctly aggregate durations" do
+ should "correctly aggregate durations" do
results = DefectCubicle.query do
select :all_measures, :product
end
@@ -33,7 +33,7 @@ class DurationTest < ActiveSupport::TestCase
assert_equal 4 * 60 * 60 * 24, results["ms2_to_ms3_sum"]
assert_equal 3.5, results["total_duration"]
- end
+ end
should "respect the condition argument" do
results = DefectCubicle.query do
select :all_measures, :product
@@ -42,6 +42,28 @@ class DurationTest < ActiveSupport::TestCase
assert_equal 3, results["conditional_duration"]
end
+ should "calculate duration_since via elapsed" do
+ Time.now = "1/10/2000"
+ results = DefectCubicle.query do
+ select :all_measures, :product
+ end
+ puts results.inspect
+ results = results[0]
+
+ assert_equal((6+5)/2.0, results["ms3_to_now_average"])
+
+ end
+ should "calculate named duration_since via age_since" do
+ Time.now = "1/10/2000"
+ results = DefectCubicle.query do
+ select :all_measures, :product
+ end
+ puts results.inspect
+ results = results[0]
+
+ assert_equal((6+5)/2.0, results["avg_time_since_ms3"])
+
+ end
end
end
View
6 test/cubicles/defect_cubicle.rb
@@ -23,8 +23,10 @@ class DefectCubicle
#durations
average_duration :ms1 => :ms2
total_duration :ms2 => :ms3
- duration :total_duration, :ms1 => :ms3, :in=>:days
- duration :conditional_duration, :ms1 => :ms3, :in=>:days, :condition=>"this.defect_id != 2"
+ duration :total_duration, :ms1 => :ms3, :in=>:days
+ duration :conditional_duration, :ms1 => :ms3, :in=>:days, :condition=>"this.defect_id != 2"
+ elapsed :ms3, :in=>:days
+ age_since :avg_time_since_ms3, :ms3, :in=>:days
#pre-cached aggregations
aggregation :month, :year, :product

0 comments on commit 6385ac0

Please sign in to comment.
Something went wrong with that request. Please try again.