This commit is contained in:
2024-04-17 18:32:23 +03:00
parent 937b154c6b
commit c25d7e3ab6
39 changed files with 986 additions and 926 deletions

View File

@@ -18,28 +18,35 @@ def add_topic_stat_columns(q):
# shouts
q = q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
q = q.add_columns(func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat'))
q = q.add_columns(
func.count(distinct(aliased_shout_topic.shout)).label("shouts_stat")
)
# authors
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_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.any(id=aliased_authors.id))
q = q.add_columns(func.count(distinct(aliased_authors.author)).label('authors_stat'))
q = q.add_columns(
func.count(distinct(aliased_authors.author)).label("authors_stat")
)
# followers
q = q.outerjoin(aliased_followers, aliased_followers.topic == Topic.id)
q = q.add_columns(
func.count(distinct(aliased_followers.follower)).label('followers_stat')
func.count(distinct(aliased_followers.follower)).label("followers_stat")
)
# comments
sub_comments = (
select(
Shout.id.label('shout_id'),
func.coalesce(func.count(Reaction.id)).label('comments_count')
Shout.id.label("shout_id"),
func.coalesce(func.count(Reaction.id)).label("comments_count"),
)
.join(ShoutTopic, ShoutTopic.shout == Shout.id)
.join(Topic, ShoutTopic.topic == Topic.id)
@@ -55,7 +62,9 @@ def add_topic_stat_columns(q):
.subquery()
)
q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.shout_id)
q = q.add_columns(func.coalesce(sub_comments.c.comments_count, 0).label('comments_stat'))
q = q.add_columns(
func.coalesce(sub_comments.c.comments_count, 0).label("comments_stat")
)
group_list = [Topic.id, sub_comments.c.comments_count]
@@ -71,23 +80,23 @@ def add_author_stat_columns(q):
q = q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id)
q = q.add_columns(
func.count(distinct(aliased_shout_author.shout)).label('shouts_stat')
func.count(distinct(aliased_shout_author.shout)).label("shouts_stat")
)
q = q.outerjoin(aliased_authors, aliased_authors.follower == Author.id)
q = q.add_columns(
func.count(distinct(aliased_authors.author)).label('authors_stat')
func.count(distinct(aliased_authors.author)).label("authors_stat")
)
q = q.outerjoin(aliased_followers, aliased_followers.author == Author.id)
q = q.add_columns(
func.count(distinct(aliased_followers.follower)).label('followers_stat')
func.count(distinct(aliased_followers.follower)).label("followers_stat")
)
# Create a subquery for comments count
sub_comments = (
select(
Author.id, func.coalesce(func.count(Reaction.id)).label('comments_count')
Author.id, func.coalesce(func.count(Reaction.id)).label("comments_count")
)
.outerjoin(
Reaction,
@@ -113,8 +122,8 @@ def add_author_stat_columns(q):
def get_with_stat(q):
records = []
try:
is_author = f'{q}'.lower().startswith('select author')
is_topic = f'{q}'.lower().startswith('select topic')
is_author = f"{q}".lower().startswith("select author")
is_topic = f"{q}".lower().startswith("select topic")
if is_author:
q = add_author_stat_columns(q)
elif is_topic:
@@ -124,22 +133,23 @@ def get_with_stat(q):
for cols in result:
entity = cols[0]
stat = dict()
stat['shouts'] = cols[1]
stat['authors'] = cols[2]
stat['followers'] = cols[3]
stat["shouts"] = cols[1]
stat["authors"] = cols[2]
stat["followers"] = cols[3]
if is_author:
stat['comments'] = cols[4]
stat["comments"] = cols[4]
entity.stat = stat
records.append(entity)
except Exception as exc:
import traceback
logger.error(exc, traceback.format_exc())
raise Exception(exc)
return records
def author_follows_authors(author_id: int):
af = aliased(AuthorFollower, name='af')
af = aliased(AuthorFollower, name="af")
q = (
select(Author)
.select_from(join(Author, af, Author.id == af.author))
@@ -158,7 +168,7 @@ def author_follows_topics(author_id: int):
async def update_author_stat(author: Author):
author_with_stat = get_with_stat(select(Author).where(Author.id==author.id))
author_with_stat = get_with_stat(select(Author).where(Author.id == author.id))
if isinstance(author_with_stat, Author):
author_dict = author_with_stat.dict()
await cache_author(author_dict)