From 3f361b1af75efc8de5891f549325cd8a73c24e8a Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 21 Feb 2024 18:38:15 +0300 Subject: [PATCH] sqlfix --- resolvers/follower.py | 12 +++--------- services/follows.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/resolvers/follower.py b/resolvers/follower.py index 0a8b3e29..26b23117 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -98,8 +98,6 @@ def query_follows(user_id: str): session.query(aliased_author) .join(AuthorFollower, AuthorFollower.follower == author_id) .filter(AuthorFollower.author == aliased_author.id) - # .options(load_only(aliased_author.id)) # TODO: Exclude unnecessary columns - .dicts() .all() ) @@ -107,23 +105,20 @@ def query_follows(user_id: str): session.query(Topic) .join(TopicFollower, TopicFollower.follower == author_id) .filter(TopicFollower.topic == Topic.id) - # .options(load_only(Topic.id)) # TODO: Exclude unnecessary columns - .dicts() .all() ) # Convert query results to lists of dictionaries - authors = set(authors_query) - topics = set(topics_query) + authors = [author.to_dict() for author in authors_query] + topics = [topic.to_dict() for topic in topics_query] # shouts_query = ( # session.query(Shout) # .join(ShoutReactionsFollower, ShoutReactionsFollower.follower == author_id) # .filter(ShoutReactionsFollower.shout == Shout.id) # .options(load_only(Shout.id)) # Exclude unnecessary columns - # .dicts() # .all() # ) - # shouts = list(shouts_query) + # shouts = [shout.to_dict() for shout in shouts_query] # communities = session.query(Community).all() return { @@ -134,7 +129,6 @@ def query_follows(user_id: str): } - async def get_follows_by_user_id(user_id: str): if user_id: redis_key = f"user:{user_id}:follows" diff --git a/services/follows.py b/services/follows.py index 76503714..c8666507 100644 --- a/services/follows.py +++ b/services/follows.py @@ -124,8 +124,13 @@ class FollowsCached: authors = session.query(Author).all() total_authors = len(authors) for i in range(0, total_authors, BATCH_SIZE): - batch_authors = authors[i:i+BATCH_SIZE] - await asyncio.gather(*[FollowsCached.update_author_cache(author) for author in batch_authors]) + batch_authors = authors[i : i + BATCH_SIZE] + await asyncio.gather( + *[ + FollowsCached.update_author_cache(author) + for author in batch_authors + ] + ) @staticmethod async def update_author_cache(author): @@ -154,5 +159,6 @@ class FollowsCached: except Exception as exc: logger.error(exc) + async def start_cached_follows(): await FollowsCached.worker()