Orbit/lib/orbit_app/module/visitor_counter_eval.rb

37 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

module OrbitApp
module Module
module VisitorCounterEval
Version = "0.1"
def display_visitors(options={})
2013-08-27 05:48:52 +00:00
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?
2013-08-27 08:07:58 +00:00
impressions = Impression.collection.map_reduce(map, reduce,read: :primary, out: "vr")
2013-08-27 05:48:52 +00:00
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
2013-08-27 05:48:52 +00:00
display_visitors(created_at: {'$gte' => Date.today.beginning_of_day , '$lte' => DateTime.now})
end
def display_visitors_this_week
2013-08-27 05:48:52 +00:00
display_visitors(created_at: {'$gte' => Date.today.beginning_of_day - 7.days, '$lte' => DateTime.now})
end
def display_visitors_this_month
2013-08-27 05:48:52 +00:00
display_visitors(created_at: {'$gte' => Date.today.beginning_of_day - 1.month, '$lte' => DateTime.now})
end
def display_visitors_this_year
2013-08-27 05:48:52 +00:00
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
2013-08-27 05:48:52 +00:00