FIX: don't use PMs in Incoming Links reports

This commit is contained in:
Régis Hanol 2018-05-16 16:55:54 +02:00
parent 9554d9c56a
commit 4103783821
2 changed files with 56 additions and 7 deletions

View File

@ -49,7 +49,7 @@ class IncomingLinksReport
end
def self.per_user(start_date:)
@per_user_query ||= IncomingLink
@per_user_query ||= public_incoming_links
.where('incoming_links.created_at > ? AND incoming_links.user_id IS NOT NULL', start_date)
.joins(:user)
.group('users.username')
@ -79,15 +79,17 @@ class IncomingLinksReport
end
def self.link_count_per_domain(limit: 10, start_date:)
IncomingLink.where('incoming_links.created_at > ?', start_date)
public_incoming_links
.where('incoming_links.created_at > ?', start_date)
.joins(incoming_referer: :incoming_domain)
.group('incoming_domains.name')
.order('count_all DESC')
.limit(limit).count
.limit(limit)
.count
end
def self.per_domain(domains)
IncomingLink
public_incoming_links
.joins(incoming_referer: :incoming_domain)
.where('incoming_links.created_at > ? AND incoming_domains.name IN (?)', 30.days.ago, domains)
.group('incoming_domains.name')
@ -95,7 +97,7 @@ class IncomingLinksReport
def self.topic_count_per_domain(domains)
# COUNT(DISTINCT) is slow
per_domain(domains).joins(:post).count("DISTINCT posts.topic_id")
per_domain(domains).count("DISTINCT posts.topic_id")
end
def self.report_top_referred_topics(report)
@ -114,9 +116,15 @@ class IncomingLinksReport
end
def self.link_count_per_topic(start_date:)
IncomingLink.joins(:post)
public_incoming_links
.where('incoming_links.created_at > ? AND topic_id IS NOT NULL', start_date)
.group('topic_id')
.count
end
def self.public_incoming_links
IncomingLink
.joins(post: :topic)
.where("topics.archetype = ?", Archetype.default)
end
end

View File

@ -56,6 +56,45 @@ describe IncomingLinksReport do
{ topic_id: p2.topic.id, topic_title: p2.topic.title, topic_url: p2.topic.relative_url, num_clicks: 2 + 3 },
]
end
it "does not report PMs" do
public_topic = Fabricate(:topic)
message_topic = Fabricate(:private_message_topic)
public_post = Fabricate(:post, topic: public_topic)
message_post = Fabricate(:post, topic: message_topic)
IncomingLink.add(
referer: "http://foo.com",
host: "http://discourse.example.com",
topic_id: public_topic.id,
id_address: "1.2.3.4",
username: public_post.user.username,
)
IncomingLink.add(
referer: "http://foo.com",
host: "http://discourse.example.com",
topic_id: message_topic.id,
id_address: "5.6.7.8",
username: message_post.user.username,
)
r = IncomingLinksReport.find('top_referrers').as_json
expect(r[:data]).to eq [
{ username: public_post.user.username, user_id: public_post.user.id, num_clicks: 1, num_topics: 1 },
]
r = IncomingLinksReport.find('top_traffic_sources').as_json
expect(r[:data]).to eq [
{ domain: 'foo.com', num_clicks: 1, num_topics: 1 },
]
r = IncomingLinksReport.find('top_referred_topics').as_json
expect(r[:data]).to eq [
{ topic_id: public_topic.id, topic_title: public_topic.title, topic_url: public_topic.relative_url, num_clicks: 1 },
]
end
end
describe 'top_referrers' do
@ -161,7 +200,9 @@ describe IncomingLinksReport do
topic1 = Fabricate.build(:topic, id: 123); topic2 = Fabricate.build(:topic, id: 234)
# TODO: OMG OMG THE STUBBING
IncomingLinksReport.stubs(:link_count_per_topic).returns(topic1.id => 8, topic2.id => 3)
Topic.stubs(:select).returns(Topic); Topic.stubs(:where).returns(Topic) # bypass some activerecord methods
# bypass some activerecord methods
Topic.stubs(:select).returns(Topic)
Topic.stubs(:where).returns(Topic)
Topic.stubs(:all).returns([topic1, topic2])
expect(top_referred_topics[:data][0]).to eq(topic_id: topic1.id, topic_title: topic1.title, topic_url: topic1.relative_url, num_clicks: 8)
expect(top_referred_topics[:data][1]).to eq(topic_id: topic2.id, topic_title: topic2.title, topic_url: topic2.relative_url, num_clicks: 3)