feat(file-summary): 生成汇总报告和导出下载
This commit is contained in:
65
review_agent/file_summary/services/report.py
Normal file
65
review_agent/file_summary/services/report.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from review_agent.models import ExportedSummaryFile, FileSummaryBatch
|
||||
|
||||
|
||||
def _exports_dir(batch: FileSummaryBatch) -> Path:
|
||||
root = Path(batch.work_dir or Path("media") / "file_summary" / batch.batch_no)
|
||||
export_dir = root / "exports"
|
||||
export_dir.mkdir(parents=True, exist_ok=True)
|
||||
return export_dir
|
||||
|
||||
|
||||
def build_summary_table(batch: FileSummaryBatch) -> str:
|
||||
lines = [
|
||||
"| 序号 | 目录层级 | 文件名 | 类型 | 页数 | 状态 | 异常说明 |",
|
||||
"| --- | --- | --- | --- | --- | --- | --- |",
|
||||
]
|
||||
for item in batch.items.order_by("file_index"):
|
||||
lines.append(
|
||||
"| {index} | {directory} | {name} | {file_type} | {pages} | {status} | {error} |".format(
|
||||
index=item.file_index,
|
||||
directory=item.directory_level or "-",
|
||||
name=item.file_name,
|
||||
file_type=item.file_type,
|
||||
pages=item.page_count if item.page_count is not None else "-",
|
||||
status=item.statistics_status,
|
||||
error=item.error_message or "-",
|
||||
)
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def build_markdown_report(batch: FileSummaryBatch) -> str:
|
||||
return "\n\n".join(
|
||||
[
|
||||
f"# 文件目录与页数汇总报告\n\n批次号:{batch.batch_no}",
|
||||
(
|
||||
"## 汇总信息\n\n"
|
||||
f"- 产品名称:{batch.product_name or '-'}\n"
|
||||
f"- 文件总数:{batch.total_files}\n"
|
||||
f"- 统计成功:{batch.success_files}\n"
|
||||
f"- 统计失败:{batch.failed_files}\n"
|
||||
f"- 不支持:{batch.unsupported_files}\n"
|
||||
f"- 不确定:{batch.uncertain_files}\n"
|
||||
f"- 总页数:{batch.total_pages}"
|
||||
),
|
||||
"## 文件明细\n\n" + build_summary_table(batch),
|
||||
"## 处理说明\n\n单文件失败不会阻断批次,失败与不确定文件已在明细中标注。",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def generate_markdown_report(batch: FileSummaryBatch) -> tuple[ExportedSummaryFile, str]:
|
||||
content = build_markdown_report(batch)
|
||||
path = _exports_dir(batch) / f"{batch.batch_no}-summary.md"
|
||||
path.write_text(content, encoding="utf-8")
|
||||
exported = ExportedSummaryFile.objects.create(
|
||||
batch=batch,
|
||||
export_type=ExportedSummaryFile.ExportType.MARKDOWN,
|
||||
file_name=path.name,
|
||||
storage_path=str(path),
|
||||
)
|
||||
return exported, build_summary_table(batch)
|
||||
Reference in New Issue
Block a user