This commit is contained in:
parent
4fe15d1440
commit
c6a4f04779
|
@ -10,43 +10,39 @@ from services.cache import cache_author
|
||||||
|
|
||||||
|
|
||||||
def add_topic_stat_columns(q):
|
def add_topic_stat_columns(q):
|
||||||
aliased_shout_author = aliased(ShoutAuthor)
|
|
||||||
aliased_topic_follower = aliased(TopicFollower)
|
|
||||||
aliased_shout_topic = aliased(ShoutTopic)
|
aliased_shout_topic = aliased(ShoutTopic)
|
||||||
|
aliased_authors = aliased(ShoutAuthor)
|
||||||
|
aliased_followers = aliased(TopicFollower)
|
||||||
aliased_shout = aliased(Shout)
|
aliased_shout = aliased(Shout)
|
||||||
|
|
||||||
q = (
|
q = q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
|
||||||
q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
|
q = q.add_columns(
|
||||||
.add_columns(
|
func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat')
|
||||||
func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat')
|
|
||||||
)
|
|
||||||
.outerjoin(
|
|
||||||
aliased_shout_author,
|
|
||||||
and_(
|
|
||||||
aliased_shout_topic.shout == aliased_shout_author.shout,
|
|
||||||
aliased_shout.published_at.is_not(None),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.outerjoin(
|
|
||||||
aliased_shout_author,
|
|
||||||
aliased_shout_topic.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.outerjoin(aliased_shout, and_(
|
||||||
|
aliased_shout.id == aliased_shout_topic.shout,
|
||||||
|
aliased_shout.published_at.is_not(None),
|
||||||
|
aliased_shout.deleted_at.is_(None)
|
||||||
|
))
|
||||||
|
q = q.outerjoin(aliased_authors, aliased_shout.authors.contains(aliased_authors.author))
|
||||||
|
q = q.add_columns(
|
||||||
|
func.count(distinct(aliased_authors.author)).label('authors_stat')
|
||||||
|
)
|
||||||
|
|
||||||
|
q = q.outerjoin(aliased_followers, aliased_followers.topic == Topic.id)
|
||||||
|
q = q.add_columns(
|
||||||
|
func.count(distinct(aliased_followers.follower)).label('followers_stat')
|
||||||
|
)
|
||||||
|
|
||||||
# Create a subquery for comments count
|
# Create a subquery for comments count
|
||||||
_sub_comments = (
|
sub_comments = (
|
||||||
select(
|
select(
|
||||||
Shout.id, func.coalesce(func.count(Reaction.id), 0).label('comments_count')
|
Shout.id.label('shout_id'), func.coalesce(func.count(Reaction.id)).label('comments_count')
|
||||||
)
|
)
|
||||||
.join(
|
.join(ShoutTopic, Topic.id == ShoutTopic.topic)
|
||||||
|
.join(Shout, ShoutTopic.shout == Shout.id)
|
||||||
|
.outerjoin(
|
||||||
Reaction,
|
Reaction,
|
||||||
and_(
|
and_(
|
||||||
Reaction.shout == Shout.id,
|
Reaction.shout == Shout.id,
|
||||||
|
@ -58,12 +54,12 @@ def add_topic_stat_columns(q):
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
|
|
||||||
# q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.id)
|
q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.shout_id)
|
||||||
# q = q.add_columns(
|
q = q.add_columns(func.coalesce(sub_comments.c.comments_count, 0).label('comments_stat'))
|
||||||
# func.coalesce(func.sum(sub_comments.c.comments_count), 0).label('comments_stat')
|
|
||||||
# )
|
|
||||||
|
|
||||||
q = q.group_by(Topic.id)
|
group_list = [Topic.id, sub_comments.c.comments_count]
|
||||||
|
|
||||||
|
q = q.group_by(*group_list)
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -107,7 +103,7 @@ def add_author_stat_columns(q):
|
||||||
|
|
||||||
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
|
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
|
||||||
q = q.add_columns(sub_comments.c.comments_count)
|
q = q.add_columns(sub_comments.c.comments_count)
|
||||||
group_list = [Author.id, sub_comments.c.comments_count]
|
group_list = [Topic.id, sub_comments.c.comments_count]
|
||||||
|
|
||||||
q = q.group_by(*group_list)
|
q = q.group_by(*group_list)
|
||||||
|
|
||||||
|
|
|
@ -49,9 +49,10 @@ async def get_topics_by_author(_, _info, author_id=0, slug='', user=''):
|
||||||
@query.field('get_topic')
|
@query.field('get_topic')
|
||||||
def get_topic(_, _info, slug: str):
|
def get_topic(_, _info, slug: str):
|
||||||
q = select(Topic).filter(Topic.slug == slug)
|
q = select(Topic).filter(Topic.slug == slug)
|
||||||
topics = get_with_stat(q)
|
result = get_with_stat(q)
|
||||||
if topics:
|
if result:
|
||||||
return topics[0]
|
[topic] = result
|
||||||
|
return topic
|
||||||
|
|
||||||
|
|
||||||
@mutation.field('create_topic')
|
@mutation.field('create_topic')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user