异步优化

还有个httpx的client启动优化没解决,准备后期用uninfo解决了。
This commit is contained in:
Chenric 2024-09-21 18:53:41 +08:00
parent 7aaddb98f5
commit 65dc67c6ac
3 changed files with 65 additions and 39 deletions

View File

@ -216,7 +216,7 @@ async def handle_rank(
start: datetime = Arg(),
stop: datetime = Arg(),
):
if id := state["group_id"]:
id = str(id)
logger.debug(f"group_id: {id}")
@ -248,47 +248,14 @@ async def handle_rank(
rank = got_rank(raw_rank)
logger.debug(rank)
rank2: List[UserRankInfo] = []
ids = await persist_id2user_id([int(i[0]) for i in rank])
for i in range(len(rank)):
rank[i][0] = str(ids[i])
logger.debug(rank[i])
total = sum([i[1] for i in rank])
index = 1
user_infos = await get_user_infos(bot, event, user_ids=[str(i[0]) for i in rank])
for user_info,i in zip(user_infos,rank):
if user_info:
logger.debug(user_info)
user_nickname = (
user_info.user_displayname
if user_info.user_displayname
else user_info.user_name if user_info.user_name else user_info.user_id
)
user_avatar = (
await user_info.user_avatar.get_image()
if user_info.user_avatar
else open(
os.path.dirname(os.path.abspath(__file__))
+ "/template/avatar/default.jpg",
"rb",
).read()
)
user = UserRankInfo(
**model_dump(user_info),
user_bnum=i[1],
user_proportion=round(i[1] / total * 100, 2),
user_index=index,
user_nickname=user_nickname,
user_avatar_bytes=user_avatar,
)
user.user_gender = (
""
if user_info.user_gender == "female"
else "" if user_info.user_gender == "male" else ""
)
rank2.append(user)
index += 1
t1 = t.time()
rank2 = await get_user_infos(bot, event, rank)
logger.debug(f"获取用户信息花费时间:{t.time() - t1}")
string: str = ""
for i in rank2:

View File

@ -18,6 +18,7 @@ class UserRankInfo(UserInfo):
# class MsgCountDayData(BaseModel):
# session_id: str
# session_bnum: int
class MessageCountCache(Model):
__table_args__ = {"extend_existing": True}
id: Mapped[int] = mapped_column(primary_key=True)

View File

@ -8,6 +8,7 @@ from sqlalchemy.sql import ColumnElement
from nonebot.log import logger
from nonebot.params import Depends
from nonebot.compat import model_dump
from nonebot.matcher import Matcher
from nonebot.adapters import Bot, Event
@ -183,7 +184,64 @@ async def get_rank_image(rank: List[UserRankInfo]) -> bytes:
},
pages={"viewport": {"width": 1000, "height": 10}},
)
def _get_user_nickname(user_info:UserInfo)->str:
user_nickname = (
user_info.user_displayname
if user_info.user_displayname
else user_info.user_name if user_info.user_name else user_info.user_id
)
return user_nickname
async def get_user_infos(bot:Bot,event:Event,user_ids:List[str],use_cache: bool = True)-> List[Optional[UserInfo]]:
async def _get_user_default_avatar()->bytes:
img = open(
os.path.dirname(os.path.abspath(__file__))
+ "/template/avatar/default.jpg",
"rb",
).read()
return img
def get_default_user_info()->UserInfo:
user_info = UserInfo(
user_id="114514",
user_name="鬼知道这谁bot获取不了",
)
return user_info
async def get_user_infos(bot:Bot,event:Event,rank:List,use_cache: bool = True)-> List[UserRankInfo]:
user_ids = [i[0] for i in rank]
pool = [get_user_info(bot, event, id, use_cache) for id in user_ids]
return (await asyncio.gather(*pool))
user_infos = (await asyncio.gather(*pool))
pool = []
for i in user_infos:
if not i:
pool.append(_get_user_default_avatar())
continue
if i.user_avatar:
pool.append(i.user_avatar.get_image())
user_avatars = await asyncio.gather(*pool)
for i in user_avatars:
if not i:
user_avatars[user_avatars.index(i)] = await(_get_user_default_avatar())
total = sum([i[1] for i in rank])
rank2 = []
for i in range(len(rank)):
user_info = user_infos[i]
if not user_info:
user_info = get_default_user_info()
user = UserRankInfo(
**model_dump(user_info),
user_bnum=rank[i][1],
user_proportion=round(rank[i][1] / total * 100, 2),
user_index=i+1,
user_nickname=_get_user_nickname(user_info),
user_avatar_bytes=user_avatars[i],
)
rank2.append(user)
return rank2