load-chats-debug

This commit is contained in:
Untone 2024-01-24 10:36:50 +03:00
parent 8901ded662
commit cbd77122a5
4 changed files with 23 additions and 5 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ __pycache__
.vscode
poetry.lock
.venv
.ruff_cache

View File

@ -1,3 +1,7 @@
[0.2.20]
- added logger
- typing revision
[0.2.19]
- fix: stripping user_id

View File

@ -9,6 +9,11 @@ from services.core import CacheStorage
from services.rediscache import redis
from services.schema import query
import logging
logger = logging.getLogger("[resolvers.load] ")
logger.setLevel(logging.DEBUG)
async def get_unread_counter(chat_id: str, member_id: int) -> int:
unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{member_id}")
@ -61,23 +66,27 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
author_id = info.context["author_id"]
chats = []
if author_id:
logger.debug("got author", author_id)
cids = await redis.execute("SMEMBERS", f"chats_by_author/{author_id}")
logger.debug("got cids", cids)
members_online = (await redis.execute("SMEMBERS", "authors-online")) or [] # to show online status
logger.debug("members online", members_online)
if isinstance(cids, set):
members_online = (await redis.execute("SMEMBERS", "authors-online")) or []
# TODO: add sort by chat.created_at with in-memory caching chats service
cids = list(cids)[offset : (offset + limit)]
lock = asyncio.Lock()
if len(cids) == 0:
print(f"[resolvers.load] no chats for user with id={author_id}")
logger.debug(f"no chats for user with id={author_id}")
r = await create_chat(None, info, members=[2]) # member with id = 2 is discours
print(f"[resolvers.load] created chat: {r['chat_id']}")
logger.debug(f"created chat: {r['chat_id']}")
cids.append(r["chat"]["id"])
logger.debug(f"getting data for {len(cids)} user's chats")
for cid in cids:
async with lock:
chat_str = await redis.execute("GET", f"chats/{cid}")
if isinstance(chat_str, str):
print(f"[resolvers.load] redis GET by {cid}: {chat_str}")
logger.debug(f"redis GET by {cid}: {chat_str}")
c: ChatPayload = json.loads(chat_str)
c["messages"] = (await load_messages(cid, 5, 0)) or []
c["unread"] = await get_unread_counter(cid, author_id)
@ -88,7 +97,12 @@ async def load_chats(_, info, limit: int = 50, offset: int = 0) -> Dict[str, Uni
if a:
a["online"] = a.get("id") in members_online
c["members"].append(a)
else:
logger.error(f"cant find author by id {member_id}")
chats.append(c)
else:
logger.error(f"cant find chat by id {cid}")
return {"chats": chats, "error": None}

View File

@ -16,7 +16,6 @@ async def check_auth(req) -> str | None:
user_id = ""
if token:
# Logging the authentication token
# print(f"[services.auth] checking auth token: {token}")
query_name = "validate_jwt_token"
operation = "ValidateToken"
headers = {