Class: IWonder::Event

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/i_wonder/event.rb

Class Method Summary collapse

Class Method Details

.fast_create(attr_hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/i_wonder/event.rb', line 32

def fast_create(attr_hash)
  attr_hash[:created_at] = Time.zone.now.utc
  attr_hash[:updated_at] = Time.zone.now.utc
  
  keys = [:event_type, :account_id, :user_id, :session_id, :controller, :action, :extra_details, :remote_ip, :user_agent, :referrer, :created_at, :updated_at]
  key_str = keys.collect(&:to_s).join(", ")
  
  pre_value = keys.collect{ "?" }.join(", ")
  values_array = keys.collect{|k|
    v = attr_hash[k] 
    if v.is_a?(Hash)
      v = v.delete_blank.to_yaml
    elsif v.is_a?(String)
      v = v.truncate(200)
    end
    v
  }
  value_str = sanitize_sql_array( [pre_value] +  values_array) 
  
  Event.connection.execute "INSERT INTO #{table_name} (#{key_str}) values (#{value_str})"
end

.get_details_for_event_type(type) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'app/models/i_wonder/event.rb', line 66

def get_details_for_event_type(type)
  Event.where(:event_type => type).select("COUNT(event_type) as count, MAX(created_at) as most_recent").group("event_type").collect{|e|
    {
      :event_type => type,
      :count => e["count"].to_i,
      :most_recent => Time.parse(e["most_recent"])
    }
  }.first
end

.groupsObject

returns an array of hashes. Each one has a :count, :most_recent and is sorted by decending most_recent



55
56
57
58
59
60
61
62
63
# File 'app/models/i_wonder/event.rb', line 55

def groups
  Event.select("event_type, COUNT(event_type) as count, MAX(created_at) as most_recent").group("event_type").order("most_recent DESC").collect{|e|
    {
      :event_type => e["event_type"],
      :count => e["count"].to_i,
      :most_recent => Time.parse(e["most_recent"])
    }
  }
end

.merge_session_to_user(session_id, user_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/i_wonder/event.rb', line 9

def merge_session_to_user(session_id, user_id)
  # grab the session_id off of the new_visitor attached to that user
  new_visitor_event = Event.where(:user_id => user_id, :event_type => "new_visitor").first
  original_session_id = (new_visitor_event ? new_visitor_event.session_id : session_id)
  

  # for all events on the current session, attach the user and session from that first event
  update_all({:user_id => user_id, :session_id => original_session_id}, ["session_id = ? AND user_id IS NULL", session_id])
  
  # Change the new_visitor to a return_visit
  if new_visitor_event and original_session_id != session_id
    update_all({:event_type => "return_visit"}, ["user_id = ? AND event_type = ? AND id <> ?", user_id, "new_visitor", new_visitor_event.id])
  end
  
  
  # TODO: merge any tests on the session id over to the original session_id
  # remove any duplicates (going with the original)
  
  return original_session_id
end