From 2d0ca1c7bff3796d03d641130f7fe6e278e893eb Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 14 Nov 2024 13:25:33 +0300 Subject: [PATCH] myrate-fix+log --- resolvers/reader.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index 653cc4e7..164d911f 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -141,6 +141,7 @@ def query_with_stat(info): q = q.add_columns(authors_subquery.c.authors) if has_field(info, "stat"): + logger.info("Building stat query...") stats_subquery = ( select( Reaction.shout, @@ -165,8 +166,10 @@ def query_with_stat(info): author_dict = info.context.get("author") if info.context else None author_id = author_dict.get("id") if author_dict else None + logger.info(f"Current author_id: {author_id}") if author_id: + logger.info(f"Building user reaction query for author {author_id}") user_reaction_subquery = ( select( Reaction.shout.label("shout_id"), @@ -180,20 +183,22 @@ def query_with_stat(info): Reaction.reply_to.is_(None), ) ) - .order_by(Reaction.created_at.desc()) # Берем последнюю реакцию + .order_by(Reaction.created_at.desc()) .distinct(Reaction.shout) .subquery() ) + logger.info("Joining stats and user reaction subqueries") q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id) q = q.outerjoin(user_reaction_subquery, user_reaction_subquery.c.shout_id == Shout.id) + logger.info("Building final query with user reactions") q = q.add_columns( json_builder( "comments_count", - stats_subquery.c.comments_count, + func.coalesce(stats_subquery.c.comments_count, 0), "rating", - stats_subquery.c.rating, + func.coalesce(stats_subquery.c.rating, 0), "last_reacted_at", stats_subquery.c.last_reacted_at, "my_rate", @@ -201,13 +206,14 @@ def query_with_stat(info): ).label("stat") ) else: + logger.info("No author found, building query without user reactions") q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id) q = q.add_columns( json_builder( "comments_count", - stats_subquery.c.comments_count, + func.coalesce(stats_subquery.c.comments_count, 0), "rating", - stats_subquery.c.rating, + func.coalesce(stats_subquery.c.rating, 0), "last_reacted_at", stats_subquery.c.last_reacted_at, "my_rate",