Commit 4668ea6b authored by azul's avatar azul
Browse files

namespace all the tracking things

Tracking now is Tracking::Page.
parent acae4e8c
......@@ -5,13 +5,12 @@ module Common::Application::Tracking
# controllers should call this when they want to record a trackingevent.
# e.g. in order to update the page view count.
def track(options={})
return unless current_site.tracking
options.reverse_merge! current_user: current_user,
group: @group,
user: @user,
action: :view
if current_site.tracking
Tracking.insert_delayed options
end
Tracking::Page.insert_delayed options
end
end
......@@ -19,9 +19,9 @@ class CronController < ActionController::Base
when 'notices_send_digests'
PageHistory.send_digest_pending_notifications
when 'tracking_update_hourlies'
Tracking.process
Tracking::Page.process
when 'tracking_update_dailies'
Daily.update
Tracking::Daily.update
when 'cache_session_clean'
clean_session_cache
when 'cache_fragment_clean'
......
......@@ -113,7 +113,7 @@ module Pages::BeforeFilters
group = current_site.tracking? && @group
group ||= current_site.tracking? && @page.owner.is_a?(Group) && @page.owner
user = current_site.tracking? && @page.owner.is_a?(User) && @page.owner
Tracking.insert_delayed(
Tracking::Page.insert_delayed(
page: @page, current_user: current_user, action: action,
group: group, user: user
)
......
......@@ -2,8 +2,8 @@ module PageExtension::Tracking
def self.included(base)
base.send(:include, InstanceMethods)
base.instance_eval do
has_many :dailies
has_many :hourlies
has_many :dailies, class_name: 'Tracking::Daily'
has_many :hourlies, class_name: 'Tracking::Hourly'
end
end
......
#
# Module for all things related to tracking user activity
#
module Tracking
end
# lots of MySQL-specific stuff here.
class Daily < ActiveRecord::Base
# mainly MySQL-specific stuff here.
class Tracking::Daily < ActiveRecord::Base
belongs_to :page
def self.update
......
......@@ -2,6 +2,6 @@
# They are filled from trackings table every hour.
# dailies fetch their data from here every day.
#
class Hourly < ActiveRecord::Base
class Tracking::Hourly < ActiveRecord::Base
belongs_to :page
end
# um, so, yeah, basically we don't use ActiveRecord for this.
# lots of MySQL-specific stuff here.
class Tracking < ActiveRecord::Base
class Tracking::Page < ActiveRecord::Base
self.table_name = 'trackings'
#belongs_to :page
#belongs_to :group
#belongs_to :user
......@@ -28,7 +30,7 @@ class Tracking < ActiveRecord::Base
##
def self.process
return if (Tracking.count == 0)
return if (count == 0)
unprocessed_since = last_processed_at
......@@ -173,7 +175,7 @@ class Tracking < ActiveRecord::Base
end
def self.last_processed_at
Tracking.order(:tracked_at).first.try.tracked_at || Time.now - 3.month
order(:tracked_at).first.try.tracked_at || Time.now - 3.month
end
end
......@@ -23,7 +23,7 @@ module Crabgrass
config.autoload_paths << "#{Rails.root}/lib"
config.autoload_paths << "#{Rails.root}/app/models"
config.autoload_paths += %w(activity assets associations discussion chat profile poll task tracking requests mailers notice).
config.autoload_paths += %w(activity assets associations discussion chat profile poll task requests mailers notice).
collect { |dir| "#{Rails.root}/app/models/#{dir}" }
config.autoload_paths << "#{Rails.root}/app/permissions"
config.autoload_paths << "#{Rails.root}/app/sweepers"
......
......@@ -18,7 +18,7 @@ class PaginationTest < ActiveSupport::TestCase
# last page gets only 1 view
# lets us test that pagination sorts them properly
(all_pages.size - index).times do
Tracking.insert_delayed(current_user: user,
Tracking::Page.insert_delayed(current_user: user,
user: user,
group: group,
page: page,
......@@ -27,8 +27,8 @@ class PaginationTest < ActiveSupport::TestCase
end
end
Tracking.process
Daily.update
Tracking::Page.process
Tracking::Daily.update
# pagination group options
paginate_options = {
......
require_relative 'test_helper'
require 'test_helper'
class TrackingTest < ActiveSupport::TestCase
class Tracking::PageTest < ActiveSupport::TestCase
# otherwise transactions fail because UNLOCK TABLES implicitly commits the transaction
self.use_transactional_fixtures = false
......@@ -14,12 +14,12 @@ class TrackingTest < ActiveSupport::TestCase
group = groups(:rainbow)
assert membership = user.memberships.find_by_group_id(group.id)
assert_difference('Membership.find(%d).total_visits'%membership.id) do
Tracking.insert_delayed(current_user: user, group: group)
Tracking.process
Tracking::Page.insert_delayed(current_user: user, group: group)
Tracking::Page.process
end
assert_difference('Membership.find(%d).total_visits'%membership.id) do
Tracking.insert_delayed(current_user: user.id, group: group.id)
Tracking.process
Tracking::Page.insert_delayed(current_user: user.id, group: group.id)
Tracking::Page.process
end
end
......@@ -27,11 +27,11 @@ class TrackingTest < ActiveSupport::TestCase
current_user = users(:blue)
user = users(:orange)
assert_difference 'Tracking.count', 3 do
3.times { Tracking.insert_delayed(current_user: current_user, user: user) }
assert_difference 'Tracking::Page.count', 3 do
3.times { Tracking::Page.insert_delayed(current_user: current_user, user: user) }
end
assert_difference 'Tracking.count', -3 do
Tracking.process
assert_difference 'Tracking::Page.count', -3 do
Tracking::Page.process
end
assert_equal 3, current_user.relationships.with(user).total_visits
end
......@@ -42,17 +42,17 @@ class TrackingTest < ActiveSupport::TestCase
group = groups(:rainbow)
action = :view
# let's clean things up first so they do not get in the way...
Tracking.process
Daily.update
Hourly.find(:all).each{|h| h.destroy}
assert_no_difference 'Daily.count' do
Tracking::Page.process
Tracking::Daily.update
Tracking::Hourly.find(:all).each{|h| h.destroy}
assert_no_difference 'Tracking::Daily.count' do
# daily should not be created for the new hourlies
# we only create them with one day delay to avoid double counting.
assert_difference 'Hourly.count' do
assert_difference 'Tracking::Hourly.count' do
# 1, "hourly should be created for the tracked view" do
assert_tracking(user, group, page, action)
Tracking.process
Daily.update
Tracking::Page.process
Tracking::Daily.update
end
end
# we create trackings for the day before yesterday here
......@@ -60,13 +60,13 @@ class TrackingTest < ActiveSupport::TestCase
# And we add another day for caution because Hourlies store the timestamp at the end
# of the hour they were tracked in. So this will be on the next day in the hour
# before midnight.
assert_difference 'Daily.count' do
# Hourly should be created for the tracked view
assert_difference 'Tracking::Daily.count' do
# Tracking::Hourly should be created for the tracked view
# but then removed after being processed for daily.
assert_no_difference 'Hourly.count' do
assert_no_difference 'Tracking::Hourly.count' do
assert_tracking(user, group, page, action, Time.now - 3.days)
Tracking.process
Daily.update
Tracking::Page.process
Tracking::Daily.update
end
end
end
......@@ -76,10 +76,10 @@ class TrackingTest < ActiveSupport::TestCase
group1 = groups(:rainbow)
group2 = groups(:animals)
group3 = groups(:recent_group)
1.times { Tracking.insert_delayed(current_user: user, group: group3) }
2.times { Tracking.insert_delayed(current_user: user, group: group2) }
3.times { Tracking.insert_delayed(current_user: user, group: group1) }
Tracking.process
1.times { Tracking::Page.insert_delayed(current_user: user, group: group3) }
2.times { Tracking::Page.insert_delayed(current_user: user, group: group2) }
3.times { Tracking::Page.insert_delayed(current_user: user, group: group1) }
Tracking::Page.process
assert_equal [group1, group2, group3], user.primary_groups.most_active[0..2]
end
......@@ -87,19 +87,19 @@ class TrackingTest < ActiveSupport::TestCase
# Insert delayed is not delaysed for testing so this should not cause problems.
def assert_tracking(user, group, page, action, time=nil)
Tracking.insert_delayed(current_user: user, group: group, page: page, action: action, time: time)
track=Tracking.last
Tracking::Page.insert_delayed(current_user: user, group: group, page: page, action: action, time: time)
track=Tracking::Page.last
assert_equal track.current_user_id, user.id, "User not stored correctly in Tracking"
assert_equal track.group_id, group.id, "Group not stored correctly in Tracking"
assert_equal track.page_id, page.id, "Page not stored correctly in Tracking"
if action != :unstar
assert_equal "#{action}s", ["views", "edits", "stars"].find{|a| Tracking.last.send a},
assert_equal "#{action}s", ["views", "edits", "stars"].find{|a| Tracking::Page.last.send a},
'Tracking did not count the right action.'
assert_equal 1, ["views", "edits", "stars"].select{|a| Tracking.last.send a}.size,
assert_equal 1, ["views", "edits", "stars"].select{|a| Tracking::Page.last.send a}.size,
'There shall be exactly one action counted.'
else
# TODO: check this before ActiveRecord gets in the way.
assert_equal 0, ["views", "edits", "stars"].select{|a| Tracking.last.send a}.size,
assert_equal 0, ["views", "edits", "stars"].select{|a| Tracking::Page.last.send a}.size,
'For :unstar all values should evaluate to false.'
end
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment