Class: Gricer::Mongoid::Session
- Inherits:
-
Object
- Object
- Gricer::Mongoid::Session
- Includes:
- ActiveModel::Session, ActiveModel::Statistics, CounterCache, MapReduce, Touch, Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/gricer/mongoid/session.rb
Class Method Summary collapse
-
.avg_duration ⇒ Float
Get the average duration of sessions in seconds.
-
.browsers ⇒ Mongoid::Criteria
Filter out anything that is not a Browser or MobileBrowser.
- .first_by_id(id) ⇒ Object
Methods included from ActiveModel::Statistics
Methods included from ActiveModel::Session
#duration, #flash_version=, included, #ip_address=, #requested_locale, #requested_locale=, #silverlight_version=
Methods included from MapReduce
Methods included from Touch
Methods included from CounterCache
Class Method Details
.avg_duration ⇒ Float
Get the average duration of sessions in seconds.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/gricer/mongoid/session.rb', line 55 def self.avg_duration map = "function() { emit(this.id, new Date(this.updated_at) - new Date(this.created_at)) }" reduce = "function (_, xs) { var x = {sum: 0, count: 0}; for (var i=0; i<xs.length; i++) { x.sum += xs[i]; x.count ++ } return x }" finalize = "function (_, x) {return x.sum / x.count }" result = map_reduce map, reduce, finalize: finalize return 0.0 unless result["results"][0] duration = result["results"][0]["value"] / 1000 duration.nan? ? 0.0 : duration end |
.browsers ⇒ Mongoid::Criteria
Filter out anything that is not a Browser or MobileBrowser
44 45 46 |
# File 'app/models/gricer/mongoid/session.rb', line 44 def self.browsers any_in agent_id: Gricer::Mongoid::Agent.browsers.only(:id).map{|x| x.id} end |
.first_by_id(id) ⇒ Object
48 49 50 |
# File 'app/models/gricer/mongoid/session.rb', line 48 def self.first_by_id(id) where('_id' => id).first end |