diff --git a/app/controllers/embed_controller.rb b/app/controllers/embed_controller.rb index 263d2cfaf..af3ef591c 100644 --- a/app/controllers/embed_controller.rb +++ b/app/controllers/embed_controller.rb @@ -1,7 +1,6 @@ class EmbedController < ApplicationController skip_before_filter :check_xhr skip_before_filter :preload_json - skip_before_filter :store_incoming_links skip_before_filter :verify_authenticity_token before_filter :ensure_embeddable diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 1d42fc4b3..97cd4c9aa 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -7,7 +7,6 @@ class PostsController < ApplicationController # Need to be logged in for all actions here before_filter :ensure_logged_in, except: [:show, :replies, :by_number, :short_link, :reply_history, :revisions, :expand_embed, :markdown, :raw, :cooked] - skip_before_filter :store_incoming_links, only: [:short_link] skip_before_filter :check_xhr, only: [:markdown_id, :markdown_num, :short_link] def markdown_id diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index 68cbba1c9..f29c24b63 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -63,7 +63,7 @@ class StaticController < ApplicationController ) end - skip_before_filter :store_incoming_links, :verify_authenticity_token, only: [:cdn_asset] + skip_before_filter :verify_authenticity_token, only: [:cdn_asset] def cdn_asset path = File.expand_path(Rails.root + "public/assets/" + params[:path]) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 7a0917e6c..b380804d0 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -1,6 +1,6 @@ class UploadsController < ApplicationController before_filter :ensure_logged_in, except: [:show] - skip_before_filter :store_incoming_links, :check_xhr, only: [:show] + skip_before_filter :check_xhr, only: [:show] def create file = params[:file] || params[:files].first diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index ff9ba3a1f..f8befd482 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -3,7 +3,7 @@ require_dependency 'letter_avatar' class UserAvatarsController < ApplicationController DOT = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") - skip_before_filter :store_incoming_links, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter] + skip_before_filter :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :show_letter] def refresh_gravatar user = User.find_by(username_lower: params[:username].downcase) diff --git a/app/models/badge.rb b/app/models/badge.rb index 6587ed045..ff0205779 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -86,18 +86,17 @@ SQL SQL FirstShare = < ? AND incoming_links.user_id IS NOT NULL', 30.days.ago).joins(:user).group('users.username') + @per_user_query ||= IncomingLink + .where('incoming_links.created_at > ? AND incoming_links.user_id IS NOT NULL', 30.days.ago) + .joins(:user) + .joins(:post) + .group('users.username') end def self.link_count_per_user @@ -51,7 +55,7 @@ class IncomingLinksReport end def self.topic_count_per_user - per_user.count('incoming_links.topic_id', distinct: true) + per_user.count('topic_id', distinct: true) end @@ -71,11 +75,19 @@ class IncomingLinksReport end def self.link_count_per_domain(limit=10) - IncomingLink.where('created_at > ? AND domain IS NOT NULL', 30.days.ago).group('domain').order('count_all DESC').limit(limit).count + IncomingLink.where('incoming_links.created_at > ?', 30.days.ago) + .joins(:incoming_referer => :incoming_domain) + .group('incoming_domains.name') + .order('count_all DESC') + .limit(limit).count end def self.per_domain(domains) - IncomingLink.where('created_at > ? AND domain IN (?)', 30.days.ago, domains).group('domain') + IncomingLink + .joins(:incoming_referer => :incoming_domain) + .joins(:post) + .where('incoming_links.created_at > ? AND incoming_domains.name IN (?)', 30.days.ago, domains) + .group('incoming_domains.name') end def self.topic_count_per_domain(domains) @@ -100,6 +112,9 @@ class IncomingLinksReport end def self.link_count_per_topic - IncomingLink.where('created_at > ? AND topic_id IS NOT NULL', 30.days.ago).group('topic_id').count + IncomingLink.joins(:post) + .where('incoming_links.created_at > ? AND topic_id IS NOT NULL', 30.days.ago) + .group('topic_id') + .count end end diff --git a/app/models/incoming_referer.rb b/app/models/incoming_referer.rb index 3c50461e1..8d714b7ff 100644 --- a/app/models/incoming_referer.rb +++ b/app/models/incoming_referer.rb @@ -1,4 +1,22 @@ class IncomingReferer < ActiveRecord::Base + belongs_to :incoming_domain + + def self.add!(opts) + domain_id = opts[:incoming_domain_id] + domain_id ||= opts[:incoming_domain].id + path = opts[:path] + + current = find_by(path: path, incoming_domain_id: domain_id) + return current if current + + begin + current = create!(path: path, incoming_domain_id: domain_id) + rescue + # duplicates + end + + current || find_by(path: path, incoming_domain_id: domain_id) + end end # == Schema Information diff --git a/db/migrate/20140804030041_remove_url_from_incoming_referer.rb b/db/migrate/20140804030041_remove_url_from_incoming_referer.rb new file mode 100644 index 000000000..330619413 --- /dev/null +++ b/db/migrate/20140804030041_remove_url_from_incoming_referer.rb @@ -0,0 +1,9 @@ +class RemoveUrlFromIncomingReferer < ActiveRecord::Migration + def up + remove_column :incoming_referers, :url + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/migrate/20140804060439_drop_topic_id_from_incoming_links.rb b/db/migrate/20140804060439_drop_topic_id_from_incoming_links.rb new file mode 100644 index 000000000..305e93ea1 --- /dev/null +++ b/db/migrate/20140804060439_drop_topic_id_from_incoming_links.rb @@ -0,0 +1,5 @@ +class DropTopicIdFromIncomingLinks < ActiveRecord::Migration + def change + remove_column :incoming_links, :topic_id + end +end diff --git a/spec/models/incoming_link_spec.rb b/spec/models/incoming_link_spec.rb index 255eb5ebd..ea239c0e4 100644 --- a/spec/models/incoming_link_spec.rb +++ b/spec/models/incoming_link_spec.rb @@ -2,8 +2,6 @@ require 'spec_helper' describe IncomingLink do - it { should belong_to :topic } - let(:post) { Fabricate(:post) } let(:topic) { post.topic } diff --git a/spec/models/incoming_links_report_spec.rb b/spec/models/incoming_links_report_spec.rb index 6c5e7952f..98b0e1c21 100644 --- a/spec/models/incoming_links_report_spec.rb +++ b/spec/models/incoming_links_report_spec.rb @@ -2,6 +2,33 @@ require 'spec_helper' describe IncomingLinksReport do + describe 'integration' do + it 'runs correctly' do + p1 = create_post + + IncomingLink.add( + referer: 'http://test.com', + host: 'http://boo.com', + topic_id: p1.topic.id, + ip_address: '10.0.0.2', + username: p1.user.username + ) + + + c = IncomingLinksReport.link_count_per_topic + c[p1.topic_id].should == 1 + + c = IncomingLinksReport.link_count_per_domain + c["test.com"].should == 1 + + c = IncomingLinksReport.topic_count_per_domain(['test.com', 'foo.com']) + c["test.com"].should == 1 + + c = IncomingLinksReport.topic_count_per_user() + c[p1.username].should == 1 + end + end + describe 'top_referrers' do subject(:top_referrers) { IncomingLinksReport.find('top_referrers').as_json }