alc
Some checks failed
Deploy on push / deploy (push) Failing after 8s

This commit is contained in:
Untone 2024-08-07 14:54:13 +03:00
parent 1524f141b8
commit c601fcc2a4

View File

@ -23,6 +23,8 @@ from utils.logger import root_logger as logger
from services.schema import query
from services.search import search_text
from services.viewed import ViewedStorage
def query_shouts():
"""
Базовый запрос для получения публикаций с подзапросами статистики, авторов и тем.
@ -32,44 +34,44 @@ def query_shouts():
# Создаем алиасы для таблиц для избежания конфликтов имен
aliased_reaction = aliased(Reaction)
# Используем чистый SQL-запрос для подзапросов с уникальными значениями
authors_subquery = text("""
SELECT
shout.id AS shout_id,
json_agg(distinct json_build_object(
'id', author.id,
'name', author.name,
'slug', author.slug,
'pic', author.pic
)) AS authors
FROM
author
JOIN
shout_author ON shout_author.author = author.id
JOIN
shout ON shout.id = shout_author.shout
GROUP BY
shout.id
""")
# Используем SQLAlchemy для подзапросов с уникальными значениями
authors_subquery = (
select(
Shout.id.label("shout_id"),
func.json_agg(
distinct(
func.json_build_object(
'id', Author.id,
'name', Author.name,
'slug', Author.slug,
'pic', Author.pic
)
)
).label("authors")
)
.join(ShoutAuthor, ShoutAuthor.author == Author.id)
.group_by(Shout.id)
.subquery()
)
topics_subquery = text("""
SELECT
shout.id AS shout_id,
json_agg(distinct json_build_object(
'id', topic.id,
'title', topic.title,
'body', topic.body,
'slug', topic.slug
)) AS topics
FROM
topic
JOIN
shout_topic ON shout_topic.topic = topic.id
JOIN
shout ON shout.id = shout_topic.shout
GROUP BY
shout.id
""")
topics_subquery = (
select(
Shout.id.label("shout_id"),
func.json_agg(
distinct(
func.json_build_object(
'id', Topic.id,
'title', Topic.title,
'body', Topic.body,
'slug', Topic.slug
)
)
).label("topics")
)
.join(ShoutTopic, ShoutTopic.topic == Topic.id)
.group_by(Shout.id)
.subquery()
)
# Основной запрос с использованием подзапросов
q = (