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

This commit is contained in:
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()