feat:为日志添加上限和配置防止膨胀
This commit is contained in:
@@ -13,7 +13,7 @@ class PromptPreviewLogger:
|
||||
"""负责保存 Maisaka Prompt 预览文件并控制目录容量。"""
|
||||
|
||||
_BASE_DIR = Path("logs") / "maisaka_prompt"
|
||||
_MAX_PREVIEW_GROUPS_PER_CHAT = 1024
|
||||
_DEFAULT_MAX_PREVIEW_GROUPS_PER_CHAT = 256
|
||||
_TRIM_COUNT = 100
|
||||
|
||||
@classmethod
|
||||
@@ -54,20 +54,21 @@ class PromptPreviewLogger:
|
||||
def _trim_overflow(cls, chat_dir: Path) -> None:
|
||||
"""超过阈值时按批次删除最老的若干组预览文件。"""
|
||||
|
||||
max_preview_groups = cls._get_max_preview_groups_per_chat()
|
||||
grouped_files: dict[str, list[Path]] = {}
|
||||
for file_path in chat_dir.iterdir():
|
||||
if not file_path.is_file():
|
||||
continue
|
||||
grouped_files.setdefault(file_path.stem, []).append(file_path)
|
||||
|
||||
if len(grouped_files) <= cls._MAX_PREVIEW_GROUPS_PER_CHAT:
|
||||
if len(grouped_files) <= max_preview_groups:
|
||||
return
|
||||
|
||||
sorted_groups = sorted(
|
||||
grouped_files.items(),
|
||||
key=lambda item: min(path.stat().st_mtime for path in item[1]),
|
||||
)
|
||||
overflow_count = len(grouped_files) - cls._MAX_PREVIEW_GROUPS_PER_CHAT
|
||||
overflow_count = len(grouped_files) - max_preview_groups
|
||||
trim_count = min(len(sorted_groups), max(cls._TRIM_COUNT, overflow_count))
|
||||
for _, file_group in sorted_groups[:trim_count]:
|
||||
for old_file in file_group:
|
||||
@@ -75,3 +76,13 @@ class PromptPreviewLogger:
|
||||
old_file.unlink()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
|
||||
@classmethod
|
||||
def _get_max_preview_groups_per_chat(cls) -> int:
|
||||
try:
|
||||
from src.config.config import global_config
|
||||
|
||||
configured_limit = global_config.log.maisaka_prompt_preview_limit
|
||||
return max(1, int(configured_limit or cls._DEFAULT_MAX_PREVIEW_GROUPS_PER_CHAT))
|
||||
except Exception:
|
||||
return cls._DEFAULT_MAX_PREVIEW_GROUPS_PER_CHAT
|
||||
|
||||
@@ -13,7 +13,7 @@ from .path_utils import BASE_DIR, build_reply_effect_chat_dir, normalize_preview
|
||||
class ReplyEffectStorage:
|
||||
"""负责回复效果记录的独立 JSON 文件存储。"""
|
||||
|
||||
_MAX_RECORDS_PER_CHAT = 1024
|
||||
_DEFAULT_MAX_RECORDS_PER_CHAT = 256
|
||||
_TRIM_COUNT = 100
|
||||
|
||||
def __init__(self, base_dir: Path | None = None) -> None:
|
||||
@@ -61,15 +61,26 @@ class ReplyEffectStorage:
|
||||
def _trim_overflow(self, chat_dir: Path) -> None:
|
||||
"""超过容量时删除最旧的回复效果记录。"""
|
||||
|
||||
max_records = self._get_max_records_per_chat()
|
||||
files = [file_path for file_path in chat_dir.glob("*.json") if file_path.is_file()]
|
||||
if len(files) <= self._MAX_RECORDS_PER_CHAT:
|
||||
if len(files) <= max_records:
|
||||
return
|
||||
|
||||
sorted_files = sorted(files, key=lambda file_path: file_path.stat().st_mtime)
|
||||
overflow_count = len(files) - self._MAX_RECORDS_PER_CHAT
|
||||
overflow_count = len(files) - max_records
|
||||
trim_count = min(len(sorted_files), max(self._TRIM_COUNT, overflow_count))
|
||||
for old_file in sorted_files[:trim_count]:
|
||||
try:
|
||||
old_file.unlink()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
|
||||
@classmethod
|
||||
def _get_max_records_per_chat(cls) -> int:
|
||||
try:
|
||||
from src.config.config import global_config
|
||||
|
||||
configured_limit = global_config.log.maisaka_reply_effect_limit
|
||||
return max(1, int(configured_limit or cls._DEFAULT_MAX_RECORDS_PER_CHAT))
|
||||
except Exception:
|
||||
return cls._DEFAULT_MAX_RECORDS_PER_CHAT
|
||||
|
||||
Reference in New Issue
Block a user