113 lines
4.1 KiB
Python
113 lines
4.1 KiB
Python
"""Govdoc Bridge — 结果适配器。
|
|
|
|
将 govdoc_engine 原始结果对象 (AuditResult / Finding / SemanticEntity)
|
|
映射为 ORM 模型字段和前端 VO 字典。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi_modules.fastapi_leaudit.govdoc_engine.engine.result import AuditResult
|
|
|
|
|
|
class ResultAdapter:
|
|
"""Govdoc 引擎结果 → 平台数据模型适配器。
|
|
|
|
负责将 govdoc_engine 的原始执行结果转换为:
|
|
- GovdocRun 状态字段更新
|
|
- GovdocRuleResult 列表
|
|
- GovdocReportArtifact 清单
|
|
- 前端 VO 字典
|
|
"""
|
|
|
|
def AdaptRunSummary(self, EngineResult: AuditResult) -> dict[str, Any]:
|
|
"""从 AuditResult.summary 提取 run 汇总字段。"""
|
|
s = EngineResult.summary
|
|
return {
|
|
"totalScore": s.score,
|
|
"passedCount": s.passed_count,
|
|
"failedCount": s.failed_count,
|
|
"skippedCount": s.skipped_count,
|
|
"resultStatus": "pass" if s.failed_count == 0 else "fail" if s.passed_count == 0 else "partial",
|
|
"resultSummaryJson": None, # 可为后续扩展预留
|
|
}
|
|
|
|
def AdaptRuleResults(self, EngineResult: AuditResult) -> list[dict[str, Any]]:
|
|
"""从 AuditResult.findings 提取规则执行明细列表。"""
|
|
results: list[dict[str, Any]] = []
|
|
for f in EngineResult.findings:
|
|
results.append({
|
|
"ruleId": f.rule_id,
|
|
"ruleName": f.rule_name,
|
|
"severity": f.severity,
|
|
"category": f.category,
|
|
"message": f.message,
|
|
"suggestion": f.suggestion,
|
|
"actual": f.actual,
|
|
"expected": f.expected,
|
|
"evidence": f.evidence,
|
|
"paragraphIndex": f.location.paragraph_index if f.location else None,
|
|
"paragraphText": f.location.context if f.location else None,
|
|
"locationPath": f.location.role if f.location else None,
|
|
"result": "fail",
|
|
"score": None,
|
|
})
|
|
return results
|
|
|
|
def AdaptCheckedRules(self, EngineResult: AuditResult) -> list[dict[str, Any]]:
|
|
"""从 AuditResult.checked_rules 提取规则检查状态列表。"""
|
|
results: list[dict[str, Any]] = []
|
|
for cr in EngineResult.checked_rules:
|
|
results.append({
|
|
"ruleId": cr.rule_id,
|
|
"ruleName": cr.name,
|
|
"severity": cr.severity,
|
|
"category": cr.category,
|
|
"result": cr.status, # pass/fail/skipped
|
|
"skipReason": cr.skip_reason,
|
|
"score": None,
|
|
})
|
|
return results
|
|
|
|
def AdaptEntities(self, EngineResult: AuditResult) -> list[dict[str, Any]]:
|
|
"""从 AuditResult.entities 提取实体识别结果。"""
|
|
entities: list[dict[str, Any]] = []
|
|
for name, entity in EngineResult.entities.items():
|
|
if entity is None:
|
|
continue
|
|
entities.append({
|
|
"name": entity.name,
|
|
"text": entity.text,
|
|
"paragraphIndices": entity.paragraph_indices,
|
|
"primaryRole": entity.primary_role,
|
|
"source": entity.source,
|
|
"confidence": entity.confidence,
|
|
})
|
|
return entities
|
|
|
|
def AdaptArtifacts(self, _EngineResult: AuditResult, _RunId: int) -> list[dict[str, Any]]:
|
|
"""从引擎结果提取报告产物清单。
|
|
|
|
报告文件由 reporter 模块生成后上传 OSS。
|
|
当前返回空列表,待 report_adapter 实现后补齐。
|
|
"""
|
|
return []
|
|
|
|
def BuildDetailVO(
|
|
self,
|
|
Document: Any,
|
|
Run: Any,
|
|
RuleResults: list[dict[str, Any]],
|
|
Entities: list[dict[str, Any]],
|
|
Artifacts: list[dict[str, Any]],
|
|
) -> dict[str, Any]:
|
|
"""构建前端详情页 VO。"""
|
|
return {
|
|
"document": Document,
|
|
"run": Run,
|
|
"findings": RuleResults,
|
|
"entities": Entities,
|
|
"reports": Artifacts,
|
|
}
|