Files
syops/backend/app/services/schedule/excel_service.py
T

46 lines
1.2 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Any
from openpyxl import Workbook
def export_schedule_result_excel(*, run_no: str, summary: dict, details: list[dict], root_dir: str) -> tuple[str | None, str | None, int]:
if not details:
return None, None, 0
out_root = Path(root_dir)
out_root.mkdir(parents=True, exist_ok=True)
rows: list[dict[str, Any]] = []
for row in details:
output = row.get('dify_output')
if not isinstance(output, dict):
output = {}
entry = dict(output)
for k in ('writeback_ok', 'error'):
if k in row:
entry[k] = row[k]
rows.append(entry)
headers: list[str] = []
for row in rows:
for k in row:
if k not in headers:
headers.append(k)
wb = Workbook()
ws = wb.active
ws.title = 'result'
ws.append(headers)
for row in rows:
ws.append([row.get(k, '') for k in headers])
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)