Permalink
Browse files

DB Schema changed. Actions and Visitors have now a N-to-N relation. V…

…isit table created. This way it is possible to ask how many visitors visited a certain page
  • Loading branch information...
1 parent d1a6e8c commit 7f77df3101fb2d5f3cb4002ad4569aa3e9459d16 @rosario committed Mar 27, 2009
@@ -62,9 +62,11 @@ def graph1
- @time_spent = vs.group_by {|v|v.time_spent_in_words }.map{|k,v| [k,v.size]}
+ @time_spent = vs.group_by {|v|v.time_spent_in_words }.map{|k,v| [k,v.size]}.sort_by{|t,s| s}.reverse
+ as = p.actions
+ @actions = as.map{|a| [a.path,a.visits.size]}.sort_by{|u,s| s}.reverse
end
@@ -67,6 +67,8 @@ def log
action = Action.create(:url=>"#{params[:url]}", :url_id=> Digest::MD5.hexdigest(params[:url]),
:kind => params[:action_kind].to_i)
v.add_action(action)
+ # Add the action to the project, only if it's new
+ p.add_action(action)
end
@@ -31,12 +31,26 @@ def create
u.project_id = p.id
u.save
+ # Testing, add the actions
+ urls = ["http://0.0.0.0:3000/website/page1",
+ "http://0.0.0.0:3000/website/page2",
+ "http://0.0.0.0:3000/website/page3",
+ "http://0.0.0.0:3000/website/page4",
+ "http://0.0.0.0:3000/website/page5"]
+ as = []
+ for u in urls
+ p.add_action(Action.new(:url=>"#{u}", :url_id=> Digest::MD5.hexdigest(u), :kind => rand(2)))
+ end
+
+
# Randomly create 500 visitors. Useful for graphs generation
500.times do
v = Visitor.create_random
- v.add_random_actions
+ v.add_random_actions(p.actions)
p.visitors << v
end
+
+
else
p= Project.find(u.project_id)
View
@@ -1,6 +1,10 @@
# An Action represent the page the visitor requested
class Action < ActiveRecord::Base
- belongs_to :visitor
+ # belongs_to :visitor
+ belongs_to :project
+ has_many :visits
+ has_many :visitors, :through => :visits
+
require 'uri'
# Non uso piu URI.split ma URI.parse
@@ -43,8 +47,12 @@ def self.new_random
url = urls[rand(urls.size)]
+ a = Action.find_by_url(url)
+ if a.nil?
+ a = Action.new(:url=>"#{url}", :url_id=> Digest::MD5.hexdigest(url), :kind => rand(2))
+ end
- Action.new(:url=>"#{url}", :url_id=> Digest::MD5.hexdigest(url), :kind => rand(2))
+ return a
end
View
@@ -2,7 +2,16 @@
class Project < ActiveRecord::Base
has_many :users
has_many :visitors
+ has_many :actions
+ # Add an action to the project, only if it's new
+ def add_action(action)
+ a = self.actions.find_by_url(action.url)
+ if a.nil?
+ actions << action
+ end
+
+ end
# Get the visitors between two dates
def visitors_between(date_begin, date_end)
View
@@ -0,0 +1,4 @@
+class Visit < ActiveRecord::Base
+ belongs_to :visitor
+ belongs_to :action
+end
View
@@ -1,6 +1,10 @@
# Visitor handle the visits
class Visitor < ActiveRecord::Base
- has_many :actions
+ has_many :visits
+ has_many :actions, :through => :visits
+
+ belongs_to :project
+
include Parser
@@ -1406,10 +1410,12 @@ def add_action(a)
actions << a
if self.first_action_time.nil?
- self.first_action_time = a.created_at
+ # It's important the time of the visit, not the time when the action was created
+ self.first_action_time = a.visits.last.created_at # not this -> a.created_at
end
- self.last_action_time = a.created_at
+ # It's important the time of the visit, not the time when the action was created
+ self.last_action_time = a.visits.last.created_at # not this -> a.created_at
self.time_spent = self.last_action_time - self.first_action_time
self.save
end
@@ -1500,24 +1506,46 @@ def self.create_with_settings(user_settings)
# Add random actions to the visitor. For testing purpose
- def add_random_actions
+ def add_random_actions(actions)
+
+ # Need to add few random actions
+ # All the visits will start when the visitor is created
time = created_at
- nactions = 1 + rand(4)
-
- as = []
+
+ # The number of actions is random
+ nactions = 1 + rand(actions.size)
+
+ # The time of the visit is also random,
+ times = []
nactions.times do
- a = Action.new_random
- a.created_at = time + rand(100).seconds
- as << a
+ times << time + rand(100).seconds
end
- #Actions are sorted
- sorted_actions = as.sort_by { |a| a.created_at}
+ # Sort the time of each visit
+ times.sort!
- for a in sorted_actions
- add_action(a)
- end
+ i = 0
+ as = []
+ nactions.times do
+ # Add a random action
+# a = Action.new_random
+ self.add_action(actions[rand(nactions)])
+
+ # Since the action is added "now", it will have the wrong creation time...
+ # We are supposing that the visitor was also created with a random time,
+ # so all the his visits will be after his "creation time"
+ # Now let's change the time of this visit
+ visit = visits.last
+ visit.created_at = times[i] # Change the time of the visit according to times
+ visit.save
+ i = i + 1
+ end
+ #Calculate the time spent on the website
+ self.first_action_time = visits.first.created_at
+ self.last_action_time = visits.last.created_at
+ self.time_spent = self.last_action_time - self.first_action_time
+ self.save
end
@@ -40,6 +40,11 @@
:placeholder => "placeholder6",
:legend => "Time Spent on Site"}%>
+<%= render :partial => "bar_graph",
+ :locals => {:data =>@actions,
+ :placeholder => "placeholder8",
+ :legend => "Number of visitors per page"}%>
+
<%# Example. The data is plotted, with no "time" formatting %>
<%= render :partial => "line_plot" ,
:locals => {:data => @example,
@@ -0,0 +1,14 @@
+class CreateVisits < ActiveRecord::Migration
+ def self.up
+ create_table :visits do |t|
+ t.integer :visitor_id
+ t.integer :action_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :visits
+ end
+end
@@ -0,0 +1,9 @@
+class RemoveVisitorIdFromAction < ActiveRecord::Migration
+ def self.up
+ remove_column :actions, :visitor_id
+ end
+
+ def self.down
+ add_column :actions, :visitor_id, :integer
+ end
+end
@@ -0,0 +1,9 @@
+class AddProjectIdToAction < ActiveRecord::Migration
+ def self.up
+ add_column :actions, :project_id, :integer
+ end
+
+ def self.down
+ remove_column :actions, :project_id
+ end
+end
View
@@ -9,15 +9,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090325151531) do
+ActiveRecord::Schema.define(:version => 20090327123548) do
create_table "actions", :force => true do |t|
t.string "url"
- t.integer "visitor_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "url_id"
t.integer "kind"
+ t.integer "project_id"
end
create_table "projects", :force => true do |t|
@@ -33,8 +33,8 @@
t.datetime "updated_at"
end
- add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
+ add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
create_table "users", :force => true do |t|
t.string "name"
@@ -78,4 +78,11 @@
t.integer "was_here"
end
+ create_table "visits", :force => true do |t|
+ t.integer "visitor_id"
+ t.integer "action_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
end
View
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ visitor_id:
+ action_id: 1
+
+two:
+ visitor_id:
+ action_id: 1
View
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class VisitTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit 7f77df3

Please sign in to comment.