From 784f790b837028516e3360ee038031172dd8c723 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 21 Feb 2024 19:48:33 +0300 Subject: [PATCH] stats-follows --- resolvers/author.py | 6 +++- resolvers/follower.py | 70 ++++++++++++++++++++++++++++--------------- services/follows.py | 42 ++++++++++++++++---------- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 7e2c9fa9..d623ec3a 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -264,7 +264,11 @@ async def get_author_follows( ) -> List[Author]: with local_session() as session: if not user and (author_id or slug): - user = session.query(Author.user).where(or_(Author.id == author_id, Author.slug == slug)).first() + user = ( + session.query(Author.user) + .where(or_(Author.id == author_id, Author.slug == slug)) + .first() + ) if user: follows = await get_follows_by_user_id(user) return follows diff --git a/resolvers/follower.py b/resolvers/follower.py index 56ac1a6a..1714f0a1 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -98,7 +98,13 @@ def query_follows(user_id: str): if isinstance(author, Author): author_id = author.id authors_query = ( - select(column('name'), column('id'), column('slug'), column('pic'), column('bio')) + select( + column('name'), + column('id'), + column('slug'), + column('pic'), + column('bio'), + ) .select_from(Author) .join(AuthorFollower, AuthorFollower.follower == author_id) .filter(AuthorFollower.author == Author.id) @@ -114,29 +120,45 @@ def query_follows(user_id: str): topics_query = add_topic_stat_columns(topics_query) # Convert query results to lists of dictionaries - authors = [{ - 'id': author.id, - 'name': author.name, - 'slug': author.slug, - 'pic': author.pic, - 'bio': author.bio, - 'stat': { - 'shouts': shouts_stat, - 'followers': followers_stat, - 'followings': followings_stat, - } - } for [author, shouts_stat, followers_stat, followings_stat] in session.execute(authors_query)] - topics = [{ - 'id': topic.id, - 'title': topic.title, - 'slug': topic.slug, - 'body': topic.body, - 'stat': { - 'shouts': shouts_stat, - 'authors': authors_stat, - 'followers': followers_stat, - } - } for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(topics_query)] + authors = [ + { + 'id': author.id, + 'name': author.name, + 'slug': author.slug, + 'pic': author.pic, + 'bio': author.bio, + 'stat': { + 'shouts': shouts_stat, + 'followers': followers_stat, + 'followings': followings_stat, + }, + } + for [ + author, + shouts_stat, + followers_stat, + followings_stat, + ] in session.execute(authors_query) + ] + topics = [ + { + 'id': topic.id, + 'title': topic.title, + 'slug': topic.slug, + 'body': topic.body, + 'stat': { + 'shouts': shouts_stat, + 'authors': authors_stat, + 'followers': followers_stat, + }, + } + for [ + topic, + shouts_stat, + authors_stat, + followers_stat, + ] in session.execute(topics_query) + ] # shouts_query = ( # session.query(Shout) # .join(ShoutReactionsFollower, ShoutReactionsFollower.follower == author_id) diff --git a/services/follows.py b/services/follows.py index 792f6589..b407ff92 100644 --- a/services/follows.py +++ b/services/follows.py @@ -103,14 +103,18 @@ async def handle_author_follower_change(connection, author_id, follower_id, is_i ).first() if follower and author: await update_follows_for_user( - connection, follower.user, 'author', { - "id": author.id, - "name": author.name, - "slug": author.slug, - "pic": author.pic, - "bio": author.bio, - "stat": author.stat - }, is_insert + connection, + follower.user, + 'author', + { + 'id': author.id, + 'name': author.name, + 'slug': author.slug, + 'pic': author.pic, + 'bio': author.bio, + 'stat': author.stat, + }, + is_insert, ) @@ -132,17 +136,23 @@ async def handle_topic_follower_change(connection, topic_id, follower_id, is_ins ).first() if follower and topic: await update_follows_for_user( - connection, follower.user, 'topic', { - "id": topic.id, - "title": topic.title, - "slug": topic.slug, - "body": topic.body, - "stat": topic.stat - }, is_insert + connection, + follower.user, + 'topic', + { + 'id': topic.id, + 'title': topic.title, + 'slug': topic.slug, + 'body': topic.body, + 'stat': topic.stat, + }, + is_insert, ) + BATCH_SIZE = 33 + class FollowsCached: lock = asyncio.Lock() @@ -175,7 +185,7 @@ class FollowsCached: 'slug': author.slug, 'pic': author.pic, 'bio': author.bio, - 'stat': author.stat + 'stat': author.stat, } ), )