module OrbitApp module Module module VisitorCounterEval Version = "0.1" def display_visitors(options={}) map = "function(){ emit( this.referrer,{session_hash: this.session_hash,created_at: this.created_at}); };" reduce = "function(key, values){ var sum = 0; values.forEach(function(doc){sum += doc.amount; }); return {amount: sum};};" if options.blank? impressions = Impression.collection.map_reduce(map, reduce,read: :primary, out: "vr") else query = Impression.where(options).selector impressions = Impression.where(options).collection.map_reduce(map, reduce,read: :primary, out: "vr",:query=>query) end impressions.count end def display_visitors_today display_visitors(created_at: {'$gte' => Date.today.beginning_of_day , '$lte' => DateTime.now}) end def display_visitors_this_week display_visitors(created_at: {'$gte' => Date.today.beginning_of_day - 7.days, '$lte' => DateTime.now}) end def display_visitors_this_month display_visitors(created_at: {'$gte' => Date.today.beginning_of_day - 1.month, '$lte' => DateTime.now}) end def display_visitors_this_year display_visitors(created_at: {'$gte' => Date.today.beginning_of_day - 1.year, '$lte' =>DateTime.now}) end end # end of VisitorCounterEval end # of Module end # of OrbitApp