faster-response
All checks were successful
Deploy on push / deploy (push) Successful in 1m8s

This commit is contained in:
Untone 2024-06-11 22:46:35 +03:00
parent 04e20b29ee
commit c24f3bbb4a
7 changed files with 46 additions and 38 deletions

View File

@ -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

View File

@ -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}

View File

@ -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:

View File

@ -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()

View File

@ -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()

View File

@ -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():

View File

@ -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)