Showing
1 changed file
with
49 additions
and
0 deletions
| @@ -400,6 +400,36 @@ def append_knowledge_log(source: str, payload: dict): | @@ -400,6 +400,36 @@ def append_knowledge_log(source: str, payload: dict): | ||
| 400 | logger.warning(f"Knowledge Query: 写日志失败: {exc}") | 400 | logger.warning(f"Knowledge Query: 写日志失败: {exc}") |
| 401 | 401 | ||
| 402 | 402 | ||
| 403 | +def _trim_text(text: str, limit: int = 300) -> str: | ||
| 404 | + text = _sanitize_log_text(text) | ||
| 405 | + return text if len(text) <= limit else text[:limit] + "..." | ||
| 406 | + | ||
| 407 | + | ||
| 408 | +def _compact_records(items): | ||
| 409 | + """将节点/记录压缩为简洁日志格式,避免污染。""" | ||
| 410 | + compacted = [] | ||
| 411 | + if not items: | ||
| 412 | + return compacted | ||
| 413 | + | ||
| 414 | + for item in items: | ||
| 415 | + if not isinstance(item, dict): | ||
| 416 | + compacted.append(_trim_text(str(item))) | ||
| 417 | + continue | ||
| 418 | + | ||
| 419 | + entry = {} | ||
| 420 | + for key, value in item.items(): | ||
| 421 | + # 仅记录必要字段,其他字段做字符串压缩 | ||
| 422 | + if isinstance(value, (str, int, float, bool)): | ||
| 423 | + entry[key] = _trim_text(str(value)) | ||
| 424 | + else: | ||
| 425 | + try: | ||
| 426 | + entry[key] = _trim_text(json.dumps(value, ensure_ascii=False)) | ||
| 427 | + except Exception: | ||
| 428 | + entry[key] = _trim_text(str(value)) | ||
| 429 | + compacted.append(entry) | ||
| 430 | + return compacted | ||
| 431 | + | ||
| 432 | + | ||
| 403 | # 初始化 knowledge_query.log | 433 | # 初始化 knowledge_query.log |
| 404 | init_knowledge_log() | 434 | init_knowledge_log() |
| 405 | 435 | ||
| @@ -1551,6 +1581,25 @@ def query_graph(): | @@ -1551,6 +1581,25 @@ def query_graph(): | ||
| 1551 | ) | 1581 | ) |
| 1552 | 1582 | ||
| 1553 | result = query_engine.query(params) | 1583 | result = query_engine.query(params) |
| 1584 | + try: | ||
| 1585 | + append_knowledge_log( | ||
| 1586 | + 'GRAPH_QUERY_RESULT', | ||
| 1587 | + { | ||
| 1588 | + 'report_id': report_id or 'latest', | ||
| 1589 | + 'counts': { | ||
| 1590 | + 'matched_sections': len(result.matched_sections), | ||
| 1591 | + 'matched_queries': len(result.matched_queries), | ||
| 1592 | + 'matched_sources': len(result.matched_sources), | ||
| 1593 | + 'total_nodes': result.total_nodes, | ||
| 1594 | + }, | ||
| 1595 | + 'query_params': result.query_params, | ||
| 1596 | + 'matched_sections': _compact_records(result.matched_sections), | ||
| 1597 | + 'matched_queries': _compact_records(result.matched_queries), | ||
| 1598 | + 'matched_sources': _compact_records(result.matched_sources), | ||
| 1599 | + } | ||
| 1600 | + ) | ||
| 1601 | + except Exception as log_exc: # pragma: no cover - 日志失败不阻塞主流程 | ||
| 1602 | + logger.warning(f\"Knowledge Query: 结果写日志失败: {log_exc}\") | ||
| 1554 | 1603 | ||
| 1555 | return jsonify({ | 1604 | return jsonify({ |
| 1556 | 'success': True, | 1605 | 'success': True, |
-
Please register or login to post a comment