Archived
1
0
Fork 0

optimization for database query

This commit is contained in:
yzhh 2022-08-29 23:54:20 +08:00
parent 17004faad9
commit b95fb0845e
5 changed files with 14 additions and 23 deletions

1
bot.py
View file

@ -2,7 +2,6 @@ import os
import shutil
import subprocess
import sys
import traceback
from queue import Queue, Empty
from threading import Thread
from time import sleep

View file

@ -209,15 +209,11 @@ class FetchTarget(FT):
except Exception:
Logger.error(traceback.format_exc())
else:
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name)
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name, "QQ")
group_list_raw = await bot.call_action('get_group_list')
group_list = []
for g in group_list_raw:
group_list.append(g['group_id'])
group_list = [g['group_id'] for g in group_list_raw]
friend_list_raw = await bot.call_action('get_friend_list')
friend_list = []
for f in friend_list_raw:
friend_list.append(f['user_id'])
friend_list = [f['user_id'] for f in friend_list_raw]
guild_list_raw = await bot.call_action('get_guild_list')
guild_list = []
for g in guild_list_raw:
@ -231,7 +227,7 @@ class FetchTarget(FT):
traceback.print_exc()
continue
for x in get_target_id:
fetch = await FetchTarget.fetch_target(x)
fetch = await FetchTarget.fetch_target(x.targetId)
Logger.debug(fetch)
if fetch:
if fetch.target.targetFrom == 'QQ|Group':
@ -244,7 +240,6 @@ class FetchTarget(FT):
if fetch.session.target not in guild_list:
continue
try:
print(fetch)
send = await fetch.sendDirectMessage(message)
if enable_analytics:
BotDBUtil.Analytics(fetch).add('', module_name, 'schedule')

View file

@ -174,9 +174,9 @@ class FetchTarget(FT):
except Exception:
Logger.error(traceback.format_exc())
else:
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name)
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name, "Telegram")
for x in get_target_id:
fetch = await FetchTarget.fetch_target(x)
fetch = await FetchTarget.fetch_target(x.targetId)
if fetch:
try:
send = await fetch.sendDirectMessage(message)

View file

@ -198,9 +198,9 @@ class FetchTarget(FT):
except Exception:
Logger.error(traceback.format_exc())
else:
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name)
get_target_id = BotDBUtil.TargetInfo.get_enabled_this(module_name, "Discord")
for x in get_target_id:
fetch = await FetchTarget.fetch_target(x)
fetch = await FetchTarget.fetch_target(x.targetId)
if fetch:
try:
send = await fetch.sendDirectMessage(message)

View file

@ -1,5 +1,5 @@
import datetime
from typing import Union
from typing import Union, List
import ujson as json
from tenacity import retry, stop_after_attempt
@ -192,14 +192,11 @@ class BotDBUtil:
return self.query.locale
@staticmethod
def get_enabled_this(module_name):
query = session.query(TargetInfo).filter(TargetInfo.enabledModules.like(f'%{module_name}%'))
targetIds = []
for x in query:
enabled_list = json.loads(x.enabledModules)
if module_name in enabled_list:
targetIds.append(x.targetId)
return targetIds
def get_enabled_this(module_name, id_prefix=None) -> List[TargetInfo]:
filter_ = [TargetInfo.enabledModules.like(f'%"{module_name}"%')]
if id_prefix is not None:
filter_.append(TargetInfo.targetId.like(f'{id_prefix}%'))
return session.query(TargetInfo).filter(*filter_).all()
class SenderInfo:
@retry(stop=stop_after_attempt(3))