myrate-fix+log
All checks were successful
Deploy on push / deploy (push) Successful in 6s

This commit is contained in:
Untone 2024-11-14 13:25:33 +03:00
parent 88812da592
commit 2d0ca1c7bf

View File

@ -141,6 +141,7 @@ def query_with_stat(info):
q = q.add_columns(authors_subquery.c.authors) q = q.add_columns(authors_subquery.c.authors)
if has_field(info, "stat"): if has_field(info, "stat"):
logger.info("Building stat query...")
stats_subquery = ( stats_subquery = (
select( select(
Reaction.shout, Reaction.shout,
@ -165,8 +166,10 @@ def query_with_stat(info):
author_dict = info.context.get("author") if info.context else None author_dict = info.context.get("author") if info.context else None
author_id = author_dict.get("id") if author_dict else None author_id = author_dict.get("id") if author_dict else None
logger.info(f"Current author_id: {author_id}")
if author_id: if author_id:
logger.info(f"Building user reaction query for author {author_id}")
user_reaction_subquery = ( user_reaction_subquery = (
select( select(
Reaction.shout.label("shout_id"), Reaction.shout.label("shout_id"),
@ -180,20 +183,22 @@ def query_with_stat(info):
Reaction.reply_to.is_(None), Reaction.reply_to.is_(None),
) )
) )
.order_by(Reaction.created_at.desc()) # Берем последнюю реакцию .order_by(Reaction.created_at.desc())
.distinct(Reaction.shout) .distinct(Reaction.shout)
.subquery() .subquery()
) )
logger.info("Joining stats and user reaction subqueries")
q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id) 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) 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( q = q.add_columns(
json_builder( json_builder(
"comments_count", "comments_count",
stats_subquery.c.comments_count, func.coalesce(stats_subquery.c.comments_count, 0),
"rating", "rating",
stats_subquery.c.rating, func.coalesce(stats_subquery.c.rating, 0),
"last_reacted_at", "last_reacted_at",
stats_subquery.c.last_reacted_at, stats_subquery.c.last_reacted_at,
"my_rate", "my_rate",
@ -201,13 +206,14 @@ def query_with_stat(info):
).label("stat") ).label("stat")
) )
else: else:
logger.info("No author found, building query without user reactions")
q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id) q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id)
q = q.add_columns( q = q.add_columns(
json_builder( json_builder(
"comments_count", "comments_count",
stats_subquery.c.comments_count, func.coalesce(stats_subquery.c.comments_count, 0),
"rating", "rating",
stats_subquery.c.rating, func.coalesce(stats_subquery.c.rating, 0),
"last_reacted_at", "last_reacted_at",
stats_subquery.c.last_reacted_at, stats_subquery.c.last_reacted_at,
"my_rate", "my_rate",