mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-07 20:45:57 +08:00
✨ 定时清理数据库
This commit is contained in:
parent
d978a76254
commit
d011e5d8f9
@ -1,9 +1,19 @@
|
||||
from gsuid_core.sv import SV
|
||||
from gsuid_core.bot import Bot
|
||||
from gsuid_core.models import Event
|
||||
from gsuid_core.aps import scheduler
|
||||
from gsuid_core.logger import logger
|
||||
from gsuid_core.data_store import get_res_path
|
||||
from gsuid_core.utils.database.base_models import DB_PATH
|
||||
from gsuid_core.utils.database.models import GsUser, GsCache
|
||||
from gsuid_core.utils.backup.backup_files import clean_log, backup_file
|
||||
from gsuid_core.utils.database.models import (
|
||||
GsUser,
|
||||
GsCache,
|
||||
CoreUser,
|
||||
CoreGroup,
|
||||
)
|
||||
|
||||
sv_core_clean = SV('Core清除', pm=0)
|
||||
DB_BACKUP = get_res_path(['GsCore', 'database_backup'])
|
||||
|
||||
|
||||
@ -11,8 +21,28 @@ DB_BACKUP = get_res_path(['GsCore', 'database_backup'])
|
||||
async def database_backup():
|
||||
await backup_file(DB_PATH, DB_BACKUP)
|
||||
clean_log()
|
||||
logger.success('[早柚核心] 数据库已备份!')
|
||||
|
||||
|
||||
@scheduler.scheduled_job('cron', hour=0, minute=2)
|
||||
async def clear_cache():
|
||||
await GsCache.delete_all_cache(GsUser)
|
||||
logger.success('[早柚核心] 缓存已清除!')
|
||||
|
||||
|
||||
# 清除重复user和group
|
||||
@scheduler.scheduled_job('cron', hour=1, minute=2)
|
||||
async def delete_core_user_group():
|
||||
await CoreUser.clean_repeat_user()
|
||||
await CoreGroup.clean_repeat_group()
|
||||
logger.success('早柚核心] 重复用户和群组已清除!')
|
||||
|
||||
|
||||
@sv_core_clean.on_fullmatch(
|
||||
('core清除数据库', 'Core清除数据库'),
|
||||
block=True,
|
||||
)
|
||||
async def send_core_master_help_msg(bot: Bot, ev: Event):
|
||||
logger.info('[早柚核心] 开始执行[清除数据库]')
|
||||
await delete_core_user_group()
|
||||
await bot.send('操作已成功完成! 该操作不会影响现有数据!')
|
||||
|
@ -114,6 +114,7 @@ async def get_global_analysis(bot_id: str, bot_self_id: str):
|
||||
new_user.append(i)
|
||||
|
||||
_user_all_list = list(set(user_all_list))
|
||||
out_user = list(set(out_user))
|
||||
|
||||
user_num = len(user_data)
|
||||
group_num = len(group_data)
|
||||
|
@ -110,6 +110,38 @@ class CoreUser(BaseBotIDModel, table=True):
|
||||
user_name: Optional[str] = Field(default='1', title='用户名')
|
||||
user_icon: Optional[str] = Field(default='1', title='用户头像')
|
||||
|
||||
@classmethod
|
||||
@with_session
|
||||
async def clean_repeat_user(
|
||||
cls,
|
||||
session: AsyncSession,
|
||||
):
|
||||
# 移除重复的rows
|
||||
datas = await cls.get_all_data()
|
||||
_lst = []
|
||||
for data in datas:
|
||||
if (
|
||||
data.bot_id,
|
||||
data.user_id,
|
||||
data.group_id,
|
||||
data.user_name,
|
||||
) in _lst or data.group_id == '1':
|
||||
await cls.delete_row(
|
||||
bot_id=data.bot_id,
|
||||
user_id=data.user_id,
|
||||
group_id=data.group_id,
|
||||
user_name=data.user_name,
|
||||
)
|
||||
else:
|
||||
_lst.append(
|
||||
(
|
||||
data.bot_id,
|
||||
data.user_id,
|
||||
data.group_id,
|
||||
data.user_name,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@with_session
|
||||
async def get_all_user(
|
||||
@ -217,6 +249,32 @@ class CoreGroup(BaseBotIDModel, table=True):
|
||||
group_name: str = Field(default='1', title='群名')
|
||||
group_icon: str = Field(default='1', title='群头像')
|
||||
|
||||
@classmethod
|
||||
@with_session
|
||||
async def clean_repeat_group(
|
||||
cls,
|
||||
session: AsyncSession,
|
||||
):
|
||||
# 移除重复的rows
|
||||
datas = await cls.get_all_data()
|
||||
_lst = []
|
||||
for data in datas:
|
||||
if (
|
||||
data.bot_id,
|
||||
data.group_id,
|
||||
) in _lst or data.group_id == '1':
|
||||
await cls.delete_row(
|
||||
bot_id=data.bot_id,
|
||||
group_id=data.group_id,
|
||||
)
|
||||
else:
|
||||
_lst.append(
|
||||
(
|
||||
data.bot_id,
|
||||
data.group_id,
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@with_session
|
||||
async def get_all_group(
|
||||
|
Loading…
x
Reference in New Issue
Block a user