feat(monthly_plan): 增强月度计划系统,引入状态管理和智能抽取
对月度计划系统进行了全面的重构和功能增强,以提供更智能、更可持续的计划管理体验。 主要变更包括: - **引入状态生命周期**: 废弃了原有的 `is_deleted` 软删除标记,引入了更明确的 `status` 字段 (`active`, `completed`, `archived`),用于管理计划的整个生命周期。 - **增加使用统计与自动完成**: 新增 `usage_count` 和 `last_used_date` 字段来跟踪计划的使用情况。当计划使用次数达到可配置的阈值后,会自动标记为 `completed`。 - **实现智能计划抽取**: 为每日日程生成实现了新的智能抽取算法。该算法会优先选择使用次数较少且近期未被使用的计划,以增加计划的多样性并避免重复。 - **更新配置选项**: 移除了旧的概率删除相关配置,增加了 `completion_threshold`、`avoid_repetition_days` 等新选项以支持新逻辑。 - **数据库模型更新**: 更新了 `MonthlyPlan` 的数据库模型和索引,以支持新功能并优化查询性能。保留 `is_deleted` 字段以兼容旧数据。
This commit is contained in:
@@ -5,11 +5,12 @@
|
||||
|
||||
from sqlalchemy import Column, String, Float, Integer, Boolean, Text, Index, create_engine, DateTime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from sqlalchemy.pool import QueuePool
|
||||
import os
|
||||
import datetime
|
||||
import time
|
||||
from typing import Iterator, Optional
|
||||
from src.common.logger import get_logger
|
||||
from contextlib import contextmanager
|
||||
|
||||
@@ -508,16 +509,25 @@ class CacheEntries(Base):
|
||||
)
|
||||
|
||||
class MonthlyPlan(Base):
|
||||
"""月层计划模型"""
|
||||
"""月度计划模型"""
|
||||
__tablename__ = 'monthly_plans'
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
plan_text = Column(Text, nullable=False)
|
||||
target_month = Column(String(7), nullable=False, index=True) # "YYYY-MM"
|
||||
is_deleted = Column(Boolean, nullable=False, default=False, index=True)
|
||||
status = Column(get_string_field(20), nullable=False, default='active', index=True) # 'active', 'completed', 'archived'
|
||||
usage_count = Column(Integer, nullable=False, default=0)
|
||||
last_used_date = Column(String(10), nullable=True, index=True) # "YYYY-MM-DD" format
|
||||
created_at = Column(DateTime, nullable=False, default=datetime.datetime.now)
|
||||
|
||||
# 保留 is_deleted 字段以兼容现有数据,但标记为已弃用
|
||||
is_deleted = Column(Boolean, nullable=False, default=False)
|
||||
|
||||
__table_args__ = (
|
||||
Index('idx_monthlyplan_target_month_status', 'target_month', 'status'),
|
||||
Index('idx_monthlyplan_last_used_date', 'last_used_date'),
|
||||
Index('idx_monthlyplan_usage_count', 'usage_count'),
|
||||
# 保留旧索引以兼容
|
||||
Index('idx_monthlyplan_target_month_is_deleted', 'target_month', 'is_deleted'),
|
||||
)
|
||||
|
||||
@@ -628,9 +638,9 @@ def initialize_database():
|
||||
|
||||
|
||||
@contextmanager
|
||||
def get_db_session():
|
||||
def get_db_session() -> Iterator[Session]:
|
||||
"""数据库会话上下文管理器 - 推荐使用这个而不是get_session()"""
|
||||
session = None
|
||||
session: Optional[Session] = None
|
||||
try:
|
||||
_, SessionLocal = initialize_database()
|
||||
session = SessionLocal()
|
||||
|
||||
Reference in New Issue
Block a user