app/models/cohortly/metric.rb in cohortly-0.0.7 vs app/models/cohortly/metric.rb in cohortly-0.0.8
- old
+ new
@@ -10,10 +10,11 @@
key :controller, String
key :action, String
timestamps!
ensure_index :tags
+ ensure_index :user_start_date
def self.store!(args)
data = args[4]
data[:tags] = []
if data[:controller]
@@ -32,17 +33,24 @@
query[:$where] = "function() { return #{ groups.collect {|x| 'this.tags.indexOf("' + x + '") >= 0' }.join(' || ') }; }"
end
collection_name = self.report_table_name(tags, groups, weekly)
# incremental map_reduce pattern
meta = Cohortly::ReportMeta.find_or_create_by_collection_name(collection_name)
- query[:created_at] = { :$gt => meta.last_update_on.utc } if meta.last_update_on
+
+ if meta.last_update_on
+ query[:created_at] = { :$gt => meta.last_update_on.utc }
+ out_collection = { :reduce => meta.store_name }
+ else
+ out_collection = meta.store_name
+ end
+
+ meta.last_update_on = Time.now.utc
+ meta.save
self.collection.map_reduce(weekly ? self.week_map : self.month_map,
self.reduce,
- { :out => meta.last_update_on ? { :reduce => collection_name } : collection_name,
+ { :out => out_collection,
:raw => true,
:query => query})
- meta.last_update_on = Time.now.utc
- meta.save
end
def self.cohort_chart_for_tag(tags = nil, groups = nil)
self.cohort_chart(tags, groups, false)
end