From c24f3bbb4a83b42e08343c9a6b4e28ead68b7bfa Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 11 Jun 2024 22:46:35 +0300 Subject: [PATCH] faster-response --- resolvers/author.py | 34 +++++++++++++++++++++------------- resolvers/reaction.py | 6 +++--- resolvers/reader.py | 2 -- resolvers/stat.py | 4 ++-- services/cache.py | 2 +- services/triggers.py | 34 ++++++++++++++++++---------------- services/webhook.py | 2 +- 7 files changed, 46 insertions(+), 38 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 589f12f0..983d88f7 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -1,3 +1,4 @@ +import asyncio import time from sqlalchemy import desc, select, text @@ -34,9 +35,13 @@ async def update_author(_, info, profile): session.add(author) session.commit() author_query = select(Author).where(Author.user == user_id) - [author] = get_with_stat(author_query) - if author: - await cache_author(author.dict()) + result = get_with_stat(author_query) + if result: + author_with_stat = result[0] + if isinstance(author_with_stat, Author): + author_dict = author_with_stat.dict() + # await cache_author(author_dict) + asyncio.create_task(cache_author(author_dict)) return {"error": None, "author": author} except Exception as exc: import traceback @@ -54,7 +59,6 @@ def get_authors_all(_, _info): @query.field("get_author") async def get_author(_, _info, slug="", author_id=0): - author = None author_dict = None try: author_id = get_author_id_from(slug=slug, user="", author_id=author_id) @@ -65,11 +69,13 @@ async def get_author(_, _info, slug="", author_id=0): if not author_dict or not author_dict.get("stat"): # update stat from db author_query = select(Author).filter(Author.id == author_id) - [author] = get_with_stat(author_query) - if isinstance(author, Author): - logger.debug(f"update @{author.slug} with id {author.id}") - author_dict = author.dict() - await cache_author(author_dict) + result = get_with_stat(author_query) + if result: + author_with_stat = result[0] + if isinstance(author_with_stat, Author): + author_dict = author_with_stat.dict() + # await cache_author(author_dict) + asyncio.create_task(cache_author(author_dict)) except ValueError: pass except Exception as exc: @@ -92,10 +98,12 @@ async def get_author_id(_, _info, user: str): author_query = select(Author).filter(Author.user == user_id) result = get_with_stat(author_query) if result: - [author] = result - if author: - await cache_author(author.dict()) - return author + author_with_stat = result[0] + if isinstance(author_with_stat, Author): + author_dict = author_with_stat.dict() + # await cache_author(author_dict) + asyncio.create_task(cache_author(author_dict)) + return author_with_stat except Exception as exc: import traceback diff --git a/resolvers/reaction.py b/resolvers/reaction.py index ec986fb6..7524ee3a 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -114,7 +114,7 @@ async def _create_reaction(session, info, shout, author_id: int, reaction): # пересчет счетчика комментариев if str(r.kind) == ReactionKind.COMMENT.value: - await update_author_stat(author_id) + update_author_stat(author_id) # collaborative editing if rdict.get("reply_to") and r.kind in PROPOSAL_REACTIONS and author_id in shout.authors: @@ -138,7 +138,7 @@ async def _create_reaction(session, info, shout, author_id: int, reaction): # обновление счетчика комментариев в кеше if str(r.kind) == ReactionKind.COMMENT.value: - await update_author_stat(author_id) + update_author_stat(author_id) rdict["shout"] = shout.dict() rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0} @@ -315,7 +315,7 @@ async def delete_reaction(_, info, reaction_id: int): # обновление счетчика комментариев в кеше if str(r.kind) == ReactionKind.COMMENT.value: - await update_author_stat(author.id) + update_author_stat(author.id) await notify_reaction(reaction_dict, "delete") return {"error": None, "reaction": reaction_dict} diff --git a/resolvers/reader.py b/resolvers/reader.py index 8a862919..f6daa34c 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -307,8 +307,6 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0): with local_session() as session: result = session.execute(shouts_query).unique().all() if result: - logger.debug(result) - logger.debug(len(result)) for [ shout, ] in result: diff --git a/resolvers/stat.py b/resolvers/stat.py index 9ae00aea..9efa278a 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -259,11 +259,11 @@ def author_follows_topics(author_id: int): return get_with_stat(author_follows_topics_query) -async def update_author_stat(author_id: int): +def update_author_stat(author_id: int): author_query = select(Author).where(Author.id == author_id) try: result = get_with_stat(author_query) - if result and len(result) == 1: + if result: author_with_stat = result[0] if isinstance(author_with_stat, Author): author_dict = author_with_stat.dict() diff --git a/services/cache.py b/services/cache.py index 1853d195..feae99bf 100644 --- a/services/cache.py +++ b/services/cache.py @@ -82,7 +82,7 @@ async def get_cached_author_by_user_id(user_id: str, get_with_stat) -> dict: if not author_id: author_query = select(Author).filter(Author.user == user_id) result = get_with_stat(author_query) - if result and len(result) == 1: + if result: author_with_stat = result[0] if isinstance(author_with_stat, Author): author_dict = author_with_stat.dict() diff --git a/services/triggers.py b/services/triggers.py index c4586d3b..b3ddb351 100644 --- a/services/triggers.py +++ b/services/triggers.py @@ -17,15 +17,20 @@ DEFAULT_FOLLOWS = { } -async def handle_author_follower_change(author_id: int, follower_id: int, is_insert: bool): +def handle_author_follower_change(author_id: int, follower_id: int, is_insert: bool): logger.info(author_id) author_query = select(Author).select_from(Author).filter(Author.id == author_id) - [author] = get_with_stat(author_query) + author_result = get_with_stat(author_query) follower_query = select(Author).select_from(Author).filter(Author.id == follower_id) - [follower] = get_with_stat(follower_query) - if follower and author: - await cache_author(author.dict()) - await cache_follows(follower.id, "author", author.id, is_insert) + follower_result = get_with_stat(follower_query) + if follower_result and author_result: + author_with_stat = author_result[0] + follower = follower_result[0] + if author_with_stat: + author_dict = author_with_stat.dict() + # await cache_author(author_with_stat) + asyncio.create_task(cache_author(author_dict)) + asyncio.create_task(cache_follows(follower.id, "author", author_with_stat.id, is_insert)) async def handle_topic_follower_change(topic_id: int, follower_id: int, is_insert: bool): @@ -68,7 +73,7 @@ def after_reaction_update(mapper, connection, reaction: Reaction): author_subquery = select(Author).where(Author.id == reaction.created_by) result = get_with_stat(author_subquery) - if result and len(result) == 1: + if result: author_with_stat = result[0] if isinstance(author_with_stat, Author): author_dict = author_with_stat.dict() @@ -99,9 +104,10 @@ def after_author_update(_mapper, _connection, author: Author): author_query = select(Author).where(Author.id == author.id) result = get_with_stat(author_query) if result: - [author_with_stat] = result - if author_with_stat: - _task = asyncio.create_task(cache_author(author_with_stat.dict())) + author_with_stat = result[0] + author_dict = author_with_stat.dict() + # await cache_author(author_with_stat) + asyncio.create_task(cache_author(author_dict)) def after_topic_follower_insert(_mapper, _connection, target: TopicFollower): @@ -120,16 +126,12 @@ def after_topic_follower_delete(_mapper, _connection, target: TopicFollower): def after_author_follower_insert(_mapper, _connection, target: AuthorFollower): logger.info(target) - asyncio.create_task( - handle_author_follower_change(target.author, target.follower, True) # type: ignore - ) + handle_author_follower_change(target.author, target.follower, True) def after_author_follower_delete(_mapper, _connection, target: AuthorFollower): logger.info(target) - asyncio.create_task( - handle_author_follower_change(target.author, target.follower, False) # type: ignore - ) + handle_author_follower_change(target.author, target.follower, False) def events_register(): diff --git a/services/webhook.py b/services/webhook.py index 2e1d32aa..73967bbd 100644 --- a/services/webhook.py +++ b/services/webhook.py @@ -52,7 +52,7 @@ class WebhookEndpoint(HTTPEndpoint): session.commit() author_query = select(Author).filter(Author.user == user_id) result = get_with_stat(author_query) - if result and len(result) == 1: + if result: author_with_stat = result[0] author_dict = author_with_stat.dict() # await cache_author(author_with_stat)