FIX user directory time period count (#6586)

This commit is contained in:
Maja Komel 2018-11-12 15:30:05 +01:00 committed by Régis Hanol
parent 599ab4e966
commit 012da86a07
2 changed files with 35 additions and 4 deletions

View File

@ -78,13 +78,13 @@ class DirectoryItem < ActiveRecord::Base
u.id user_id,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :was_liked_type THEN 1 ELSE 0 END) likes_received,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :like_type THEN 1 ELSE 0 END) likes_given,
COALESCE((SELECT COUNT(topic_id) FROM topic_views AS v WHERE v.user_id = u.id AND v.viewed_at >= :since), 0) topics_entered,
COALESCE((SELECT COUNT(id) FROM user_visits AS uv WHERE uv.user_id = u.id AND uv.visited_at >= :since), 0) days_visited,
COALESCE((SELECT SUM(posts_read) FROM user_visits AS uv2 WHERE uv2.user_id = u.id AND uv2.visited_at >= :since), 0) posts_read,
COALESCE((SELECT COUNT(topic_id) FROM topic_views AS v WHERE v.user_id = u.id AND v.viewed_at > :since), 0) topics_entered,
COALESCE((SELECT COUNT(id) FROM user_visits AS uv WHERE uv.user_id = u.id AND uv.visited_at > :since), 0) days_visited,
COALESCE((SELECT SUM(posts_read) FROM user_visits AS uv2 WHERE uv2.user_id = u.id AND uv2.visited_at > :since), 0) posts_read,
SUM(CASE WHEN t2.id IS NOT NULL AND ua.action_type = :new_topic_type THEN 1 ELSE 0 END) topic_count,
SUM(CASE WHEN p.id IS NOT NULL AND t.id IS NOT NULL AND ua.action_type = :reply_type THEN 1 ELSE 0 END) post_count
FROM users AS u
LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND COALESCE(ua.created_at, :since) >= :since
LEFT OUTER JOIN user_actions AS ua ON ua.user_id = u.id AND COALESCE(ua.created_at, :since) > :since
LEFT OUTER JOIN posts AS p ON ua.target_post_id = p.id AND p.deleted_at IS NULL AND p.post_type = :regular_post_type AND NOT p.hidden
LEFT OUTER JOIN topics AS t ON p.topic_id = t.id AND t.archetype = 'regular' AND t.deleted_at IS NULL AND t.visible
LEFT OUTER JOIN topics AS t2 ON t2.id = ua.target_topic_id AND t2.archetype = 'regular' AND t2.deleted_at IS NULL AND t2.visible

View File

@ -110,5 +110,36 @@ describe DirectoryItem do
expect(directory_item.topic_count).to eq(1)
end
it "creates directory item with correct activity count per period_type" do
user = Fabricate(:user)
UserVisit.create(user_id: user.id, visited_at: 1.minute.ago, posts_read: 1, mobile: false, time_read: 1)
UserVisit.create(user_id: user.id, visited_at: 2.days.ago, posts_read: 1, mobile: false, time_read: 1)
UserVisit.create(user_id: user.id, visited_at: 1.week.ago, posts_read: 1, mobile: false, time_read: 1)
UserVisit.create(user_id: user.id, visited_at: 1.month.ago, posts_read: 1, mobile: false, time_read: 1)
DirectoryItem.refresh!
daily_directory_item = DirectoryItem
.where(period_type: DirectoryItem.period_types[:daily])
.where(user_id: user.id)
.first
expect(daily_directory_item.days_visited).to eq(1)
weekly_directory_item = DirectoryItem
.where(period_type: DirectoryItem.period_types[:weekly])
.where(user_id: user.id)
.first
expect(weekly_directory_item.days_visited).to eq(2)
monthly_directory_item = DirectoryItem
.where(period_type: DirectoryItem.period_types[:monthly])
.where(user_id: user.id)
.first
expect(monthly_directory_item.days_visited).to eq(3)
end
end
end