get-shouts-with-stats-fix4
Some checks failed
Deploy on push / deploy (push) Failing after 10s

This commit is contained in:
Untone 2024-10-31 13:52:32 +03:00
parent d48577b191
commit f61a2d07fe

View File

@ -155,24 +155,19 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
:param offset: Смещение для пагинации. :param offset: Смещение для пагинации.
:return: Список публикаций с включенной статистикой. :return: Список публикаций с включенной статистикой.
""" """
# Определение алиасов подзапросов # Определение алиасов подзапросов авторов и тем (предполагается, что они уже определены ранее)
authors_subquery = ( authors_subquery = (
select( select(
ShoutAuthor.shout.label("shout_id"), ShoutAuthor.shout.label("shout_id"),
func.json_agg( func.json_agg(
func.json_build_object( func.json_build_object(
"id", "id", Author.id,
Author.id, "name", Author.name,
"name", "slug", Author.slug,
Author.name, "pic", Author.pic,
"slug", "caption", ShoutAuthor.caption
Author.slug,
"pic",
Author.pic,
"caption",
ShoutAuthor.caption,
) )
).label("authors"), ).label("authors")
) )
.join(Author, ShoutAuthor.author == Author.id) .join(Author, ShoutAuthor.author == Author.id)
.group_by(ShoutAuthor.shout) .group_by(ShoutAuthor.shout)
@ -184,20 +179,32 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
ShoutTopic.shout.label("shout_id"), ShoutTopic.shout.label("shout_id"),
func.json_agg( func.json_agg(
func.json_build_object( func.json_build_object(
"id", Topic.id, "title", Topic.title, "slug", Topic.slug, "is_main", ShoutTopic.main "id", Topic.id,
"title", Topic.title,
"slug", Topic.slug,
"is_main", ShoutTopic.main
) )
).label("topics"), ).label("topics"),
func.max(func.case([(ShoutTopic.main, Topic.slug)])).label("main_topic_slug"), func.max(
case(
(ShoutTopic.main, Topic.slug)
)
).label("main_topic_slug")
) )
.join(Topic, ShoutTopic.topic == Topic.id) .join(Topic, ShoutTopic.topic == Topic.id)
.group_by(ShoutTopic.shout) .group_by(ShoutTopic.shout)
.subquery("topics_subquery") .subquery("topics_subquery")
) )
# Определение подзапроса для получения даты последней реакции
last_reaction = ( last_reaction = (
select(func.max(Reaction.created_at).label("last_reacted_at")) select(
.filter(Reaction.shout == Shout.id, Reaction.deleted_at.is_(None)) Reaction.shout.label("shout_id"),
.subquery() func.max(Reaction.created_at).label("last_reacted_at")
)
.filter(Reaction.deleted_at.is_(None))
.group_by(Reaction.shout)
.subquery("last_reaction")
) )
# Основной запрос # Основной запрос
@ -207,8 +214,8 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
func.count(func.distinct(Reaction.id)).label("comments_stat"), func.count(func.distinct(Reaction.id)).label("comments_stat"),
func.sum( func.sum(
case( case(
(Reaction.kind == ReactionKind.LIKE.value, 1), (Reaction.kind == "LIKE", 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1), (Reaction.kind == "DISLIKE", -1),
else_=0 else_=0
) )
).label("rating_stat"), ).label("rating_stat"),
@ -230,7 +237,8 @@ def get_shouts_with_stats(q, limit=50, offset=0, author_id=None):
Shout.id, Shout.id,
authors_subquery.c.authors, authors_subquery.c.authors,
topics_subquery.c.topics, topics_subquery.c.topics,
topics_subquery.c.main_topic_slug topics_subquery.c.main_topic_slug,
last_reaction.c.last_reacted_at
) )
.order_by(Shout.published_at.desc().nullslast()) .order_by(Shout.published_at.desc().nullslast())
.limit(limit) .limit(limit)