refactor: 下沉处理历史筛选到 audit 服务层
This commit is contained in:
@@ -133,6 +133,53 @@ def create_notification_record(
|
||||
)
|
||||
|
||||
|
||||
def build_history_list_context(
|
||||
*,
|
||||
scenario_id: str = "",
|
||||
keyword: str = "",
|
||||
notify_status: str = "",
|
||||
risk_status: str = "",
|
||||
) -> dict:
|
||||
"""
|
||||
组装处理历史列表页所需的筛选结果与展示上下文。
|
||||
|
||||
View 只负责读取 query params,筛选逻辑和列表聚合统一在服务层完成。
|
||||
"""
|
||||
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
|
||||
]
|
||||
if risk_status:
|
||||
logs = [
|
||||
log
|
||||
for log in logs
|
||||
if (log.structured_output or {}).get("highest_risk_level") == risk_status
|
||||
or (log.structured_output or {}).get("risk_level") == risk_status
|
||||
]
|
||||
history_rows = build_history_rows(logs)
|
||||
return {
|
||||
"history_rows": history_rows,
|
||||
"history_metrics": build_history_metrics(history_rows),
|
||||
"selected_scenario_id": scenario_id,
|
||||
"keyword": keyword,
|
||||
"notify_status": notify_status,
|
||||
"risk_status": risk_status,
|
||||
}
|
||||
|
||||
|
||||
def build_history_rows(logs) -> list[dict]:
|
||||
"""
|
||||
为处理历史列表补齐风险状态和通知状态。
|
||||
|
||||
@@ -4,55 +4,20 @@ from .models import AgentAuditLog, NotificationRecord
|
||||
from apps.chat.models import Conversation
|
||||
from .services import (
|
||||
build_detail_summary,
|
||||
build_history_metrics,
|
||||
build_history_rows,
|
||||
build_history_list_context,
|
||||
normalize_conversation_node_results,
|
||||
)
|
||||
|
||||
|
||||
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()
|
||||
risk_status = (request.GET.get("risk_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
|
||||
]
|
||||
if risk_status:
|
||||
logs = [
|
||||
log
|
||||
for log in logs
|
||||
if (log.structured_output or {}).get("highest_risk_level") == risk_status
|
||||
or (log.structured_output or {}).get("risk_level") == risk_status
|
||||
]
|
||||
history_rows = build_history_rows(logs)
|
||||
return render(
|
||||
request,
|
||||
"audit/log_list.html",
|
||||
{
|
||||
"history_rows": history_rows,
|
||||
"history_metrics": build_history_metrics(history_rows),
|
||||
"selected_scenario_id": scenario_id,
|
||||
"keyword": keyword,
|
||||
"notify_status": notify_status,
|
||||
"risk_status": risk_status,
|
||||
},
|
||||
context = build_history_list_context(
|
||||
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(),
|
||||
risk_status=(request.GET.get("risk_status") or "").strip(),
|
||||
)
|
||||
return render(request, "audit/log_list.html", context)
|
||||
|
||||
|
||||
def log_detail(request, log_id: int):
|
||||
|
||||
Reference in New Issue
Block a user