Class: Gricer::Mongoid::Session

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Session, ActiveModel::Statistics, CounterCache, MapReduce, Touch, Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/gricer/mongoid/session.rb

Class Method Summary collapse

Methods included from ActiveModel::Statistics

included, #model_type

Methods included from ActiveModel::Session

#duration, #flash_version=, included, #ip_address=, #requested_locale, #requested_locale=, #silverlight_version=

Methods included from MapReduce

included

Methods included from Touch

#touch, #touch!

Methods included from CounterCache

included

Class Method Details

.avg_durationFloat

Get the average duration of sessions in seconds.

Returns:

  • (Float)


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

.browsersMongoid::Criteria

Filter out anything that is not a Browser or MobileBrowser

Returns:

  • (Mongoid::Criteria)


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