shout-query-substat-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m12s
All checks were successful
Deploy on push / deploy (push) Successful in 1m12s
This commit is contained in:
parent
78d575863d
commit
998db09c09
|
@ -81,21 +81,45 @@ def query_shouts(slug=None):
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Подзапросы для каждого счетчика
|
||||||
|
comments_subquery = (
|
||||||
|
select(func.count(distinct(aliased_reaction.id)).label("comments_count"))
|
||||||
|
.where(and_(
|
||||||
|
aliased_reaction.shout == Shout.id,
|
||||||
|
aliased_reaction.kind == ReactionKind.COMMENT.value,
|
||||||
|
aliased_reaction.deleted_at.is_(None)
|
||||||
|
))
|
||||||
|
.scalar_subquery()
|
||||||
|
)
|
||||||
|
|
||||||
|
# followers_subquery = (
|
||||||
|
# select(func.count(distinct(ShoutReactionsFollower.follower)).label("followers_count"))
|
||||||
|
# .where(ShoutReactionsFollower.shout == Shout.id)
|
||||||
|
# .scalar_subquery()
|
||||||
|
# )
|
||||||
|
|
||||||
|
rating_subquery = (
|
||||||
|
select(func.sum(
|
||||||
|
case(
|
||||||
|
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
|
||||||
|
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
||||||
|
else_=0
|
||||||
|
)
|
||||||
|
).label("rating"))
|
||||||
|
.where(and_(
|
||||||
|
aliased_reaction.shout == Shout.id,
|
||||||
|
aliased_reaction.reply_to.is_(None),
|
||||||
|
aliased_reaction.deleted_at.is_(None)
|
||||||
|
))
|
||||||
|
.scalar_subquery()
|
||||||
|
)
|
||||||
|
|
||||||
# Основной запрос с использованием подзапросов
|
# Основной запрос с использованием подзапросов
|
||||||
q = (
|
q = (
|
||||||
select(
|
select(
|
||||||
Shout,
|
Shout,
|
||||||
func.count(aliased_reaction.id)
|
comments_subquery.label("comments_stat"),
|
||||||
.filter(aliased_reaction.kind == ReactionKind.COMMENT.value)
|
rating_subquery.label("rating_stat"),
|
||||||
.label("comments_stat"),
|
|
||||||
# func.count(ShoutReactionsFollower.follower).label("followers_stat"),
|
|
||||||
func.sum(
|
|
||||||
case(
|
|
||||||
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
|
|
||||||
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
|
||||||
else_=0,
|
|
||||||
)
|
|
||||||
).label("rating_stat"),
|
|
||||||
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
func.max(aliased_reaction.created_at).label("last_reacted_at"),
|
||||||
authors_subquery.c.authors.label("authors"),
|
authors_subquery.c.authors.label("authors"),
|
||||||
topics_subquery.c.topics.label("topics"),
|
topics_subquery.c.topics.label("topics"),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user