Permalink
Browse files

==0.1.16

  *Added a set of default constants for expression templates, such as date_today, date_today_iso, date_today_utc_iso, etc.
  Also added the 'define' method to the query DSL (instead of simply the aggregation DSL) so that you can either define new
  named expressions or override an existing named expression or date constant in a query context, instead of only at the
  global level
  • Loading branch information...
1 parent a4b871b commit 7ea7e045ecd0fb412b34dfc8f5dbef416980c59c @PlasticLizard committed Apr 19, 2010
View
6 CHANGELOG.rdoc
@@ -1,3 +1,9 @@
+==0.1.16
+ *Added a set of default constants for expression templates, such as date_today, date_today_iso, date_today_utc_iso, etc.
+ Also added the 'define' method to the query DSL (instead of simply the aggregation DSL) so that you can either define new
+ named expressions or override an existing named expression or date constant in a query context, instead of only at the
+ global level
+
==0.1.15
*Added 'difference' calculated measure to aggregation DSL, used in the same was as 'ratio' - so, in your aggregation
definition, you can do 'difference :big_diff, :measure_one, :measure_two'
View
2 cubicle.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{cubicle}
- s.version = "0.1.15"
+ s.version = "0.1.16"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Nathan Stults"]
View
1 lib/cubicle.rb
@@ -3,6 +3,7 @@
require "mongo"
require "logger"
require "mustache"
+require "time"
dir = File.dirname(__FILE__)
["mongo_environment",
View
2 lib/cubicle/aggregation/aggregation_manager.rb
@@ -142,7 +142,7 @@ def ensure_indexes(collection_name,dimension_names)
end
def aggregate(query,options={})
- view = AggregationView.new(aggregation)
+ view = AggregationView.new(aggregation,query)
map, reduce = MapReduceHelper.generate_map_function(query), MapReduceHelper.generate_reduce_function
View
20 lib/cubicle/aggregation/aggregation_view.rb
@@ -3,16 +3,28 @@ module Aggregation
class AggregationView < HashWithIndifferentAccess
attr_accessor :aggregation
- def initialize(aggregation)
+ def initialize(aggregation,query)
- super[:time_now] = "new Date(#{Time.now.to_i*1000})"
+ time_now = (query.named_expressions[:time_now] || Time.now).to_time
- self.merge!(aggregation.named_expressions)
+ self[:time_now] = "new Date(#{time_now.to_i*1000})"
+ self[:date_today] = "new Date(#{time_now.to_date.to_time.to_i*1000})"
+ self[:time_now_iso] = "#{time_now.iso8601}"
+ self[:date_today_iso] = "#{time_now.strftime('%Y-%m-%d')}"
+
+ self[:time_now_utc] = "new Date(#{time_now.utc.to_i*1000})"
+ self[:date_today_utc] = "new Date(#{time_now.utc.to_date.to_time.to_i*1000})"
+ self[:time_now_utc_iso] = "#{time_now.utc.iso8601}"
+ self[:date_today_utc_iso] = "#{time_now.utc.strftime('%Y-%m-%d')}"
list = aggregation.measures + aggregation.dimensions
list.each do |m|
- super[m.name] = m.expression
+ self[m.name] = m.expression
end
+
+ self.merge!(aggregation.named_expressions)
+ self.merge!(query.named_expressions)
+
self.each do |key,value|
self[key] = expand_template(value)
end
View
3 lib/cubicle/query.rb
@@ -2,7 +2,7 @@ module Cubicle
class Query
include Dsl
- attr_reader :time_period, :transient, :aggregation
+ attr_reader :time_period, :transient, :aggregation, :named_expressions
attr_accessor :source_collection_name
def initialize(aggregation)
@@ -19,6 +19,7 @@ def initialize(aggregation)
@from_date_filter = "$gte"
@to_date_filter = "$lte"
@query_aliases=HashWithIndifferentAccess.new
+ @named_expressions=HashWithIndifferentAccess.new
end
def clone
View
4 lib/cubicle/query/dsl.rb
@@ -112,6 +112,10 @@ def alias_member(alias_hash)
self
end
alias alias_members alias_member
+
+ def define(expression_name,value)
+ @named_expressions[expression_name] = value
+ end
end
end
View
5 test/cubicle/cubicle_aggregation_test.rb
@@ -10,6 +10,7 @@ class CubicleAggregationTest < ActiveSupport::TestCase
context "without arguments" do
setup do
@results = DefectCubicle.query
+ Time.now = "4/1/2010"
end
should "return a collection of appropriate aggregated values based on the cubicle parameters" do
puts @results.inspect
@@ -32,6 +33,7 @@ class CubicleAggregationTest < ActiveSupport::TestCase
assert_equal 1, @results[0]["distinct_products"]
assert_equal 1/1, @results[0]["distinct_ratio"]
assert_equal 1, @results[0]["inevitable_defects"]
+ assert_equal 0, @results[0]["defects_this_year"]
assert_equal "2010-01-01", @results[1]["manufacture_date"]
assert_equal "2010-01", @results[1]["month"]
@@ -50,6 +52,7 @@ class CubicleAggregationTest < ActiveSupport::TestCase
assert_equal 1, @results[1]["distinct_products"]
assert_equal 1.0/2.0, @results[1]["distinct_ratio"]
assert_equal 1, @results[1]["inevitable_defects"]
+ assert_equal 2, @results[1]["defects_this_year"]
assert_equal "2010-01-05", @results[2]["manufacture_date"]
assert_equal "2010-01", @results[2]["month"]
@@ -68,6 +71,7 @@ class CubicleAggregationTest < ActiveSupport::TestCase
assert_equal 1, @results[2]["distinct_products"]
assert_equal 1/1, @results[2]["distinct_ratio"]
assert_equal 0, @results[2]["inevitable_defects"]
+ assert_equal 1, @results[2]["defects_this_year"]
assert_equal "2010-02-01", @results[3]["manufacture_date"]
assert_equal "2010-02", @results[3]["month"]
@@ -86,6 +90,7 @@ class CubicleAggregationTest < ActiveSupport::TestCase
assert_equal 1, @results[3]["distinct_products"]
assert_equal 1/1, @results[3]["distinct_ratio"]
assert_equal 0, @results[3]["inevitable_defects"]
+ assert_equal 1, @results[3]["defects_this_year"]
end
end
View
26 test/cubicle/cubicle_query_test.rb
@@ -441,5 +441,31 @@ class CubicleQueryTest < ActiveSupport::TestCase
assert_equal 2, results[0]["distinct_products"]
end
end
+ context "Aggregation level 'define' calls" do
+ should "override the default of Time.now" do
+ results = DefectCubicle.query do
+ transient!
+ select :year, :defects_this_year
+ end
+ puts results.inspect
+ assert_equal 2, results.length
+ assert_equal 0, results[0].defects_this_year
+ assert_equal 4, results[1].defects_this_year
+ end
+ end
+ context "Query level 'define' calls" do
+ should "override the defaults" do
+ results = DefectCubicle.query do
+ transient!
+ define :time_now, "2009-01-01".to_time
+
+ select :year, :defects_this_year
+ end
+ puts results.inspect
+ assert_equal 2, results.length
+ assert_equal 1, results[0].defects_this_year
+ assert_equal 0, results[1].defects_this_year
+ end
+ end
end
end
View
2 test/cubicles/defect_cubicle.rb
@@ -3,6 +3,7 @@ class DefectCubicle
define :preventable, "this.root_cause != 'act_of_god'"
define :product_name, "this.product.name"
+ define :current_year, "'{{date_today_iso}}'.substring(0,4)"
date :manufacture_date, :field_name=>'manufacture_date', :alias=>:date
dimension :month, :expression=>'this.manufacture_date.substring(0,7)'
@@ -19,6 +20,7 @@ class DefectCubicle
count :distinct_products, :expression=>'{{product}}', :distinct=>true
count :preventable_defects, :expression=>'{{preventable}}'
count :conditioned_preventable,:expression=>'1.0', :condition=>'{{preventable}}'
+ count :defects_this_year, :expression=>'{{year}} == {{current_year}}'
sum :total_cost, :field_name=>'cost'
avg :avg_cost, :field_name=>'cost'

0 comments on commit 7ea7e04

Please sign in to comment.