61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""筛选过程日志持久化。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from app.db.database import get_db
|
|
from app.db import tables
|
|
|
|
|
|
def _safe_json(data: dict[str, Any] | None) -> str:
|
|
if not data:
|
|
return "{}"
|
|
try:
|
|
return json.dumps(data, ensure_ascii=False, default=str)
|
|
except Exception:
|
|
return "{}"
|
|
|
|
|
|
async def log_scan_stage(
|
|
*,
|
|
scan_session: str,
|
|
scan_mode: str,
|
|
stage: str,
|
|
stage_label: str,
|
|
input_count: int = 0,
|
|
output_count: int = 0,
|
|
filtered_count: int | None = None,
|
|
status: str = "ok",
|
|
summary: str = "",
|
|
detail: dict[str, Any] | None = None,
|
|
) -> None:
|
|
"""记录筛选漏斗的一关。
|
|
|
|
这是观测能力,不能反过来影响筛选主流程,所以所有异常都会被吞掉。
|
|
"""
|
|
try:
|
|
if filtered_count is None:
|
|
filtered_count = max(int(input_count or 0) - int(output_count or 0), 0)
|
|
async with get_db() as db:
|
|
await db.execute(
|
|
tables.scan_process_logs_table.insert().values(
|
|
scan_session=scan_session or "manual",
|
|
scan_mode=scan_mode or "",
|
|
stage=stage,
|
|
stage_label=stage_label,
|
|
status=status,
|
|
input_count=int(input_count or 0),
|
|
output_count=int(output_count or 0),
|
|
filtered_count=int(filtered_count or 0),
|
|
summary=summary or "",
|
|
detail_json=_safe_json(detail),
|
|
created_at=datetime.now(),
|
|
)
|
|
)
|
|
await db.commit()
|
|
except Exception:
|
|
pass
|