FIX: correctly count participants when more than 24
Also cuts out one query for the normal case
This commit is contained in:
parent
b998efdc94
commit
9d925f6b26
|
@ -279,7 +279,7 @@ class TopicViewSerializer < ApplicationSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def participant_count
|
def participant_count
|
||||||
object.participants.size
|
object.participant_count
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -275,9 +275,11 @@ class TopicView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
MAX_PARTICIPANTS = 24
|
||||||
|
|
||||||
def post_counts_by_user
|
def post_counts_by_user
|
||||||
@post_counts_by_user ||= begin
|
@post_counts_by_user ||= begin
|
||||||
post_ids = unfiltered_posts.pluck(:id)
|
post_ids = unfiltered_post_ids
|
||||||
|
|
||||||
return {} if post_ids.blank?
|
return {} if post_ids.blank?
|
||||||
|
|
||||||
|
@ -288,13 +290,30 @@ class TopicView
|
||||||
AND user_id IS NOT NULL
|
AND user_id IS NOT NULL
|
||||||
GROUP BY user_id
|
GROUP BY user_id
|
||||||
ORDER BY count_all DESC
|
ORDER BY count_all DESC
|
||||||
LIMIT 24
|
LIMIT #{MAX_PARTICIPANTS}
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
Hash[Post.exec_sql(sql, post_ids: post_ids).values]
|
Hash[Post.exec_sql(sql, post_ids: post_ids).values]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def participant_count
|
||||||
|
@participant_count ||=
|
||||||
|
begin
|
||||||
|
if participants.size == MAX_PARTICIPANTS
|
||||||
|
sql = <<~SQL
|
||||||
|
SELECT COUNT(DISTINCT user_id)
|
||||||
|
FROM posts
|
||||||
|
WHERE id IN (:post_ids)
|
||||||
|
AND user_id IS NOT NULL
|
||||||
|
SQL
|
||||||
|
Post.exec_sql(sql, post_ids: unfiltered_post_ids).getvalue(0, 0).to_i
|
||||||
|
else
|
||||||
|
participants.size
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def participants
|
def participants
|
||||||
@participants ||= begin
|
@participants ||= begin
|
||||||
participants = {}
|
participants = {}
|
||||||
|
@ -354,6 +373,17 @@ class TopicView
|
||||||
@filtered_post_ids ||= filtered_post_stream.map { |tuple| tuple[0] }
|
@filtered_post_ids ||= filtered_post_stream.map { |tuple| tuple[0] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unfiltered_post_ids
|
||||||
|
@unfiltered_post_ids ||=
|
||||||
|
begin
|
||||||
|
if @contains_gaps
|
||||||
|
unfiltered_post.pluck(:id)
|
||||||
|
else
|
||||||
|
filtered_post_ids
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def read_posts_set
|
def read_posts_set
|
||||||
|
|
Loading…
Reference in New Issue