1.Excel文件字段顺序调整

2.测试排程时增加执行记录,编号与其他场景编号分开
This commit is contained in:
hugo committed 2026-05-20 18:50:53 +08:00
1 parent ddc461f548
commit ae6ced00b5
10 files changed
+194 -76

No files matched your search

+63 -38
View File
@@ -1,7 +1,7 @@
from __future__ import annotations
from __future__ import annotations
import io
import json
from pathlib import Path
from typing import Any
from openpyxl import Workbook
@@ -13,75 +13,101 @@ def _flatten_value(v: Any) -> Any:
return v
_EXPORT_SKIP_KEYS = {"dify_output", "json_output", "output", "_detail_rows", "detail_text"}
def export_schedule_result_excel(
*,
run_no: str,
summary: dict,
details: list[dict],
root_dir: str,
header_order: list[str] | None = None,
header_labels: dict[str, str] | None = None,
) -> tuple[str | None, str | None, int]:
detail_alias_order: list[str] | None = None,
) -> tuple[bytes | None, str | None, int]:
if not details:
return None, None, 0
out_root = Path(root_dir)
out_root.mkdir(parents=True, exist_ok=True)
header_order = header_order or []
header_labels = header_labels or {}
detail_alias_order = detail_alias_order or []
rows: list[dict[str, Any]] = []
sql_headers: list[str] = []
json_headers: list[str] = []
exec_result_headers: list[str] = []
exec_result_order = ["dify_result", "dify_message", "writeback_srm", "writeback_err"]
sql_headers: list[str] = []
detail_headers: list[str] = []
exec_result_headers: list[str] = []
def _append_unique(target: list[str], key: str) -> None:
if key not in target:
target.append(key)
def _collect_json_headers(obj: Any) -> None:
if isinstance(obj, dict):
for k in obj.keys():
_append_unique(json_headers, str(k))
for row in details:
if not isinstance(row, dict):
continue
flat_row: dict[str, Any] = {}
for k, v in row.items():
for k in row.keys():
key = str(k)
if key in {"dify_output", "json_output", "output"}:
_collect_json_headers(v)
if isinstance(v, dict):
for json_key, json_val in v.items():
flat_row[str(json_key)] = _flatten_value(json_val)
if key in _EXPORT_SKIP_KEYS:
continue
flat_row[key] = _flatten_value(v)
if key in exec_result_order:
_append_unique(exec_result_headers, key)
else:
_append_unique(sql_headers, key)
detail_rows = row.get("_detail_rows")
if isinstance(detail_rows, list):
for dr in detail_rows:
if isinstance(dr, dict):
for dk in dr.keys():
_append_unique(detail_headers, str(dk))
rows.append(flat_row)
if not rows:
return None, None, 0
ordered_exec_headers = [k for k in exec_result_order if k in exec_result_headers]
other_exec_headers = [k for k in exec_result_headers if k not in exec_result_order]
ordered_sql_headers = [k for k in header_order if k in sql_headers]
other_sql_headers = [k for k in sql_headers if k not in header_order]
ordered_detail_headers = [k for k in detail_alias_order if k in detail_headers]
other_detail_headers = [k for k in detail_headers if k not in detail_alias_order]
ordered_exec_headers = [k for k in exec_result_order if k in exec_result_headers]
other_exec_headers = [k for k in exec_result_headers if k not in exec_result_order]
all_detail_headers = ordered_detail_headers + other_detail_headers
headers = (
ordered_sql_headers
+ other_sql_headers
+ [k for k in json_headers if k not in sql_headers and k not in exec_result_headers]
+ all_detail_headers
+ ordered_exec_headers
+ other_exec_headers
)
if not headers:
return None, None, 0
rows: list[dict[str, Any]] = []
for row in details:
if not isinstance(row, dict):
continue
base_row: dict[str, Any] = {}
for k, v in row.items():
key = str(k)
if key in _EXPORT_SKIP_KEYS:
continue
if key in {"dify_output", "json_output", "output"}:
if isinstance(v, dict):
for json_key, json_val in v.items():
base_row[str(json_key)] = _flatten_value(json_val)
continue
base_row[key] = _flatten_value(v)
detail_rows = row.get("_detail_rows")
if all_detail_headers and isinstance(detail_rows, list) and detail_rows:
for dr in detail_rows:
flat = dict(base_row)
if isinstance(dr, dict):
for dk in all_detail_headers:
flat[dk] = dr.get(dk, "")
rows.append(flat)
else:
rows.append(base_row)
wb = Workbook()
ws = wb.active
ws.title = "result"
@@ -90,9 +116,8 @@ def export_schedule_result_excel(
for row in rows:
ws.append([row.get(k, "") for k in headers])
buf = io.BytesIO()
wb.save(buf)
file_data = buf.getvalue()
file_name = f"schedule_{run_no}.xlsx"
file_path = out_root / file_name
wb.save(file_path)
size = file_path.stat().st_size
return str(file_path), file_name, int(size)
return file_data, file_name, len(file_data)