2024-02-24 18:45:38 +00:00
|
|
|
from sqlalchemy import func, distinct, select, join
|
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-24 18:45:38 +00:00
|
|
|
from resolvers.rating import load_author_ratings
|
2024-02-21 17:12:47 +00:00
|
|
|
from services.db import local_session
|
2024-02-24 18:45:38 +00:00
|
|
|
from orm.author import AuthorFollower, Author
|
|
|
|
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 19:43:50 +00:00
|
|
|
aliased_shout_author = aliased(ShoutAuthor)
|
|
|
|
aliased_topic_follower = aliased(TopicFollower)
|
|
|
|
aliased_shout_topic = aliased(ShoutTopic)
|
|
|
|
|
2024-02-22 23:49:34 +00:00
|
|
|
q = (
|
2024-02-23 20:15:16 +00:00
|
|
|
q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
|
2024-02-24 18:45:38 +00:00
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat')
|
|
|
|
)
|
|
|
|
.outerjoin(
|
|
|
|
aliased_shout_author,
|
|
|
|
aliased_shout_topic.shout == aliased_shout_author.shout,
|
|
|
|
)
|
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
|
|
|
|
)
|
2024-02-23 19:43:50 +00:00
|
|
|
.outerjoin(aliased_topic_follower)
|
2024-02-24 18:45:38 +00:00
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_topic_follower.follower)).label(
|
|
|
|
'followers_stat'
|
|
|
|
)
|
|
|
|
)
|
2024-02-22 23:49:34 +00:00
|
|
|
)
|
|
|
|
|
2024-02-23 19:43:50 +00:00
|
|
|
q = q.group_by(Topic.id)
|
2024-02-22 23:49:34 +00:00
|
|
|
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
|
|
|
def add_author_stat_columns(q):
|
2024-02-23 19:43:50 +00:00
|
|
|
aliased_shout_author = aliased(ShoutAuthor)
|
|
|
|
aliased_author_authors = aliased(AuthorFollower)
|
|
|
|
aliased_author_followers = aliased(AuthorFollower)
|
|
|
|
|
2024-02-22 23:49:34 +00:00
|
|
|
q = (
|
2024-02-23 19:43:50 +00:00
|
|
|
q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id)
|
2024-02-24 18:45:38 +00:00
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_shout_author.shout)).label('shouts_stat')
|
2024-02-24 16:23:53 +00:00
|
|
|
)
|
2024-02-24 18:45:38 +00:00
|
|
|
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
|
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
|
2024-02-24 16:23:53 +00:00
|
|
|
)
|
2024-02-24 18:45:38 +00:00
|
|
|
.outerjoin(
|
|
|
|
aliased_author_followers, aliased_author_followers.author == Author.id
|
2024-02-24 16:23:53 +00:00
|
|
|
)
|
2024-02-24 18:45:38 +00:00
|
|
|
.add_columns(
|
|
|
|
func.count(distinct(aliased_author_followers.follower)).label(
|
|
|
|
'followers_stat'
|
2024-02-24 16:23:53 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-02-24 18:45:38 +00:00
|
|
|
q = q.group_by(Author.id)
|
2024-02-24 16:23:53 +00:00
|
|
|
|
2024-02-24 18:45:38 +00:00
|
|
|
return q
|
2024-02-24 16:12:35 +00:00
|
|
|
|
|
|
|
|
2024-02-25 08:27:08 +00:00
|
|
|
def get_with_stat(q):
|
|
|
|
q = add_author_stat_columns(q)
|
2024-02-22 23:08:43 +00:00
|
|
|
records = []
|
2024-02-25 08:27:08 +00:00
|
|
|
logger.debug(q.replace('\n', ' '))
|
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-25 08:27:08 +00:00
|
|
|
if q.startswith('SELECT author'):
|
|
|
|
load_author_ratings(session, entity)
|
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-23 18:10:11 +00:00
|
|
|
def author_follows_authors(author_id: int):
|
2024-02-24 18:45:38 +00:00
|
|
|
af = aliased(AuthorFollower, name='af')
|
2024-02-23 20:15:16 +00:00
|
|
|
q = (
|
2024-02-24 18:45:38 +00:00
|
|
|
select(Author)
|
2024-02-24 18:56:09 +00:00
|
|
|
.select_from(join(Author, af, Author.id == af.author))
|
2024-02-24 18:45:38 +00:00
|
|
|
.where(af.follower == author_id)
|
2024-02-23 20:15:16 +00:00
|
|
|
)
|
2024-02-25 08:27:08 +00:00
|
|
|
return get_with_stat(q)
|
2024-02-23 18:10:11 +00:00
|
|
|
|
2024-02-23 19:14:08 +00:00
|
|
|
|
2024-02-23 18:10:11 +00:00
|
|
|
def author_follows_topics(author_id: int):
|
2024-02-23 20:15:16 +00:00
|
|
|
q = (
|
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-02-25 08:27:08 +00:00
|
|
|
return get_with_stat(q)
|