This commit is contained in:
parent
6e5545b190
commit
e375db4125
12
cache/cache.py
vendored
12
cache/cache.py
vendored
|
@ -310,13 +310,11 @@ async def get_cached_author_by_id(author_id: int, get_with_stat):
|
||||||
Returns:
|
Returns:
|
||||||
dict: Dictionary with author data or None if not found.
|
dict: Dictionary with author data or None if not found.
|
||||||
"""
|
"""
|
||||||
# Attempt to find author ID by author_id in Redis cache
|
# Attempt to find author data by author_id in Redis cache
|
||||||
author_id = await redis.execute("GET", f"author:id:{author_id}")
|
cached_author_data = await redis.execute("GET", f"author:id:{author_id}")
|
||||||
if author_id:
|
if cached_author_data:
|
||||||
# If ID is found, get full author data by ID
|
# If data is found, return parsed JSON
|
||||||
author_data = await redis.execute("GET", f"author:id:{author_id}")
|
return orjson.loads(cached_author_data)
|
||||||
if author_data:
|
|
||||||
return orjson.loads(author_data)
|
|
||||||
|
|
||||||
# If data is not found in cache, query the database
|
# If data is not found in cache, query the database
|
||||||
author_query = select(Author).where(Author.id == author_id)
|
author_query = select(Author).where(Author.id == author_id)
|
||||||
|
|
|
@ -243,7 +243,7 @@ def get_author_followers_stat(author_id: int) -> int:
|
||||||
return result[0] if result else 0
|
return result[0] if result else 0
|
||||||
|
|
||||||
|
|
||||||
def get_author_comments_stat(author_id):
|
def get_author_comments_stat(author_id: int):
|
||||||
q = (
|
q = (
|
||||||
select(func.coalesce(func.count(Reaction.id), 0).label("comments_count"))
|
select(func.coalesce(func.count(Reaction.id), 0).label("comments_count"))
|
||||||
.select_from(Author)
|
.select_from(Author)
|
||||||
|
@ -289,10 +289,28 @@ def get_with_stat(q):
|
||||||
stat["shouts"] = cols[1] # Статистика по публикациям
|
stat["shouts"] = cols[1] # Статистика по публикациям
|
||||||
stat["followers"] = cols[2] # Статистика по подписчикам
|
stat["followers"] = cols[2] # Статистика по подписчикам
|
||||||
if is_author:
|
if is_author:
|
||||||
stat["authors"] = get_author_authors_stat(entity.id) # Статистика по подпискам на авторов
|
# Дополнительная проверка типа entity.id
|
||||||
stat["comments"] = get_author_comments_stat(entity.id) # Статистика по комментариям
|
if not hasattr(entity, 'id'):
|
||||||
|
logger.error(f"Entity does not have id attribute: {entity}")
|
||||||
|
continue
|
||||||
|
entity_id = entity.id
|
||||||
|
if not isinstance(entity_id, int):
|
||||||
|
logger.error(f"Entity id is not integer: {entity_id} (type: {type(entity_id)})")
|
||||||
|
continue
|
||||||
|
|
||||||
|
stat["authors"] = get_author_authors_stat(entity_id) # Статистика по подпискам на авторов
|
||||||
|
stat["comments"] = get_author_comments_stat(entity_id) # Статистика по комментариям
|
||||||
else:
|
else:
|
||||||
stat["authors"] = get_topic_authors_stat(entity.id) # Статистика по авторам темы
|
# Дополнительная проверка типа entity.id для тем
|
||||||
|
if not hasattr(entity, 'id'):
|
||||||
|
logger.error(f"Entity does not have id attribute: {entity}")
|
||||||
|
continue
|
||||||
|
entity_id = entity.id
|
||||||
|
if not isinstance(entity_id, int):
|
||||||
|
logger.error(f"Entity id is not integer: {entity_id} (type: {type(entity_id)})")
|
||||||
|
continue
|
||||||
|
|
||||||
|
stat["authors"] = get_topic_authors_stat(entity_id) # Статистика по авторам темы
|
||||||
entity.stat = stat
|
entity.stat = stat
|
||||||
records.append(entity)
|
records.append(entity)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user