author-stat-fix-8
This commit is contained in:
parent
f8f3a32556
commit
4b5c101f2f
|
@ -84,13 +84,11 @@ def add_author_stat_columns(q, with_rating=False):
|
|||
)
|
||||
|
||||
# Create a subquery for comments count
|
||||
select_list = [
|
||||
Author.id,
|
||||
func.coalesce(func.count(Reaction.id)).label('comments_count'),
|
||||
]
|
||||
|
||||
sub_comments = (
|
||||
select(*select_list)
|
||||
select(
|
||||
Author.id,
|
||||
func.coalesce(func.count(Reaction.id)).label('comments_count')
|
||||
)
|
||||
.outerjoin(
|
||||
Reaction,
|
||||
and_(
|
||||
|
@ -106,50 +104,33 @@ def add_author_stat_columns(q, with_rating=False):
|
|||
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
|
||||
q = q.add_columns(sub_comments.c.comments_count)
|
||||
|
||||
if with_rating:
|
||||
# Create a subquery for ratings counters
|
||||
select_list = [
|
||||
Author.id,
|
||||
func.sum(case((AuthorRating.plus == true(), 1), else_=0)).label('likes_count'),
|
||||
func.sum(case((AuthorRating.plus != true(), 1), else_=0)).label('dislikes_count'),
|
||||
func.sum(case((and_(Reaction.kind == ReactionKind.LIKE.value, Shout.authors.any(id=Author.id)), 1), else_=0)).label('shouts_likes'),
|
||||
func.sum(case((and_(Reaction.kind == ReactionKind.DISLIKE.value, Shout.authors.any(id=Author.id)), 1), else_=0)).label('shouts_dislikes'),
|
||||
]
|
||||
q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id)
|
||||
q = q.add_columns(
|
||||
func.sum(case((AuthorRating.plus == true(), 1), else_=0)).label('likes_count'),
|
||||
func.sum(case((AuthorRating.plus != true(), 1), else_=0)).label('dislikes_count'),
|
||||
)
|
||||
|
||||
sub_rating = (
|
||||
select(*select_list)
|
||||
.outerjoin(
|
||||
Reaction,
|
||||
and_(
|
||||
Reaction.created_by == Author.id,
|
||||
Reaction.reply_to.is_(None),
|
||||
Reaction.deleted_at.is_(None),
|
||||
),
|
||||
if with_rating:
|
||||
subq = select(Reaction).where(
|
||||
and_(
|
||||
Reaction.shout == Shout.id,
|
||||
Shout.authors.any(id=Author.id),
|
||||
Reaction.reply_to.is_(None),
|
||||
Reaction.deleted_at.is_(None)
|
||||
)
|
||||
.group_by(Author.id)
|
||||
.subquery()
|
||||
)
|
||||
q = q.outerjoin(sub_rating, Author.id == sub_rating.c.id)
|
||||
).subquery()
|
||||
|
||||
q = q.outerjoin(subq, subq.c.shout == Shout.id)
|
||||
q = q.add_columns(
|
||||
sub_rating.c.likes_count,
|
||||
sub_rating.c.dislikes_count,
|
||||
sub_rating.c.shouts_likes,
|
||||
sub_rating.c.shouts_dislikes,
|
||||
)
|
||||
q = q.group_by(
|
||||
Author.id,
|
||||
sub_comments.c.comments_count,
|
||||
sub_rating.c.likes_count,
|
||||
sub_rating.c.dislikes_count,
|
||||
sub_rating.c.shouts_likes,
|
||||
sub_rating.c.shouts_dislikes,
|
||||
)
|
||||
else:
|
||||
q = q.group_by(
|
||||
Author.id,
|
||||
sub_comments.c.comments_count
|
||||
func.count(case((subq.c.kind == ReactionKind.LIKE.value, subq.c.id), else_=None)).label('shouts_likes'),
|
||||
func.count(case((subq.c.kind == ReactionKind.DISLIKE.value, subq.c.id), else_=None)).label('shouts_dislikes'),
|
||||
)
|
||||
|
||||
q = q.group_by(
|
||||
Author.id,
|
||||
sub_comments.c.comments_count
|
||||
)
|
||||
|
||||
return q
|
||||
|
||||
|
||||
|
@ -173,10 +154,10 @@ def get_with_stat(q, with_rating=False):
|
|||
if is_author:
|
||||
stat['comments'] = cols[4]
|
||||
if with_rating:
|
||||
logger.debug('author, shouts, authors, followers, comments, rating, rating_shouts')
|
||||
logger.debug('author, shouts, authors, followers, comments, author_likes, author_dislikes, shouts_likes, shouts_dislikes, comment_likes, comments_dislikes')
|
||||
logger.debug(cols)
|
||||
stat['rating'] = cols[6] - cols[6]
|
||||
stat['rating_shouts'] = cols[7] - cols[8]
|
||||
stat['rating'] = cols[6] - cols[7]
|
||||
stat['rating_shouts'] = cols[8] - cols[9]
|
||||
entity.stat = stat
|
||||
records.append(entity)
|
||||
except Exception as exc:
|
||||
|
|
Loading…
Reference in New Issue
Block a user