This commit is contained in:
@@ -1,81 +1,36 @@
|
||||
from sqlalchemy import func, distinct
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from orm.author import Author, AuthorFollower
|
||||
from orm.shout import ShoutAuthor, ShoutTopic
|
||||
from orm.topic import Topic, TopicFollower
|
||||
from services.db import local_session
|
||||
# from services.viewed import ViewedStorage
|
||||
from orm.author import AuthorFollower
|
||||
from orm.shout import ShoutTopic, ShoutAuthor
|
||||
|
||||
|
||||
def add_author_stat_columns(q):
|
||||
shout_author_aliased = aliased(ShoutAuthor)
|
||||
q = q.outerjoin(shout_author_aliased).add_columns(
|
||||
func.count(distinct(shout_author_aliased.shout)).label('shouts_stat')
|
||||
)
|
||||
|
||||
authors_table = aliased(AuthorFollower)
|
||||
def add_stat_columns(q, author_alias, follower_model_alias):
|
||||
shouts_stat_model = ShoutAuthor if isinstance(follower_model_alias, AuthorFollower) else ShoutTopic
|
||||
q = q.outerjoin(shouts_stat_model).add_columns(func.count(distinct(shouts_stat_model.shout)).label('shouts_stat'))
|
||||
q = q.outerjoin(
|
||||
authors_table, authors_table.follower == Author.id
|
||||
).add_columns(func.count(distinct(authors_table.author)).label('authors_stat'))
|
||||
|
||||
followers_table = aliased(AuthorFollower)
|
||||
q = q.outerjoin(followers_table, followers_table.author == Author.id).add_columns(
|
||||
func.count(distinct(followers_table.follower)).label('followers_stat')
|
||||
follower_model_alias, follower_model_alias.follower == author_alias.id
|
||||
).add_columns(func.count(distinct(follower_model_alias.author)).label('authors_stat'))
|
||||
q = q.outerjoin(follower_model_alias, follower_model_alias.author == author_alias.id).add_columns(
|
||||
func.count(distinct(follower_model_alias.follower)).label('followers_stat')
|
||||
)
|
||||
|
||||
q = q.group_by(Author.id)
|
||||
return q
|
||||
|
||||
|
||||
async def get_authors_from_query(q):
|
||||
authors = []
|
||||
def unpack_stat(q):
|
||||
records = []
|
||||
with local_session() as session:
|
||||
for [author, shouts_stat, authors_stat, followers_stat] in session.execute(q):
|
||||
author.stat = {
|
||||
'shouts': shouts_stat,
|
||||
'followers': followers_stat,
|
||||
'followings': authors_stat,
|
||||
# viewed
|
||||
}
|
||||
authors.append(author)
|
||||
return authors
|
||||
|
||||
|
||||
def add_topic_stat_columns(q):
|
||||
aliased_shout_author = aliased(ShoutAuthor)
|
||||
aliased_topic_follower = aliased(TopicFollower)
|
||||
|
||||
q = (
|
||||
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
|
||||
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
|
||||
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
|
||||
)
|
||||
.outerjoin(aliased_topic_follower)
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_topic_follower.follower)).label(
|
||||
'followers_stat'
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
q = q.group_by(Topic.id)
|
||||
|
||||
return q
|
||||
|
||||
|
||||
async def get_topics_from_query(q):
|
||||
topics = []
|
||||
with local_session() as session:
|
||||
for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(q):
|
||||
topic.stat = {
|
||||
for [entity, shouts_stat, authors_stat, followers_stat] in session.execute(q):
|
||||
entity.stat = {
|
||||
'shouts': shouts_stat,
|
||||
'authors': authors_stat,
|
||||
'followers': followers_stat,
|
||||
# 'viewed': await ViewedStorage.get_topic(topic.slug),
|
||||
'followers': followers_stat
|
||||
}
|
||||
topics.append(topic)
|
||||
records.append(entity)
|
||||
|
||||
return topics
|
||||
return records
|
||||
|
||||
|
||||
def get_with_stat(q, author_alias, follower_model_alias):
|
||||
q = add_stat_columns(q, author_alias, follower_model_alias)
|
||||
return unpack_stat(q)
|
||||
|
Reference in New Issue
Block a user