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 shutil
import subprocess import subprocess
import sys import sys
import traceback
from queue import Queue, Empty from queue import Queue, Empty
from threading import Thread from threading import Thread
from time import sleep from time import sleep

View file

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

View file

@ -174,9 +174,9 @@ class FetchTarget(FT):
except Exception: except Exception:
Logger.error(traceback.format_exc()) Logger.error(traceback.format_exc())
else: 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: for x in get_target_id:
fetch = await FetchTarget.fetch_target(x) fetch = await FetchTarget.fetch_target(x.targetId)
if fetch: if fetch:
try: try:
send = await fetch.sendDirectMessage(message) send = await fetch.sendDirectMessage(message)

View file

@ -198,9 +198,9 @@ class FetchTarget(FT):
except Exception: except Exception:
Logger.error(traceback.format_exc()) Logger.error(traceback.format_exc())
else: 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: for x in get_target_id:
fetch = await FetchTarget.fetch_target(x) fetch = await FetchTarget.fetch_target(x.targetId)
if fetch: if fetch:
try: try:
send = await fetch.sendDirectMessage(message) send = await fetch.sendDirectMessage(message)

View file

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