core/resolvers/stat.py

569 lines
21 KiB
Python
Raw Permalink Normal View History

2024-06-11 14:51:34 +00:00
import asyncio
import sys
from typing import Any, Optional
2024-06-11 14:51:34 +00:00
2024-05-18 13:16:09 +00:00
from sqlalchemy import and_, distinct, func, join, select
2024-02-22 23:49:34 +00:00
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import Select
2024-02-21 17:12:47 +00:00
2025-05-16 06:23:48 +00:00
from auth.orm import Author, AuthorFollower
2025-05-29 09:37:39 +00:00
from cache.cache import cache_author
from orm.community import Community, CommunityFollower
2024-02-25 10:29:57 +00:00
from orm.reaction import Reaction, ReactionKind
2024-04-08 07:38:58 +00:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic, TopicFollower
2024-04-19 15:22:07 +00:00
from services.db import local_session
2024-08-07 05:57:56 +00:00
from utils.logger import root_logger as logger
2024-02-21 17:12:47 +00:00
# Type alias for queries
QueryType = Select
2024-02-21 17:12:47 +00:00
def add_topic_stat_columns(q: QueryType) -> QueryType:
2024-08-07 11:02:36 +00:00
"""
Добавляет статистические колонки к запросу тем.
:param q: SQL-запрос для получения тем.
:return: Запрос с добавленными колонками статистики.
"""
# Создаем алиасы для предотвращения конфликтов имен
2024-04-23 13:05:27 +00:00
aliased_shout = aliased(ShoutTopic)
2024-05-30 12:05:06 +00:00
2024-08-07 11:02:36 +00:00
# Создаем новый объект запроса для тем
2024-06-04 08:51:39 +00:00
new_q = select(Topic)
2024-08-07 11:02:36 +00:00
# Применяем необходимые фильтры и добавляем колонки статистики
2024-06-05 14:45:55 +00:00
new_q = (
new_q.join(
aliased_shout,
aliased_shout.topic == Topic.id,
)
.join(
Shout,
and_(
aliased_shout.shout == Shout.id,
Shout.deleted_at.is_(None),
),
)
2024-08-07 11:18:05 +00:00
.add_columns(
func.count(distinct(aliased_shout.shout)).label("shouts_stat")
) # Подсчет уникальных публикаций для темы
2024-05-30 11:01:34 +00:00
)
2024-05-30 11:45:41 +00:00
2024-04-23 13:05:27 +00:00
aliased_follower = aliased(TopicFollower)
2024-05-30 12:05:06 +00:00
2024-08-07 11:02:36 +00:00
# Добавляем количество подписчиков темы
2024-06-05 14:45:55 +00:00
new_q = new_q.outerjoin(aliased_follower, aliased_follower.topic == Topic.id).add_columns(
2024-04-25 08:25:39 +00:00
func.count(distinct(aliased_follower.follower)).label("followers_stat")
2024-04-23 13:05:27 +00:00
)
2024-08-07 11:02:36 +00:00
# Группировка по идентификатору темы
return new_q.group_by(Topic.id)
2024-04-23 13:05:27 +00:00
def add_author_stat_columns(q: QueryType) -> QueryType:
2024-08-07 11:02:36 +00:00
"""
Добавляет статистические колонки к запросу авторов.
:param q: SQL-запрос для получения авторов.
:return: Запрос с добавленными колонками статистики.
"""
2025-02-10 15:38:26 +00:00
# Подзапрос для подсчета публикаций
shouts_subq = (
select(func.count(distinct(Shout.id)))
.select_from(ShoutAuthor)
2025-02-11 09:00:35 +00:00
.join(Shout, and_(Shout.id == ShoutAuthor.shout, Shout.deleted_at.is_(None)))
2025-02-10 15:38:26 +00:00
.where(ShoutAuthor.author == Author.id)
.scalar_subquery()
)
# Подзапрос для подсчета подписчиков
followers_subq = (
select(func.count(distinct(AuthorFollower.follower)))
.where(AuthorFollower.author == Author.id)
.scalar_subquery()
)
2024-06-12 09:26:53 +00:00
2025-02-10 15:38:26 +00:00
# Основной запрос
return (
2024-05-30 11:45:41 +00:00
q.select_from(Author)
2025-02-11 09:00:35 +00:00
.add_columns(shouts_subq.label("shouts_stat"), followers_subq.label("followers_stat"))
2025-02-10 15:38:26 +00:00
.group_by(Author.id)
2024-04-23 13:05:27 +00:00
)
2024-04-24 07:42:33 +00:00
2024-08-07 11:02:36 +00:00
def get_topic_shouts_stat(topic_id: int) -> int:
"""
2025-02-03 20:16:50 +00:00
Получает количество опубликованных постов для темы
2024-08-07 11:02:36 +00:00
"""
2024-04-23 11:31:34 +00:00
q = (
select(func.count(distinct(ShoutTopic.shout)))
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
.filter(
and_(
ShoutTopic.topic == topic_id,
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
)
)
2024-04-17 15:32:23 +00:00
)
2025-02-03 23:53:01 +00:00
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-04-23 11:31:34 +00:00
2024-04-09 16:38:02 +00:00
2024-08-07 11:02:36 +00:00
def get_topic_authors_stat(topic_id: int) -> int:
"""
Получает количество уникальных авторов для указанной темы.
:param topic_id: Идентификатор темы.
:return: Количество уникальных авторов, связанных с темой.
"""
2024-05-25 23:17:45 +00:00
count_query = (
2024-04-23 11:31:34 +00:00
select(func.count(distinct(ShoutAuthor.author)))
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
.filter(
and_(
ShoutTopic.topic == topic_id,
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
)
)
2024-04-17 15:32:23 +00:00
)
2024-05-25 23:17:45 +00:00
2024-08-07 11:02:36 +00:00
# Выполнение запроса и получение результата
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(count_query).scalar()
return int(result) if result else 0
2024-04-09 16:38:02 +00:00
2024-04-23 11:31:34 +00:00
2024-08-07 11:02:36 +00:00
def get_topic_followers_stat(topic_id: int) -> int:
"""
Получает количество подписчиков для указанной темы.
:param topic_id: Идентификатор темы.
:return: Количество уникальных подписчиков темы.
"""
2024-04-23 11:31:34 +00:00
aliased_followers = aliased(TopicFollower)
2024-05-30 04:12:00 +00:00
q = select(func.count(distinct(aliased_followers.follower))).filter(aliased_followers.topic == topic_id)
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-04-23 11:31:34 +00:00
2024-04-09 16:38:02 +00:00
2024-08-07 11:02:36 +00:00
def get_topic_comments_stat(topic_id: int) -> int:
"""
Получает количество комментариев для всех публикаций в указанной теме.
:param topic_id: Идентификатор темы.
:return: Общее количество комментариев к публикациям темы.
"""
# Подзапрос для получения количества комментариев для каждой публикации
2024-04-09 16:38:02 +00:00
sub_comments = (
2024-03-06 10:43:30 +00:00
select(
2024-04-17 15:32:23 +00:00
Shout.id.label("shout_id"),
2024-10-15 08:12:09 +00:00
func.coalesce(func.count(Reaction.id), 0).label("comments_count"),
2024-03-06 10:43:30 +00:00
)
2024-04-09 16:48:02 +00:00
.join(ShoutTopic, ShoutTopic.shout == Shout.id)
.join(Topic, ShoutTopic.topic == Topic.id)
2024-04-09 16:38:02 +00:00
.outerjoin(
2024-03-06 10:43:30 +00:00
Reaction,
and_(
2024-03-06 12:08:20 +00:00
Reaction.shout == Shout.id,
2024-03-06 10:43:30 +00:00
Reaction.kind == ReactionKind.COMMENT.value,
Reaction.deleted_at.is_(None),
),
)
.group_by(Shout.id)
.subquery()
2024-02-22 23:49:34 +00:00
)
2024-08-07 11:02:36 +00:00
# Запрос для суммирования количества комментариев по теме
2025-05-29 09:37:39 +00:00
q = select(func.coalesce(func.sum(sub_comments.c.comments_count), 0)).filter(ShoutTopic.topic == topic_id)
2024-04-23 11:31:34 +00:00
q = q.outerjoin(sub_comments, ShoutTopic.shout == sub_comments.c.shout_id)
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-04-09 16:38:02 +00:00
2024-04-25 09:07:30 +00:00
2024-08-07 11:02:36 +00:00
def get_author_shouts_stat(author_id: int) -> int:
"""
2025-02-03 20:16:50 +00:00
Получает количество опубликованных постов для автора
2024-08-07 11:02:36 +00:00
"""
2024-04-23 11:31:34 +00:00
aliased_shout_author = aliased(ShoutAuthor)
2024-05-06 16:27:51 +00:00
aliased_shout = aliased(Shout)
q = (
select(func.count(distinct(aliased_shout.id)))
.select_from(aliased_shout)
.join(aliased_shout_author, aliased_shout.id == aliased_shout_author.shout)
.filter(
and_(
aliased_shout_author.author == author_id,
2024-05-06 16:40:51 +00:00
aliased_shout.published_at.is_not(None),
aliased_shout.deleted_at.is_(None),
2024-05-06 16:27:51 +00:00
)
)
2024-04-23 11:31:34 +00:00
)
2024-05-06 16:27:51 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-02-22 23:49:34 +00:00
2024-08-07 11:02:36 +00:00
def get_author_authors_stat(author_id: int) -> int:
"""
Получает количество уникальных авторов, с которыми взаимодействовал указанный автор
2024-08-07 11:02:36 +00:00
"""
q = (
select(func.count(distinct(ShoutAuthor.author)))
.select_from(ShoutAuthor)
.join(Shout, ShoutAuthor.shout == Shout.id)
.join(Reaction, Reaction.shout == Shout.id)
.filter(
and_(
Reaction.created_by == author_id,
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
Reaction.deleted_at.is_(None),
)
2024-04-23 11:31:34 +00:00
)
2024-03-06 09:25:55 +00:00
)
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-03-01 06:56:36 +00:00
2024-08-07 11:02:36 +00:00
def get_author_followers_stat(author_id: int) -> int:
"""
Получает количество подписчиков для указанного автора
2024-08-07 11:02:36 +00:00
"""
q = select(func.count(AuthorFollower.follower)).filter(AuthorFollower.author == author_id)
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
return int(result) if result else 0
2024-03-01 06:56:36 +00:00
2024-04-23 11:31:34 +00:00
def get_author_comments_stat(author_id: int) -> int:
2024-10-15 08:12:09 +00:00
q = (
select(func.coalesce(func.count(Reaction.id), 0).label("comments_count"))
.select_from(Author)
2024-03-28 20:33:56 +00:00
.outerjoin(
Reaction,
and_(
Reaction.created_by == Author.id,
Reaction.kind == ReactionKind.COMMENT.value,
Reaction.deleted_at.is_(None),
),
2024-03-28 20:19:07 +00:00
)
2024-10-15 08:12:09 +00:00
.where(Author.id == author_id)
2024-03-28 20:33:56 +00:00
.group_by(Author.id)
)
2024-10-15 08:12:09 +00:00
2024-05-25 23:17:45 +00:00
with local_session() as session:
result = session.execute(q).scalar()
if result and hasattr(result, "comments_count"):
return int(result.comments_count)
return 0
2024-02-24 16:12:35 +00:00
def get_with_stat(q: QueryType) -> list[Any]:
2024-08-07 11:02:36 +00:00
"""
Выполняет запрос с добавлением статистики.
:param q: SQL-запрос для выполнения.
:return: Список объектов с добавленной статистикой.
"""
2024-04-09 19:37:58 +00:00
records = []
2024-03-12 11:59:36 +00:00
try:
with local_session() as session:
2024-08-07 11:02:36 +00:00
# Определяем, является ли запрос запросом авторов
2024-05-30 11:40:04 +00:00
author_prefixes = ("select author", "select * from author")
is_author = f"{q}".lower().startswith(author_prefixes)
2024-05-18 13:16:09 +00:00
2024-08-07 11:02:36 +00:00
# Добавляем колонки статистики в запрос
2024-05-18 12:40:15 +00:00
q = add_author_stat_columns(q) if is_author else add_topic_stat_columns(q)
2024-04-23 11:31:34 +00:00
2024-08-07 11:02:36 +00:00
# Выполняем запрос
2025-05-16 06:23:48 +00:00
result = session.execute(q).unique()
2024-04-24 07:30:32 +00:00
for cols in result:
entity = cols[0]
stat = {}
2024-08-07 11:18:05 +00:00
stat["shouts"] = cols[1] # Статистика по публикациям
2024-08-07 11:02:36 +00:00
stat["followers"] = cols[2] # Статистика по подписчикам
2024-04-24 07:30:32 +00:00
if is_author:
2025-05-29 15:56:55 +00:00
# Дополнительная проверка типа entity.id
2025-05-30 11:08:29 +00:00
if not hasattr(entity, "id"):
2025-05-29 15:56:55 +00:00
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
2025-05-30 11:08:29 +00:00
2025-05-29 15:56:55 +00:00
stat["authors"] = get_author_authors_stat(entity_id) # Статистика по подпискам на авторов
stat["comments"] = get_author_comments_stat(entity_id) # Статистика по комментариям
2024-04-26 22:41:47 +00:00
else:
2025-05-29 15:56:55 +00:00
# Дополнительная проверка типа entity.id для тем
2025-05-30 11:08:29 +00:00
if not hasattr(entity, "id"):
2025-05-29 15:56:55 +00:00
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
2025-05-30 11:08:29 +00:00
2025-05-29 15:56:55 +00:00
stat["authors"] = get_topic_authors_stat(entity_id) # Статистика по авторам темы
2024-04-24 07:30:32 +00:00
entity.stat = stat
records.append(entity)
2024-03-12 11:59:36 +00:00
except Exception as exc:
2024-05-06 17:00:26 +00:00
import traceback
2024-05-18 11:15:05 +00:00
logger.debug(q)
2024-05-06 17:00:26 +00:00
traceback.print_exc()
2024-04-23 12:14:59 +00:00
logger.error(exc, exc_info=True)
2024-02-22 23:08:43 +00:00
return records
2024-02-21 17:12:47 +00:00
def author_follows_authors(author_id: int) -> list[Any]:
2024-08-07 11:02:36 +00:00
"""
Получает список авторов, на которых подписан указанный автор.
:param author_id: Идентификатор автора.
:return: Список авторов с добавленной статистикой.
"""
2024-04-17 15:32:23 +00:00
af = aliased(AuthorFollower, name="af")
2024-05-18 11:15:05 +00:00
author_follows_authors_query = (
2024-05-30 04:12:00 +00:00
select(Author).select_from(join(Author, af, Author.id == af.author)).where(af.follower == author_id)
2024-02-23 20:15:16 +00:00
)
2024-05-18 11:15:05 +00:00
return get_with_stat(author_follows_authors_query)
2024-02-23 18:10:11 +00:00
2024-02-23 19:14:08 +00:00
def author_follows_topics(author_id: int) -> list[Any]:
2024-08-07 11:02:36 +00:00
"""
Получает список тем, на которые подписан указанный автор.
:param author_id: Идентификатор автора.
:return: Список тем с добавленной статистикой.
"""
2024-05-18 11:15:05 +00:00
author_follows_topics_query = (
2024-02-24 18:45:38 +00:00
select(Topic)
.select_from(join(Topic, TopicFollower, Topic.id == TopicFollower.topic))
.where(TopicFollower.follower == author_id)
2024-02-23 20:15:16 +00:00
)
2024-05-18 11:15:05 +00:00
return get_with_stat(author_follows_topics_query)
2024-04-09 13:43:06 +00:00
def update_author_stat(author_id: int) -> None:
2024-08-07 11:02:36 +00:00
"""
Обновляет статистику для указанного автора и сохраняет её в кэше.
:param author_id: Идентификатор автора.
"""
2024-05-18 11:15:05 +00:00
author_query = select(Author).where(Author.id == author_id)
2024-05-06 19:37:38 +00:00
try:
2024-06-11 14:51:34 +00:00
result = get_with_stat(author_query)
2024-06-11 19:46:35 +00:00
if result:
2024-06-11 14:51:34 +00:00
author_with_stat = result[0]
if isinstance(author_with_stat, Author):
author_dict = author_with_stat.dict()
2024-08-07 11:02:36 +00:00
# Асинхронное кэширование данных автора
task = asyncio.create_task(cache_author(author_dict))
# Store task reference to prevent garbage collection
if not hasattr(update_author_stat, "_background_tasks"):
update_author_stat._background_tasks = set() # type: ignore[attr-defined]
update_author_stat._background_tasks.add(task) # type: ignore[attr-defined]
task.add_done_callback(update_author_stat._background_tasks.discard) # type: ignore[attr-defined]
2024-05-06 19:37:38 +00:00
except Exception as exc:
logger.error(exc, exc_info=True)
def get_followers_count(entity_type: str, entity_id: int) -> int:
"""Получает количество подписчиков для сущности"""
try:
with local_session() as session:
if entity_type == "topic":
result = (
session.query(func.count(TopicFollower.follower)).filter(TopicFollower.topic == entity_id).scalar()
)
elif entity_type == "author":
# Count followers of this author
result = (
session.query(func.count(AuthorFollower.follower))
.filter(AuthorFollower.author == entity_id)
.scalar()
)
elif entity_type == "community":
result = (
session.query(func.count(CommunityFollower.follower))
.filter(CommunityFollower.community == entity_id)
.scalar()
)
else:
return 0
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting followers count: {e}")
return 0
def get_following_count(entity_type: str, entity_id: int) -> int:
"""Получает количество подписок сущности"""
try:
with local_session() as session:
if entity_type == "author":
# Count what this author follows
topic_follows = (
session.query(func.count(TopicFollower.topic)).filter(TopicFollower.follower == entity_id).scalar()
or 0
)
community_follows = (
session.query(func.count(CommunityFollower.community))
.filter(CommunityFollower.follower == entity_id)
.scalar()
or 0
)
return int(topic_follows) + int(community_follows)
return 0
except Exception as e:
logger.error(f"Error getting following count: {e}")
return 0
def get_shouts_count(
author_id: Optional[int] = None, topic_id: Optional[int] = None, community_id: Optional[int] = None
) -> int:
"""Получает количество публикаций"""
try:
with local_session() as session:
query = session.query(func.count(Shout.id)).filter(Shout.published_at.isnot(None))
if author_id:
query = query.filter(Shout.created_by == author_id)
if topic_id:
# This would need ShoutTopic association table
pass
if community_id:
query = query.filter(Shout.community == community_id)
result = query.scalar()
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting shouts count: {e}")
return 0
def get_authors_count(community_id: Optional[int] = None) -> int:
"""Получает количество авторов"""
try:
with local_session() as session:
if community_id:
# Count authors in specific community
result = (
session.query(func.count(distinct(CommunityFollower.follower)))
.filter(CommunityFollower.community == community_id)
.scalar()
)
else:
# Count all authors
result = session.query(func.count(Author.id)).filter(Author.deleted == False).scalar()
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting authors count: {e}")
return 0
def get_topics_count(author_id: Optional[int] = None) -> int:
"""Получает количество топиков"""
try:
with local_session() as session:
if author_id:
# Count topics followed by author
result = (
session.query(func.count(TopicFollower.topic)).filter(TopicFollower.follower == author_id).scalar()
)
else:
# Count all topics
result = session.query(func.count(Topic.id)).scalar()
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting topics count: {e}")
return 0
def get_communities_count() -> int:
"""Получает количество сообществ"""
try:
with local_session() as session:
result = session.query(func.count(Community.id)).scalar()
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting communities count: {e}")
return 0
def get_reactions_count(shout_id: Optional[int] = None, author_id: Optional[int] = None) -> int:
"""Получает количество реакций"""
try:
from orm.reaction import Reaction
with local_session() as session:
query = session.query(func.count(Reaction.id))
if shout_id:
query = query.filter(Reaction.shout == shout_id)
if author_id:
query = query.filter(Reaction.created_by == author_id)
result = query.scalar()
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting reactions count: {e}")
return 0
def get_comments_count_by_shout(shout_id: int) -> int:
"""Получает количество комментариев к статье"""
try:
from orm.reaction import Reaction
with local_session() as session:
# Using text() to access 'kind' column which might be enum
result = (
session.query(func.count(Reaction.id))
.filter(
and_(
Reaction.shout == shout_id,
Reaction.kind == "comment", # Assuming 'comment' is a valid enum value
)
)
.scalar()
)
return int(result) if result else 0
except Exception as e:
logger.error(f"Error getting comments count: {e}")
return 0
async def get_stat_background_task() -> None:
"""Фоновая задача для обновления статистики"""
try:
if not hasattr(sys.modules[__name__], "_background_tasks"):
sys.modules[__name__]._background_tasks = set() # type: ignore[attr-defined]
# Perform background statistics calculations
logger.info("Running background statistics update")
# Here you would implement actual background statistics updates
# This is just a placeholder
except Exception as e:
logger.error(f"Error in background statistics task: {e}")