2024-02-21 17:12:47 +00:00
|
|
|
from sqlalchemy import func, distinct
|
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-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 00:28:46 +00:00
|
|
|
q.outerjoin(ShoutTopic, aliased_topic.id == ShoutTopic.topic)
|
2024-02-22 23:49:34 +00:00
|
|
|
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
|
2024-02-23 00:28:46 +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_followers.topic == aliased_topic.id)
|
|
|
|
.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):
|
|
|
|
aliased_author_followers = aliased(AuthorFollower)
|
|
|
|
aliased_author_authors = aliased(AuthorFollower)
|
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-22 23:49:34 +00:00
|
|
|
.add_columns(func.count(distinct(ShoutAuthor.shout)).label('shouts_stat'))
|
2024-02-23 00:28:46 +00:00
|
|
|
.outerjoin(aliased_author_authors, AuthorFollower.follower == aliased_author.id)
|
2024-02-22 23:49:34 +00:00
|
|
|
.add_columns(func.count(distinct(aliased_author_authors.author)).label('authors_stat'))
|
2024-02-23 00:28:46 +00:00
|
|
|
.outerjoin(aliased_author_followers, AuthorFollower.author == aliased_author.id)
|
2024-02-22 23:49:34 +00:00
|
|
|
.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-22 23:08:43 +00:00
|
|
|
def unpack_stat(q):
|
|
|
|
records = []
|
2024-02-21 17:12:47 +00:00
|
|
|
with local_session() as session:
|
2024-02-22 23:08:43 +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,
|
|
|
|
'followers': followers_stat
|
2024-02-21 17:12:47 +00:00
|
|
|
}
|
2024-02-22 23:08:43 +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)
|
|
|
|
return unpack_stat(q)
|
|
|
|
|
|
|
|
|
|
|
|
def get_topics_with_stat(q):
|
|
|
|
q = add_topic_stat_columns(q)
|
2024-02-22 23:08:43 +00:00
|
|
|
return unpack_stat(q)
|