This commit is contained in:
parent
b4eff32427
commit
471781f942
|
@ -69,7 +69,7 @@ def query_with_stat(info):
|
||||||
.join(Author, Author.id == Shout.created_by)
|
.join(Author, Author.id == Shout.created_by)
|
||||||
)
|
)
|
||||||
|
|
||||||
# main_author
|
# Главный автор
|
||||||
main_author = aliased(Author)
|
main_author = aliased(Author)
|
||||||
q = q.join(main_author, main_author.id == Shout.created_by)
|
q = q.join(main_author, main_author.id == Shout.created_by)
|
||||||
q = q.add_columns(
|
q = q.add_columns(
|
||||||
|
@ -144,7 +144,9 @@ 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...")
|
logger.info("Начало построения запроса статистики")
|
||||||
|
|
||||||
|
# Подзапрос для статистики реакций
|
||||||
stats_subquery = (
|
stats_subquery = (
|
||||||
select(
|
select(
|
||||||
Reaction.shout,
|
Reaction.shout,
|
||||||
|
@ -160,21 +162,25 @@ def query_with_stat(info):
|
||||||
)
|
)
|
||||||
.filter(Reaction.reply_to.is_(None))
|
.filter(Reaction.reply_to.is_(None))
|
||||||
.label("rating"),
|
.label("rating"),
|
||||||
func.max(Reaction.created_at).filter(Reaction.reply_to.is_(None)).label("last_reacted_at"),
|
func.max(Reaction.created_at)
|
||||||
|
.filter(Reaction.reply_to.is_(None))
|
||||||
|
.label("last_reacted_at"),
|
||||||
)
|
)
|
||||||
.where(Reaction.deleted_at.is_(None))
|
.where(Reaction.deleted_at.is_(None))
|
||||||
.group_by(Reaction.shout)
|
.group_by(Reaction.shout)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
logger.info("Подзапрос статистики построен")
|
||||||
logger.debug(info)
|
|
||||||
logger.debug(info.context)
|
# Извлечение author_id из контекста
|
||||||
logger.debug(info.context.get("author"))
|
|
||||||
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"Полученный author_id: {author_id}")
|
||||||
|
|
||||||
if author_id:
|
if author_id:
|
||||||
logger.info(f"detected author_id: {author_id}")
|
logger.info(f"Построение подзапроса реакций пользователя с ID: {author_id}")
|
||||||
|
|
||||||
|
# Подзапрос для реакций текущего пользователя
|
||||||
user_reaction_subquery = (
|
user_reaction_subquery = (
|
||||||
select(
|
select(
|
||||||
Reaction.shout.label("shout_id"),
|
Reaction.shout.label("shout_id"),
|
||||||
|
@ -188,16 +194,18 @@ def query_with_stat(info):
|
||||||
Reaction.reply_to.is_(None),
|
Reaction.reply_to.is_(None),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.order_by(Reaction.created_at.desc())
|
.order_by(Reaction.shout, Reaction.created_at.desc())
|
||||||
.distinct(Reaction.shout)
|
.distinct(Reaction.shout)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
logger.info("Подзапрос реакций пользователя построен")
|
||||||
|
|
||||||
logger.info("Joining stats and user reaction subqueries")
|
logger.info("Соединение основного запроса с подзапросом статистики")
|
||||||
q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id)
|
q = q.outerjoin(stats_subquery, stats_subquery.c.shout == Shout.id)
|
||||||
|
logger.info("Соединение основного запроса с подзапросом реакций пользователя")
|
||||||
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")
|
logger.info("Добавление колонок статистики в основной запрос")
|
||||||
q = q.add_columns(
|
q = q.add_columns(
|
||||||
json_builder(
|
json_builder(
|
||||||
"comments_count",
|
"comments_count",
|
||||||
|
@ -210,8 +218,9 @@ def query_with_stat(info):
|
||||||
user_reaction_subquery.c.my_rate
|
user_reaction_subquery.c.my_rate
|
||||||
).label("stat")
|
).label("stat")
|
||||||
)
|
)
|
||||||
|
logger.info("Колонки статистики добавлены")
|
||||||
else:
|
else:
|
||||||
logger.info("No author found, building query without user reactions")
|
logger.info("Автор не найден, строим запрос без подзапроса реакций пользователя")
|
||||||
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(
|
||||||
|
@ -225,6 +234,7 @@ def query_with_stat(info):
|
||||||
None
|
None
|
||||||
).label("stat")
|
).label("stat")
|
||||||
)
|
)
|
||||||
|
logger.info("Колонки статистики без my_rate добавлены")
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -235,34 +245,34 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
"""
|
"""
|
||||||
shouts = []
|
shouts = []
|
||||||
try:
|
try:
|
||||||
logger.info(f"Starting get_shouts_with_links with limit={limit}, offset={offset}")
|
logger.info(f"Начало выполнения get_shouts_with_links с limit={limit}, offset={offset}")
|
||||||
q = q.limit(limit).offset(offset)
|
q = q.limit(limit).offset(offset)
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
logger.info("Executing query...")
|
logger.info("Выполнение основного запроса")
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
shouts_result = session.execute(q).all()
|
shouts_result = session.execute(q).all()
|
||||||
logger.info(f"Query executed, got {len(shouts_result)} results in {time.time() - t1:.3f} seconds")
|
logger.info(f"Запрос выполнен, получено {len(shouts_result)} результатов за {time.time() - t1:.3f} секунд")
|
||||||
|
|
||||||
if not shouts_result:
|
if not shouts_result:
|
||||||
logger.warning("No results found")
|
logger.warning("Нет найденных результатов")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
shouts = []
|
|
||||||
|
|
||||||
for idx, row in enumerate(shouts_result):
|
for idx, row in enumerate(shouts_result):
|
||||||
try:
|
try:
|
||||||
|
logger.debug(f"Обработка строки {idx}")
|
||||||
|
|
||||||
shout = None
|
shout = None
|
||||||
if hasattr(row, "Shout"):
|
if hasattr(row, "Shout"):
|
||||||
shout = row.Shout
|
shout = row.Shout
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Row {idx} has no Shout attribute: {row}")
|
logger.warning(f"Строка {idx} не содержит атрибута 'Shout': {row}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if shout:
|
if shout:
|
||||||
shout_id = int(f"{shout.id}")
|
shout_id = int(f"{shout.id}")
|
||||||
# logger.info(f"Processing shout ID: {shout_id}")
|
|
||||||
shout_dict = shout.dict()
|
shout_dict = shout.dict()
|
||||||
|
|
||||||
if has_field(info, "created_by") and shout_dict.get("created_by"):
|
if has_field(info, "created_by") and shout_dict.get("created_by"):
|
||||||
main_author_id = shout_dict.get("created_by")
|
main_author_id = shout_dict.get("created_by")
|
||||||
a = session.query(Author).filter(Author.id == main_author_id).first()
|
a = session.query(Author).filter(Author.id == main_author_id).first()
|
||||||
|
@ -272,15 +282,21 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
"slug": a.slug,
|
"slug": a.slug,
|
||||||
"pic": a.pic,
|
"pic": a.pic,
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasattr(row, "stat"):
|
if hasattr(row, "stat"):
|
||||||
|
logger.debug(f"Строка {idx} - stat: {row.stat}")
|
||||||
stat = {}
|
stat = {}
|
||||||
if isinstance(row.stat, str):
|
if isinstance(row.stat, str):
|
||||||
stat = json.loads(row.stat)
|
stat = json.loads(row.stat)
|
||||||
elif isinstance(row.stat, dict):
|
elif isinstance(row.stat, dict):
|
||||||
stat = row.stat
|
stat = row.stat
|
||||||
|
else:
|
||||||
|
logger.warning(f"Строка {idx} - неизвестный тип stat: {type(row.stat)}")
|
||||||
viewed = ViewedStorage.get_shout(shout_id=shout_id) or 0
|
viewed = ViewedStorage.get_shout(shout_id=shout_id) or 0
|
||||||
shout_dict["stat"] = {**stat, "viewed": viewed, "commented": stat.get("comments_count", 0)}
|
shout_dict["stat"] = {**stat, "viewed": viewed, "commented": stat.get("comments_count", 0)}
|
||||||
|
else:
|
||||||
|
logger.warning(f"Строка {idx} не содержит атрибута 'stat'")
|
||||||
|
|
||||||
if has_field(info, "main_topic") and hasattr(row, "main_topic"):
|
if has_field(info, "main_topic") and hasattr(row, "main_topic"):
|
||||||
shout_dict["main_topic"] = (
|
shout_dict["main_topic"] = (
|
||||||
json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic
|
json.loads(row.main_topic) if isinstance(row.main_topic, str) else row.main_topic
|
||||||
|
@ -292,13 +308,13 @@ def get_shouts_with_links(info, q, limit=20, offset=0):
|
||||||
if has_field(info, "topics") and hasattr(row, "topics"):
|
if has_field(info, "topics") and hasattr(row, "topics"):
|
||||||
shout_dict["topics"] = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
|
shout_dict["topics"] = json.loads(row.topics) if isinstance(row.topics, str) else row.topics
|
||||||
|
|
||||||
shouts.append(shout_dict)
|
shouts.append(shout_dict)
|
||||||
|
|
||||||
except Exception as row_error:
|
except Exception as row_error:
|
||||||
logger.error(f"Error processing row {idx}: {row_error}", exc_info=True)
|
logger.error(f"Ошибка при обработке строки {idx}: {row_error}", exc_info=True)
|
||||||
continue
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Fatal error in get_shouts_with_links: {e}", exc_info=True)
|
logger.error(f"Фатальная ошибка в get_shouts_with_links: {e}", exc_info=True)
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
return shouts
|
return shouts
|
||||||
|
|
Loading…
Reference in New Issue
Block a user