feat(monthly_plan): 增加月度计划数量上限并自动清理

style: 优化月度计划相关日志输出

- 在数据库模块中新增物理删除月度计划的函数 `delete_plans_by_ids`
- 在月度计划管理器中引入 `max_plans_per_month` 配置,用于限制每月计划数量
- 当检测到计划数量超出上限时,自动按创建时间删除最旧的计划以维持数量限制
- 优化了标记计划完成和删除计划时的日志记录,使其输出更详细的计划内容
- 调整了检查现有计划时的日志信息,使其更清晰
This commit is contained in:
tt-P607
2025-08-27 23:13:55 +08:00
parent 35157a1521
commit 29e18c2db7
2 changed files with 59 additions and 3 deletions

View File

@@ -8,7 +8,9 @@ from src.common.database.monthly_plan_db import (
add_new_plans,
get_archived_plans_for_month,
archive_active_plans_for_month,
has_active_plans
has_active_plans,
get_active_plans_for_month,
delete_plans_by_ids
)
from src.config.config import global_config, model_config
from src.llm_models.utils_model import LLMRequest
@@ -67,7 +69,23 @@ class MonthlyPlanManager:
logger.info(f" {target_month} 没有任何有效的月度计划,将立即生成。")
return await self.generate_monthly_plans(target_month)
else:
# logger.info(f"{target_month} 已存在有效的月度计划,跳过生成。")
logger.info(f"{target_month} 已存在有效的月度计划")
plans = get_active_plans_for_month(target_month)
# 检查是否超出上限
max_plans = global_config.monthly_plan_system.max_plans_per_month
if len(plans) > max_plans:
logger.warning(f"当前月度计划数量 ({len(plans)}) 超出上限 ({max_plans}),将自动删除多余的计划。")
# 按创建时间升序排序(旧的在前),然后删除超出上限的部分(新的)
plans_to_delete = sorted(plans, key=lambda p: p.created_at)[max_plans:]
delete_ids = [p.id for p in plans_to_delete]
delete_plans_by_ids(delete_ids)
# 重新获取计划列表
plans = get_active_plans_for_month(target_month)
if plans:
plan_texts = "\n".join([f" {i+1}. {plan.plan_text}" for i, plan in enumerate(plans)])
logger.info(f"当前月度计划内容:\n{plan_texts}")
return True # 已经有计划,也算成功
async def generate_monthly_plans(self, target_month: Optional[str] = None) -> bool: