Additional bugfixes
This commit is contained in:
@@ -23,5 +23,5 @@ def get_executor() -> ThreadPoolExecutor:
|
||||
def shutdown_executor() -> None:
|
||||
global _executor
|
||||
if _executor is not None:
|
||||
_executor.shutdown(wait=False)
|
||||
_executor.shutdown(wait=True)
|
||||
_executor = None
|
||||
|
||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import io
|
||||
|
||||
import openpyxl
|
||||
from reportlab.lib import colors
|
||||
from reportlab.lib.pagesizes import A4, landscape
|
||||
from reportlab.lib.styles import getSampleStyleSheet
|
||||
@@ -57,6 +58,19 @@ def _pdf_table(rows: list[dict]) -> Table:
|
||||
return t
|
||||
|
||||
|
||||
def to_xlsx_bytes(rows: list[dict]) -> bytes:
|
||||
"""Serialise *rows* to a single-sheet XLSX workbook and return the raw bytes."""
|
||||
wb = openpyxl.Workbook()
|
||||
ws = wb.active
|
||||
if rows:
|
||||
ws.append(list(rows[0].keys()))
|
||||
for row in rows:
|
||||
ws.append([str(v) if v is not None else "" for v in row.values()])
|
||||
buf = io.BytesIO()
|
||||
wb.save(buf)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def to_pdf_bytes(rows: list[dict], title: str, subtitle: str = "") -> bytes:
|
||||
"""Serialise *rows* to a single-sheet PDF and return the raw bytes."""
|
||||
buf = io.BytesIO()
|
||||
|
||||
@@ -14,7 +14,7 @@ from sqlalchemy.orm import sessionmaker, Session
|
||||
from app.core.audit import ExportRecord, append_audit, current_span_context
|
||||
from app.core.config import settings
|
||||
from app.core.executor import get_executor
|
||||
from app.core.export import to_pdf_bytes
|
||||
from app.core.export import to_pdf_bytes, to_xlsx_bytes
|
||||
from app.core.security import FrontendPrincipal, require_frontend_principal
|
||||
from app.domain.wwi import analytics
|
||||
|
||||
@@ -379,6 +379,24 @@ async def export_wwi_business_events(
|
||||
data = await asyncio.get_running_loop().run_in_executor(
|
||||
get_executor(), lambda: analytics.get_business_events(pg_factory, limit=limit)
|
||||
)
|
||||
|
||||
if format == "xlsx":
|
||||
today = datetime.now(timezone.utc).strftime("%Y%m%d")
|
||||
filename = f"wwi_business_events_{today}.xlsx"
|
||||
trace_id, span_id = current_span_context()
|
||||
|
||||
def _build_xlsx():
|
||||
content = to_xlsx_bytes(data)
|
||||
_record_export(pg_factory, "wwi", "business-events", "xlsx", filters,
|
||||
len(data), len(content), actor_id, trace_id, span_id)
|
||||
return content
|
||||
|
||||
content = await asyncio.get_running_loop().run_in_executor(get_executor(), _build_xlsx)
|
||||
return Response(
|
||||
content=content, media_type=_XLSX_MEDIA,
|
||||
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
|
||||
)
|
||||
|
||||
return await asyncio.get_running_loop().run_in_executor(
|
||||
get_executor(),
|
||||
lambda: _make_pdf(data, "wwi_business_events",
|
||||
|
||||
Reference in New Issue
Block a user