author-stats-fix
Some checks failed
Deploy on push / deploy (push) Failing after 4m45s

This commit is contained in:
2025-08-31 22:42:21 +03:00
parent 66f2e0131b
commit d68030faca
2 changed files with 52 additions and 25 deletions

View File

@@ -151,6 +151,17 @@ async def get_authors_with_stats(
# Базовый запрос для получения авторов # Базовый запрос для получения авторов
base_query = select(Author).where(Author.deleted_at.is_(None)) base_query = select(Author).where(Author.deleted_at.is_(None))
# Применяем фильтрацию по параметрам из by
if by:
for key, value in by.items():
if key != "order" and value is not None: # order обрабатывается отдельно
if hasattr(Author, key):
column = getattr(Author, key)
base_query = base_query.where(column == value)
logger.debug(f"Applied filter: {key} = {value}")
else:
logger.warning(f"Unknown filter field: {key}")
# vars for statistics sorting # vars for statistics sorting
stats_sort_field = None stats_sort_field = None
default_sort_applied = False default_sort_applied = False
@@ -753,8 +764,7 @@ async def get_author(
if "stat" in cached_author: if "stat" in cached_author:
author_dict["stat"] = cached_author["stat"] author_dict["stat"] = cached_author["stat"]
if not author_dict or not author_dict.get("stat"): # ВСЕГДА используем новый резолвер для получения полной статистики
# update stat from db using new comprehensive stats
try: try:
# Используем новый резолвер для получения полной статистики # Используем новый резолвер для получения полной статистики
filter_by: AuthorsBy = {} filter_by: AuthorsBy = {}
@@ -763,17 +773,33 @@ async def get_author(
elif author_id: elif author_id:
filter_by["id"] = author_id # author_id уже int filter_by["id"] = author_id # author_id уже int
logger.debug(f"Calling get_authors_with_stats with filter: {filter_by}")
authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by) authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by)
if authors_with_stats and len(authors_with_stats) > 0: if authors_with_stats and len(authors_with_stats) > 0:
author_dict = authors_with_stats[0] author_dict = authors_with_stats[0]
logger.debug(f"Got author with stats: shouts={author_dict.get('stat', {}).get('shouts', 'missing')}")
# Фильтруем данные согласно правам доступа # Фильтруем данные согласно правам доступа
if not is_admin: if not is_admin:
# Убираем защищенные поля для не-админов # Убираем защищенные поля для не-админов
protected_fields = ["email", "phone"] protected_fields = ["email", "phone"]
for field in protected_fields: for field in protected_fields:
author_dict.pop(field, None) author_dict.pop(field, None)
# Кэшируем полные данные для админов # Кэшируем полные данные для админов
_t = asyncio.create_task(cache_author(author_dict)) _t = asyncio.create_task(cache_author(author_dict))
else:
logger.warning(f"No author found with filter: {filter_by}")
# Fallback to basic author data without stats
with local_session() as session:
if slug:
author = session.query(Author).filter_by(slug=slug).first()
else:
author = session.query(Author).filter_by(id=author_id).first()
if author:
author_dict = author.dict(is_admin)
logger.debug(f"Using fallback author dict: {author_dict.get('name', 'unknown')}")
except Exception as e: except Exception as e:
logger.error(f"Error getting author stats: {e}") logger.error(f"Error getting author stats: {e}")
# Fallback to basic author data without stats # Fallback to basic author data without stats
@@ -906,11 +932,11 @@ async def get_author_follows(
has_access = is_admin or (viewer_id is not None and str(viewer_id) == str(temp_author.id)) has_access = is_admin or (viewer_id is not None and str(viewer_id) == str(temp_author.id))
followed_authors.append(temp_author.dict(has_access)) followed_authors.append(temp_author.dict(has_access))
# TODO: Get followed communities too followed_communities = DEFAULT_COMMUNITIES # TODO: get followed communities
return { return {
"authors": followed_authors, "authors": followed_authors,
"topics": followed_topics, "topics": followed_topics,
"communities": DEFAULT_COMMUNITIES, "communities": followed_communities,
"shouts": [], "shouts": [],
"error": None, "error": None,
} }

View File

@@ -8,8 +8,9 @@ type AuthorStat {
# Взаимодействие с другими авторами # Взаимодействие с другими авторами
coauthors: Int # Количество уникальных соавторов coauthors: Int # Количество уникальных соавторов
followers: Int # Количество подписчиков followers: Int # Количество подписчиков
authors: Int # Количество авторов, на которых подписан данный автор
# Рейтинговая система # Общий рейтинг (rating_shouts + rating_comments) # Рейтинговая система
rating_shouts: Int # Рейтинг публикаций (сумма реакций LIKE/AGREE/ACCEPT/PROOF/CREDIT минус DISLIKE/DISAGREE/REJECT/DISPROOF) rating_shouts: Int # Рейтинг публикаций (сумма реакций LIKE/AGREE/ACCEPT/PROOF/CREDIT минус DISLIKE/DISAGREE/REJECT/DISPROOF)
rating_comments: Int # Рейтинг комментариев (реакции на комментарии автора) rating_comments: Int # Рейтинг комментариев (реакции на комментарии автора)