author-stat-fix-6
All checks were successful
Deploy on push / deploy (push) Successful in 24s

This commit is contained in:
Untone 2024-03-28 23:33:56 +03:00
parent e9c852d23d
commit 8ff0e6786b

View File

@ -83,30 +83,28 @@ def add_author_stat_columns(q, with_rating=False):
func.count(distinct(aliased_followers.follower)).label('followers_stat') func.count(distinct(aliased_followers.follower)).label('followers_stat')
) )
if not with_rating: # Create a subquery for comments count
# Create a subquery for comments count select_list = [
select_list = [ Author.id,
Author.id, func.coalesce(func.count(Reaction.id)).label('comments_count'),
func.coalesce(func.count(Reaction.id)).label('comments_count'), ]
]
sub_comments = ( sub_comments = (
select(*select_list) select(*select_list)
.outerjoin( .outerjoin(
Reaction, Reaction,
and_( and_(
Reaction.created_by == Author.id, Reaction.created_by == Author.id,
Reaction.kind == ReactionKind.COMMENT.value, Reaction.kind == ReactionKind.COMMENT.value,
Reaction.deleted_at.is_(None), Reaction.deleted_at.is_(None),
), ),
)
.group_by(Author.id)
.subquery()
) )
.group_by(Author.id)
.subquery()
)
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id) q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
q = q.add_columns(sub_comments.c.comments_count) q = q.add_columns(sub_comments.c.comments_count)
q = q.group_by(Author.id, sub_comments.c.comments_count)
if with_rating: if with_rating:
# Create a subquery for ratings counters # Create a subquery for ratings counters
@ -146,6 +144,11 @@ def add_author_stat_columns(q, with_rating=False):
sub_rating.c.shouts_likes, sub_rating.c.shouts_likes,
sub_rating.c.shouts_dislikes, sub_rating.c.shouts_dislikes,
) )
else:
q = q.group_by(
Author.id,
sub_comments.c.comments_count
)
return q return q