topics-comments-stat

This commit is contained in:
Untone 2024-03-06 13:43:30 +03:00
parent 70c5233305
commit 2c1bfaf0fe
2 changed files with 30 additions and 18 deletions

View File

@ -31,21 +31,32 @@ def add_topic_stat_columns(q):
'followers_stat' 'followers_stat'
) )
) )
# TODO: topic.stat.comments )
# .outerjoin(aliased_reaction) # Create a subquery for comments count
# .add_columns( sub_comments = (
# func.count(distinct(aliased_reaction.id)).filter( select(
# and_( Shout.id, func.coalesce(func.count(Reaction.id), 0).label('comments_count')
# aliased_reaction.created_by == Author.id, )
# aliased_reaction.kind == ReactionKind.COMMENT.value, .join(
# aliased_reaction.deleted_at.is_(None), Reaction,
# ) and_(
# ) Reaction.shout_id == Shout.id,
# .label('comments_count') Reaction.kind == ReactionKind.COMMENT.value,
# ) Reaction.deleted_at.is_(None),
),
)
.group_by(Shout.id)
.subquery()
) )
q = q.group_by(Topic.id) q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.shout_id)
q = q.add_columns(
func.coalesce(func.sum(sub_comments.c.comments_count), 0).label('comments_stat')
)
q = q.group_by(
Topic.id, 'shouts_stat', 'authors_stat', 'followers_stat', 'comments_stat'
)
return q return q
@ -73,8 +84,7 @@ def add_author_stat_columns(q):
# Create a subquery for comments count # Create a subquery for comments count
sub_comments = ( sub_comments = (
select( select(
Author.id, Author.id, func.coalesce(func.count(Reaction.id), 0).label('comments_stat')
func.coalesce(func.count(Reaction.id), 0).label('comments_stat')
) )
.outerjoin( .outerjoin(
Reaction, Reaction,
@ -82,7 +92,7 @@ def add_author_stat_columns(q):
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) .group_by(Author.id)
.subquery() .subquery()

View File

@ -28,8 +28,10 @@ class WebhookEndpoint(HTTPEndpoint):
status_code=400, detail='User data is not a dictionary' status_code=400, detail='User data is not a dictionary'
) )
user_id: str = user.get('id') user_id: str = user.get('id')
name: str = (f"{user.get('given_name', user.get('slug'))} {user.get('middle_name', '')}" + name: str = (
"{user.get('family_name', '')}".strip()) or 'Аноним' f"{user.get('given_name', user.get('slug'))} {user.get('middle_name', '')}"
+ "{user.get('family_name', '')}".strip()
) or 'Аноним'
email: str = user.get('email', '') email: str = user.get('email', '')
pic: str = user.get('picture', '') pic: str = user.get('picture', '')