core/resolvers/stat.py
Untone fbbc408df6
All checks were successful
Deploy on push / deploy (push) Successful in 1m27s
clean
2024-02-23 22:24:48 +03:00

116 lines
3.5 KiB
Python

from sqlalchemy import func, distinct
from sqlalchemy.orm import aliased
from orm.topic import TopicFollower, Topic
from services.db import local_session
from orm.author import AuthorFollower, Author
from orm.shout import ShoutTopic, ShoutAuthor
from services.logger import root_logger as logger
def add_topic_stat_columns(q):
aliased_shout_authors = aliased(ShoutAuthor)
aliased_topic_followers = aliased(TopicFollower)
aliased_topic = aliased(Topic)
q = (
q.outerjoin(ShoutTopic, aliased_topic.id == ShoutTopic.topic)
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
.outerjoin(
aliased_shout_authors, ShoutTopic.shout == aliased_shout_authors.shout
)
.add_columns(
func.count(distinct(aliased_shout_authors.author)).label('authors_stat')
)
.outerjoin(
aliased_topic_followers, aliased_topic.id == aliased_topic_followers.topic
)
.add_columns(
func.count(distinct(aliased_topic_followers.follower)).label(
'followers_stat'
)
)
)
q = q.group_by(aliased_topic.id)
return q
def add_author_stat_columns(q):
aliased_author_authors = aliased(AuthorFollower, name='af_authors')
aliased_author_followers = aliased(
AuthorFollower, name='af_followers'
) # Добавлен второй псевдоним
aliased_author = aliased(Author)
q = (
q.outerjoin(ShoutAuthor, aliased_author.id == ShoutAuthor.author)
.add_columns(func.count(distinct(ShoutAuthor.shout)).label('shouts_stat'))
.outerjoin(
aliased_author_authors, aliased_author_authors.follower == aliased_author.id
)
.add_columns(
func.count(distinct(aliased_author_authors.author)).label('authors_stat')
)
.outerjoin(
aliased_author_followers,
aliased_author_followers.author == aliased_author.id,
) # Используется второй псевдоним
.add_columns(
func.count(distinct(aliased_author_followers.follower)).label(
'followers_stat'
)
) # Используется второй псевдоним
)
q = q.group_by(aliased_author.id)
return q
def execute_with_ministat(q):
records = []
with local_session() as session:
for [entity, shouts_stat, authors_stat, followers_stat] in session.execute(q):
entity.stat = {
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
}
records.append(entity)
return records
def get_authors_with_stat(q):
q = add_author_stat_columns(q)
return execute_with_ministat(q)
def get_topics_with_stat(q):
q = add_topic_stat_columns(q)
return execute_with_ministat(q)
def author_follows_authors(author_id: int):
authors = []
return authors
def author_follows_topics(author_id: int):
topics = []
return topics
def query_follows(author_id: int):
try:
topics = author_follows_topics(author_id)
authors = author_follows_authors(author_id)
return {
'topics': topics,
'authors': authors,
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
}
except Exception as e:
logger.exception(f"An error occurred while executing query_follows: {e}")
raise Exception("An error occurred while executing query_follows") from e