feat: 增强处理历史风险与通知状态展示

This commit is contained in:
2026-06-04 01:21:02 +08:00
parent 72409e9652
commit 24446658ad
4 changed files with 156 additions and 14 deletions

View File

@@ -94,3 +94,29 @@ def create_notification_record(
web_detail_url=web_detail_url,
receipt=receipt,
)
def build_history_rows(logs) -> list[dict]:
"""
为处理历史列表补齐风险状态和通知状态。
View 只负责收集筛选条件,列表展示所需的聚合字段统一在服务层完成。
"""
notification_map = {
(item.batch_id, item.conversation_id): item
for item in NotificationRecord.objects.order_by("-created_at")
}
rows = []
for log in logs:
notification = notification_map.get((log.batch_id, log.conversation_id))
structured_output = log.structured_output or {}
rows.append(
{
"log": log,
"risk_status": structured_output.get("highest_risk_level")
or structured_output.get("risk_level")
or "-",
"notify_status": notification.message_status if notification else "-",
}
)
return rows

View File

@@ -2,24 +2,39 @@ from django.shortcuts import get_object_or_404, render
from .models import AgentAuditLog, NotificationRecord
from apps.chat.models import Conversation
from .services import build_history_rows
def log_list(request):
# 处理历史页支持按批次、产品和状态筛选。
scenario_id = (request.GET.get("scenario_id") or "").strip()
keyword = (request.GET.get("keyword") or "").strip()
notify_status = (request.GET.get("notify_status") or "").strip()
logs = AgentAuditLog.objects.all()
if scenario_id:
logs = logs.filter(scenario_id=scenario_id)
if keyword:
logs = logs.filter(product_name__icontains=keyword) | logs.filter(batch_id__icontains=keyword)
if notify_status:
matched_pairs = list(
NotificationRecord.objects.filter(message_status=notify_status).values_list(
"batch_id",
"conversation_id",
)
)
logs = [
log
for log in logs
if (log.batch_id, log.conversation_id) in matched_pairs
]
return render(
request,
"audit/log_list.html",
{
"logs": logs,
"history_rows": build_history_rows(logs),
"selected_scenario_id": scenario_id,
"keyword": keyword,
"notify_status": notify_status,
},
)