mirror of
https://github.com/discourse/discourse.git
synced 2025-03-24 02:05:33 +08:00
FEATURE: add a rake task to recalculate user stats post_count and topic_count
This commit is contained in:
parent
28dd7fb562
commit
92000bc8a0
@ -226,6 +226,9 @@ end
|
|||||||
def update_user_stats
|
def update_user_stats
|
||||||
log "Updating user stats..."
|
log "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 <<-SQL
|
DB.exec <<-SQL
|
||||||
WITH X AS (
|
WITH X AS (
|
||||||
SELECT p.user_id
|
SELECT p.user_id
|
||||||
|
@ -80,6 +80,61 @@ task "users:update_posts", [:old_username, :current_username] => [:environment]
|
|||||||
puts "", "Username updated!", ""
|
puts "", "Username updated!", ""
|
||||||
end
|
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)
|
def find_user(username)
|
||||||
user = User.find_by_username(username)
|
user = User.find_by_username(username)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user