A platform for community discussion. Free, open, simple.
FEATURE: add a rake task to recalculate user stats post_count and topic_count
Neil Lalonde92000bc8a035
diff --git a/lib/tasks/import.rake b/lib/tasks/import.rakeindex 7d83eb0115c..5ce4875509c 100644--- a/lib/tasks/import.rake+++ b/lib/tasks/import.rake@@ -226,6 +226,9 @@ enddef update_user_statslog "Updating user stats..."+ # TODO: topic_count is counting all topics you replied in as if you started the topic.+ # TODO: post_count is counting first posts.+ # TODO: topic_reply_count is never used, and is counting PMs here.DB.exec <<-SQLWITH X AS (SELECT p.user_iddiff --git a/lib/tasks/users.rake b/lib/tasks/users.rakeindex da28fc6b5e3..b3abf85d78a 100644--- a/lib/tasks/users.rake+++ b/lib/tasks/users.rake@@ -80,6 +80,61 @@ task "users:update_posts", [:old_username, :current_username] => [:environment]puts "", "Username updated!", ""end+desc 'Recalculate post and topic counts in user stats'+task 'users:recalculate_post_counts' => :environment do+ puts '', 'Updating user stats...'++ filter_public_posts_and_topics = <<~SQL+ p.deleted_at IS NULL+ AND NOT COALESCE(p.hidden, 't')+ AND p.post_type = 1+ AND t.deleted_at IS NULL+ AND COALESCE(t.visible, 't')+ AND t.archetype <> 'private_message'+ AND p.user_id > 0+ SQL++ puts 'post counts...'++ # all public replies+ DB.exec <<~SQL+ WITH X AS (+ SELECT p.user_id, COUNT(p.id) post_count+ FROM posts p+ JOIN topics t ON t.id = p.topic_id+ WHERE #{filter_public_posts_and_topics}+ AND p.post_number > 1+GROUP BY p.user_id+ )+ UPDATE user_stats+ SET post_count = X.post_count+ FROM X+ WHERE user_stats.user_id = X.user_id+ AND user_stats.post_count <> X.post_count+ SQL++ puts 'topic counts...'++ # public topics+ DB.exec <<~SQL+ WITH X AS (+ SELECT p.user_id, COUNT(p.id) topic_count+ FROM posts p+ JOIN topics t ON t.id = p.topic_id+ WHERE #{filter_public_posts_and_topics}+ AND p.post_number = 1+GROUP BY p.user_id+ )+ UPDATE user_stats+ SET topic_count = X.topic_count+ FROM X+ WHERE user_stats.user_id = X.user_id+ AND user_stats.topic_count <> X.topic_count+ SQL++ puts 'Done!', ''+end+def find_user(username)user = User.find_by_username(username)