Conversation
…or interface Add backend infrastructure for multi-format audit report export: - Extend ExportFormat enum with HTML/PDF/WORD constants - Add NormalizeExportFormatStr function for string-based format normalization - Define AuditReportData, AuditSummary, AuditStatistics, LevelCount, RuleHit, AuditSQLItem, and ReportLabels data model structs - Define ReportGenerator interface with Generate and Format methods - Add CSVHeaders() and ToCSVRow() helper methods for CSV export - Add unit tests for NormalizeExportFormatStr with map case pattern
Implement CSVReportGenerator that reuses the existing CSVBuilder to generate CSV audit reports via the ReportGenerator interface. Add comprehensive unit tests covering normal data, empty SQL list, and special character escaping (comma, newline, double quote).
…ests
- Add self-contained HTML report template (templates/audit_report.html) with
4 content modules: audit summary, result statistics, problem SQL list, and
rule hit statistics. All text uses Go template i18n variables, CSS is inlined,
SQL displayed in <pre> tags, logo supports base64 embedding.
- Add report_html_template.go using embed.FS to embed the HTML template file.
- Add report_html.go implementing HTMLReportGenerator with html/template for
automatic XSS prevention via HTML escaping. Content-Type: text/html,
filename format: SQL_audit_report_{instance}_{taskId}.html.
- Add 4 unit tests: Normal (ContentType, HTML tags, SQL content, labels),
XSSPrevention (script/img tags escaped), EmptyData (empty SQL list renders),
LargeData (10000 SQL items performance check).
… export Refactor the DownloadTaskSQLReportFile API handler to support exporting audit reports in multiple formats (CSV, HTML, PDF, WORD) via the new export_format query parameter. Default to CSV for backward compatibility. Key changes: - Add BuildAuditReportData function in report_data_builder.go that converts Task + SQL data into AuditReportData (placed in controller layer to avoid circular dependency between utils and model packages) - Refactor DownloadTaskSQLReportFile to use BuildAuditReportData and ExportAuditReport for format-agnostic report generation - Add 16 new i18n locale messages for report labels (zh/en TOML + Go vars) - Update Swagger annotations with export_format parameter - Add comprehensive unit tests for helper functions: toLevelCounts, toRuleHits, extractRuleInfo, buildReportLabels (13 test functions, map case pattern, covering normal/empty/nil/edge cases) Requirement refs: REQ-6.1, REQ-6.2, REQ-6.4, REQ-NF-2.1, REQ-NF-3.1
Update swagger.json, swagger.yaml, and docs.go to include the new
export_format query parameter (type: string, default: csv) for the
/v1/tasks/audits/{task_id}/sql_report endpoint. Also update response
description from "sql report csv file" to "sql report file" to reflect
multi-format support.
… fallback (CE part)
PR Reviewer Guide 🔍(Review updated until commit 0fc0407)
|
|
Failed to generate code suggestions for PR |
|
Persistent review updated to latest commit 2d973a0 |
PR Code Suggestions ✨No code suggestions found for the PR. |
…tData function Updated the BuildAuditReportData function to initialize sqlList and problemSQLs with a predefined capacity based on taskSQLsDetail length, improving performance and memory usage.
|
Persistent review updated to latest commit 0fc0407 |
|
Failed to generate code suggestions for PR |
| // NormalizeExportFormatStr 规范化导出格式参数(字符串版本)。 | ||
| // 空字符串默认返回 CSV(向后兼容);无效格式返回错误。 | ||
| // 支持 html/pdf/word/docx/excel/xlsx/csv 等输入的规范化。 | ||
| func NormalizeExportFormatStr(format string) (ExportFormat, error) { | ||
| switch strings.ToLower(strings.TrimSpace(format)) { | ||
| case "html": | ||
| return ExportFormatHTML, nil | ||
| case "pdf": | ||
| return ExportFormatPDF, nil | ||
| case "word", "docx": | ||
| return ExportFormatWORD, nil | ||
| case "excel", "xlsx": | ||
| return ExcelExportFormat, nil | ||
| case "csv", "": | ||
| return CsvExportFormat, nil | ||
| default: | ||
| return "", fmt.Errorf("unsupported export format: %s", format) | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
ExportFormat也是导出格式的业务对象,应当移动到业务层
| // ReportType 返回生成器对应的导出格式 | ||
| ReportType() utils.ExportFormat |
There was a problem hiding this comment.
目前这个接口没有被除了单元测试之外的地方用到
| // @Param task_id path string true "task id" | ||
| // @Param no_duplicate query boolean false "select unique (fingerprint and audit result) for task sql" | ||
| // @Success 200 file 1 "sql report csv file" | ||
| // @Param export_format query string false "export format: csv, html, pdf, word" default(csv) |
| { | ||
| "type": "string", | ||
| "description": "export format: csv, html, pdf, word", | ||
| "name": "export_format", | ||
| "in": "query", | ||
| "default": "csv" | ||
| } |
| // toLevelCounts 将等级分布 map 转换为有序的 LevelCount 切片。 | ||
| // 按 error > warn > notice > normal 顺序排列。 | ||
| func toLevelCounts(dist map[string]int) []auditreport.LevelCount { | ||
| if len(dist) == 0 { | ||
| return []auditreport.LevelCount{} | ||
| } | ||
|
|
||
| levelOrder := map[string]int{ | ||
| "error": 0, | ||
| "warn": 1, | ||
| "notice": 2, | ||
| "normal": 3, | ||
| } | ||
|
|
||
| result := make([]auditreport.LevelCount, 0, len(dist)) | ||
| for level, count := range dist { | ||
| result = append(result, auditreport.LevelCount{ | ||
| Level: level, | ||
| Count: count, | ||
| }) | ||
| } | ||
|
|
||
| sort.Slice(result, func(i, j int) bool { | ||
| oi, ok := levelOrder[result[i].Level] | ||
| if !ok { | ||
| oi = 99 | ||
| } | ||
| oj, ok := levelOrder[result[j].Level] | ||
| if !ok { | ||
| oj = 99 | ||
| } | ||
| return oi < oj | ||
| }) | ||
|
|
||
| return result | ||
| } |
There was a problem hiding this comment.
可以移除,没必要这么复杂,一开始定义一个数组就可以了
levelCounts := []LevelCount{
{Level: "normal", Count: 0},
{Level: "notice", Count: 0},
{Level: "warn", Count: 0},
{Level: "error", Count: 0},
}
根据level类型,在对应下标的元素里面++,不需要在Map和Array/Slice之间杂耍
// LevelCount 等级统计
type LevelCount struct {
Level string json:"level" // normal/notice/warn/error
Count int json:"count"
}
User description
关联的 issue
描述你的变更
https://github.com/actiontech/sqle-ee/issues/2689
确认项(pr提交后操作)
Tip
请在指定复审人之前,确认并完成以下事项,完成后✅
not_compatibleneed_update_docDescription
新增构建审核报告数据及国际化标签的函数
添加 CSV 与 HTML 报告生成器及大量单元测试
修改下载报告接口支持 export_format 参数传入导出格式
更新接口文档与本地化文件,新增报告相关说明
Diagram Walkthrough
File Walkthrough
8 files
新增构建审核报告数据、提取规则和排序统计函数更新下载报告接口增加 export_format 参数处理新增 CSV 格式报告生成器实现及接口方法定义审核报告数据模型及报告生成器接口添加 CE 版报告导出入口及格式支持判断新增 HTML 报告生成器,使用模板防止 XSS实现嵌入 HTML 模板的读取及解析功能增加导出格式常量及 NormalizeExportFormatStr 函数3 files
添加审核报告数据功能的单元测试编写 CE 版报告导出相关测试,验证格式限制添加报告生成器(CSV/HTML)综合测试用例7 files
更新 API 文档中报告导出参数说明新增报告相关国际化标签定义更新 Swagger 文档中导出报告接口描述修改 Swagger YAML,新增 export_format 参数说明添加英文版报告标签国际化配置添加中文版报告标签国际化配置新增 HTML 报告模板,包含多模块报告样式