core/resolvers/stat.py

249 lines
7.5 KiB
Python
Raw Normal View History

2024-02-23 16:35:40 +00:00
from sqlalchemy import func, select, distinct, alias
2024-02-22 23:49:34 +00:00
from sqlalchemy.orm import aliased
2024-02-21 17:12:47 +00:00
2024-02-22 23:49:34 +00:00
from orm.topic import TopicFollower, Topic
2024-02-21 17:12:47 +00:00
from services.db import local_session
2024-02-22 23:49:34 +00:00
from orm.author import AuthorFollower, Author
2024-02-22 23:08:43 +00:00
from orm.shout import ShoutTopic, ShoutAuthor
2024-02-23 18:10:11 +00:00
from services.logger import root_logger as logger
2024-02-21 17:12:47 +00:00
2024-02-22 23:49:34 +00:00
def add_topic_stat_columns(q):
2024-02-23 00:28:46 +00:00
aliased_shout_authors = aliased(ShoutAuthor)
aliased_topic_followers = aliased(TopicFollower)
aliased_topic = aliased(Topic)
2024-02-22 23:49:34 +00:00
q = (
2024-02-23 11:09:12 +00:00
q.outerjoin(ShoutTopic, aliased_topic.id == ShoutTopic.topic)
2024-02-23 11:40:38 +00:00
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
2024-02-23 16:35:40 +00:00
.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'
)
)
2024-02-22 23:49:34 +00:00
)
2024-02-23 00:28:46 +00:00
q = q.group_by(aliased_topic.id)
2024-02-22 23:49:34 +00:00
return q
def add_author_stat_columns(q):
2024-02-23 11:05:46 +00:00
aliased_author_authors = aliased(AuthorFollower, name='af_authors')
2024-02-23 16:35:40 +00:00
aliased_author_followers = aliased(
AuthorFollower, name='af_followers'
) # Добавлен второй псевдоним
2024-02-23 00:28:46 +00:00
aliased_author = aliased(Author)
2024-02-22 23:49:34 +00:00
q = (
2024-02-23 00:28:46 +00:00
q.outerjoin(ShoutAuthor, aliased_author.id == ShoutAuthor.author)
2024-02-23 11:40:38 +00:00
.add_columns(func.count(distinct(ShoutAuthor.shout)).label('shouts_stat'))
2024-02-23 16:35:40 +00:00
.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'
)
) # Используется второй псевдоним
2024-02-21 17:12:47 +00:00
)
2024-02-22 23:49:34 +00:00
2024-02-23 00:28:46 +00:00
q = q.group_by(aliased_author.id)
2024-02-22 23:49:34 +00:00
2024-02-21 17:12:47 +00:00
return q
2024-02-23 01:08:29 +00:00
def execute_with_ministat(q):
2024-02-22 23:08:43 +00:00
records = []
2024-02-21 17:12:47 +00:00
with local_session() as session:
2024-02-23 11:40:38 +00:00
for [entity, shouts_stat, authors_stat, followers_stat] in session.execute(q):
entity.stat = {
2024-02-21 17:12:47 +00:00
'shouts': shouts_stat,
2024-02-22 23:08:43 +00:00
'authors': authors_stat,
2024-02-23 16:35:40 +00:00
'followers': followers_stat,
2024-02-21 17:12:47 +00:00
}
2024-02-23 11:40:38 +00:00
records.append(entity)
2024-02-21 17:12:47 +00:00
2024-02-22 23:08:43 +00:00
return records
2024-02-21 17:12:47 +00:00
2024-02-22 23:49:34 +00:00
def get_authors_with_stat(q):
q = add_author_stat_columns(q)
2024-02-23 01:08:29 +00:00
return execute_with_ministat(q)
2024-02-22 23:49:34 +00:00
def get_topics_with_stat(q):
q = add_topic_stat_columns(q)
2024-02-23 01:08:29 +00:00
return execute_with_ministat(q)
2024-02-23 16:35:40 +00:00
2024-02-23 18:10:11 +00:00
def author_follows_authors(author_id: int):
aliased_shout_authors = aliased(ShoutAuthor)
2024-02-23 16:35:40 +00:00
subquery_shout_author = (
select(
[
2024-02-23 18:10:11 +00:00
aliased_shout_authors.author,
func.count(distinct(aliased_shout_authors.shout)).label('shouts_stat'),
2024-02-23 16:35:40 +00:00
]
)
2024-02-23 18:10:11 +00:00
.group_by(aliased_shout_authors.author)
.where(aliased_shout_authors.author == author_id)
2024-02-23 16:35:40 +00:00
.alias()
)
2024-02-23 18:10:11 +00:00
alias_author_authors = aliased(AuthorFollower)
2024-02-23 17:25:52 +00:00
subquery_author_authors = (
2024-02-23 16:35:40 +00:00
select(
[
2024-02-23 18:10:11 +00:00
alias_author_authors.author,
func.count(distinct(alias_author_authors.author)).label('authors_stat'),
2024-02-23 16:35:40 +00:00
]
)
2024-02-23 18:10:11 +00:00
.group_by(alias_author_authors.author)
.where(alias_author_authors.follower == author_id)
2024-02-23 16:35:40 +00:00
.alias()
)
2024-02-23 18:10:11 +00:00
alias_author_followers = aliased(AuthorFollower)
2024-02-23 16:35:40 +00:00
subquery_author_followers = (
select(
[
2024-02-23 18:10:11 +00:00
alias_author_followers.follower,
func.count(distinct(alias_author_followers.follower)).label('followers_stat'),
2024-02-23 16:35:40 +00:00
]
)
2024-02-23 18:10:11 +00:00
.group_by(alias_author_followers.follower)
.where(alias_author_followers.author == author_id)
2024-02-23 16:35:40 +00:00
.alias()
)
subq_shout_author_alias = alias(subquery_shout_author)
subq_author_followers_alias = alias(
2024-02-23 17:25:52 +00:00
subquery_author_authors, name='subq_author_followers'
2024-02-23 16:35:40 +00:00
)
subq_author_authors_alias = alias(
subquery_author_followers, name='subq_author_authors'
)
authors_query = (
select(
[
2024-02-23 18:10:11 +00:00
Author,
2024-02-23 17:25:52 +00:00
subq_shout_author_alias.shouts_stat,
subq_author_authors_alias.authors_stat,
subq_author_followers_alias.followers_stat,
2024-02-23 16:35:40 +00:00
]
)
.select_from(Author)
.outerjoin(
2024-02-23 17:25:52 +00:00
subq_shout_author_alias, Author.id == subq_shout_author_alias.author
2024-02-23 16:35:40 +00:00
)
.outerjoin(
2024-02-23 17:25:52 +00:00
subq_author_authors_alias, Author.id == subq_author_followers_alias.author
2024-02-23 16:35:40 +00:00
)
.outerjoin(
subq_author_followers_alias,
2024-02-23 17:25:52 +00:00
Author.id == subq_author_followers_alias.follower,
2024-02-23 16:35:40 +00:00
)
)
authors = execute_with_ministat(authors_query)
2024-02-23 18:10:11 +00:00
return authors
def author_follows_topics(author_id: int):
2024-02-23 16:35:40 +00:00
subquery_shout_topic = (
select(
[
ShoutTopic.topic,
func.count(distinct(ShoutTopic.shout)).label('shouts_stat'),
]
)
.group_by(ShoutTopic.topic)
.alias()
)
subquery_shout_topic_authors = (
select(
[
ShoutTopic.topic,
func.count(distinct(ShoutTopic.author)).label('authors_stat'),
]
)
.group_by(ShoutTopic.topic)
.alias()
)
subquery_topic_followers = (
select(
[
TopicFollower.topic,
func.count(distinct(TopicFollower.follower)).label('followers_stat'),
]
)
.group_by(TopicFollower.topic_id)
.alias()
)
subq_shout_topic_alias = alias(subquery_shout_topic)
subq_shout_topic_authors_alias = alias(
subquery_shout_topic_authors, name='subq_shout_topic_authors'
)
subq_topic_followers_alias = alias(
subquery_topic_followers, name='subq_topic_followers'
)
topics_query = (
select(
[
2024-02-23 18:10:11 +00:00
Topic,
2024-02-23 16:35:40 +00:00
subq_shout_topic_alias.columns.shouts_stat,
subq_shout_topic_authors_alias.columns.authors_stat,
subq_topic_followers_alias.columns.followers_stat,
]
)
.select_from(Topic)
.outerjoin(
2024-02-23 17:25:52 +00:00
subq_shout_topic_alias, Topic.id == subq_shout_topic_alias.columns.topic
2024-02-23 16:35:40 +00:00
)
.outerjoin(
subq_shout_topic_authors_alias,
2024-02-23 17:25:52 +00:00
Topic.id == subq_shout_topic_authors_alias.columns.topic,
2024-02-23 16:35:40 +00:00
)
.outerjoin(
subq_topic_followers_alias,
2024-02-23 17:25:52 +00:00
Topic.id == subq_topic_followers_alias.columns.topic,
2024-02-23 16:35:40 +00:00
)
)
topics = execute_with_ministat(topics_query)
2024-02-23 18:10:11 +00:00
return topics
2024-02-23 16:35:40 +00:00
2024-02-23 18:10:11 +00:00
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