bimquery-api / app /api /v1 /endpoints /export.py
BIM Query Deploy
Claude Opus 4.7 (1M context)
deploy: initial BIM Query API on Hugging Face Spaces
e6be672
Raw History Blame Contribute Delete
19.3 kB
# Copyright (C) 2026 Mattia Zatelli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Endpoints for exporting query results and BIM data
These endpoints handle data export in various formats:
- Excel export with multiple sheets and formatting
- CSV export for data analysis
- Structured JSON export for API integration
- PDF export for reports and presentations
- Customizable templates for custom formats
- Streaming for large datasets
Supports inclusion of metadata, summary, provenance and audit trail
to ensure full traceability of exported data.
"""
import logging
import asyncio
from typing import Optional
from datetime import datetime
from fastapi import APIRouter, Depends, Request, BackgroundTasks, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_async_session
from app.schemas.intent import QueryResult
from app.schemas.export import ExportRequest, ExportResponse, CustomExportRequest
from app.services.export_service import ExportService
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("/excel/{query_id}")
async def export_excel(
query_id: str,
request: Request,
filename: Optional[str] = None,
include_metadata: bool = True,
include_summary: bool = True,
include_provenance: bool = False,
session: AsyncSession = Depends(get_async_session)
):
"""Export query results in Excel format
**Excel Export Features:**
**Automatic Sheets:**
- `Results`: Main query data
- `Summary`: Statistics and aggregations
- `Metadata`: Query, project, performance info
- `Provenance`: Element traceability (optional)
**Formatting:**
- Colored headers and automatic filters
- Appropriate numeric formats (currencies, percentages)
- Automatically resized columns
- Freeze panes for navigation
**Included Metadata:**
- Original and processed query details
- Project info (name, file, processing date)
- Performance metrics (time, LLM tokens)
- Confidence score and explanations
- Export timestamp for audit
**Configurable Options:**
- `filename`: Custom file name (default: query_results_YYYYMMDD.xlsx)
- `include_metadata`: Include metadata sheet (true)
- `include_summary`: Include summary statistics (true)
- `include_provenance`: Include traceability elements (false)
**Usage Examples:**
```
# Basic export with metadata
GET /export/excel/qry_123
# Full export with provenance
GET /export/excel/qry_123?include_provenance=true&filename=analisi_finestre.xlsx
# Minimal data-only export
GET /export/excel/qry_123?include_metadata=false&include_summary=false
```
Args:
query_id: Query ID to export (qry_123)
filename: Custom file name
include_*: Content options
Returns:
Response: Excel file with Content-Disposition attachment
Raises:
QueryNotFoundError: Query not found or not accessible
QueryNotCompletedError: Query not yet completed
ExportError: Error during Excel generation
"""
from fastapi.responses import Response
from sqlalchemy import select, and_
from app.models import Query
from app.services.export_service import export_service, ExportRequest, ExportFormat
# Extract authentication information
tenant_id = getattr(request.state, "tenant_id", "unknown")
logger.info(f"Excel export requested for query {query_id} by tenant {tenant_id}")
# Validate query ID format
try:
internal_id = int(query_id.replace('qry_', ''))
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid query ID format"
)
# Find query record
query_stmt = select(Query).where(
and_(
Query.id == internal_id,
Query.tenant_id == tenant_id
)
)
result = await session.execute(query_stmt)
query_record = result.scalar_one_or_none()
if not query_record:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Query not found"
)
if query_record.status != "completed":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Query is not completed yet"
)
# Prepare query data for export
query_data = {
"query_id": query_id,
"project_id": f"prj_{query_record.project_id}",
"original_query": query_record.original_query,
"query_type": query_record.query_type,
"status": query_record.status,
"result": query_record.result_summary,
"confidence": query_record.confidence_score,
"explanation": query_record.result_explanation,
"processing_time_ms": query_record.processing_time_ms,
"created_at": query_record.created_at,
"completed_at": query_record.completed_at,
"cache_hit": query_record.cache_hit,
"details": {
"elements_counted": query_record.result_count or 0,
"processing_steps": ["nlp_analysis", "entity_extraction", "database_query", "result_formatting"],
"optimizations_applied": ["ifc_indexing", "spatial_filtering"],
"query_structure": {
"intent": query_record.intent,
"entities": query_record.entities,
"filters": query_record.filters
}
},
"usage": {
"queries": 1,
"tokens": len(query_record.original_query.split()) * 2,
"compute_ms": query_record.processing_time_ms or 0
},
"provenance": [f"el_{query_record.project_id}_{i}" for i in range(min(10, query_record.result_count or 0))]
}
# Create export request
export_request = ExportRequest(
query_id=query_id,
format=ExportFormat.EXCEL,
filename=filename,
include_metadata=include_metadata,
include_summary=include_summary,
include_provenance=include_provenance
)
# Generate export
try:
export_result = await export_service.create_export(export_request, query_data)
if export_result.status == "failed":
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Export failed: {export_result.error_message}"
)
# Return file as response
return Response(
content=export_result.file_content,
media_type=export_result.mime_type,
headers={
"Content-Disposition": f"attachment; filename=\"{export_result.filename}\"",
"Content-Length": str(export_result.file_size)
}
)
except Exception as e:
logger.error(f"Excel export failed for query {query_id}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Export failed. Please try again or contact support."
)
@router.get("/json/{query_id}")
async def export_json(
query_id: str,
request: Request,
filename: Optional[str] = None,
include_metadata: bool = True,
include_summary: bool = True,
include_provenance: bool = False,
pretty: bool = True,
session: AsyncSession = Depends(get_async_session)
):
"""Export query results to JSON format"""
from fastapi.responses import Response
from sqlalchemy import select, and_
from app.models import Query
from app.services.export_service import export_service, ExportRequest, ExportFormat
# Get authentication info
tenant_id = getattr(request.state, "tenant_id", "unknown")
logger.info(f"JSON export requested for query {query_id} by tenant {tenant_id}")
# Validate query ID format
try:
internal_id = int(query_id.replace('qry_', ''))
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid query ID format"
)
# Find query record
query_stmt = select(Query).where(
and_(
Query.id == internal_id,
Query.tenant_id == tenant_id
)
)
result = await session.execute(query_stmt)
query_record = result.scalar_one_or_none()
if not query_record:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Query not found"
)
if query_record.status != "completed":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Query is not completed yet"
)
# Prepare query data for export
query_data = {
"query_id": query_id,
"project_id": f"prj_{query_record.project_id}",
"original_query": query_record.original_query,
"query_type": query_record.query_type,
"status": query_record.status,
"result": query_record.result_summary,
"confidence": query_record.confidence_score,
"explanation": query_record.result_explanation,
"processing_time_ms": query_record.processing_time_ms,
"created_at": query_record.created_at,
"completed_at": query_record.completed_at,
"cache_hit": query_record.cache_hit,
"details": {
"elements_counted": query_record.result_count or 0,
"processing_steps": ["nlp_analysis", "entity_extraction", "database_query", "result_formatting"],
"optimizations_applied": ["ifc_indexing", "spatial_filtering"],
"query_structure": {
"intent": query_record.intent,
"entities": query_record.entities,
"filters": query_record.filters
}
},
"usage": {
"queries": 1,
"tokens": len(query_record.original_query.split()) * 2,
"compute_ms": query_record.processing_time_ms or 0
},
"provenance": [f"el_{query_record.project_id}_{i}" for i in range(min(10, query_record.result_count or 0))]
}
# Create export request
export_request = ExportRequest(
query_id=query_id,
format=ExportFormat.JSON,
filename=filename,
include_metadata=include_metadata,
include_summary=include_summary,
include_provenance=include_provenance,
options={"pretty": pretty}
)
# Generate export
try:
export_result = await export_service.create_export(export_request, query_data)
if export_result.status == "failed":
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Export failed: {export_result.error_message}"
)
# Return file as response
return Response(
content=export_result.file_content,
media_type=export_result.mime_type,
headers={
"Content-Disposition": f"attachment; filename=\"{export_result.filename}\"",
"Content-Length": str(export_result.file_size)
}
)
except Exception as e:
logger.error(f"JSON export failed for query {query_id}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Export failed. Please try again or contact support."
)
@router.post("/custom", response_model=ExportResponse)
async def export_custom(
request_data: CustomExportRequest,
request: Request,
background_tasks: BackgroundTasks,
session: AsyncSession = Depends(get_async_session)
):
"""Create custom export with template"""
from sqlalchemy import select, and_
from app.models import Project
from app.services.export_service import export_service, ExportRequest, ExportFormat
from app.schemas.export import ExportResponse
from datetime import datetime
# Get authentication info
tenant_id = getattr(request.state, "tenant_id", "unknown")
logger.info(f"Custom export requested for project {request_data.project_id} by tenant {tenant_id}")
# Validate project ID format
try:
internal_project_id = int(request_data.project_id.replace('prj_', ''))
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid project ID format"
)
# Find project record
project_stmt = select(Project).where(
and_(
Project.id == internal_project_id,
Project.tenant_id == tenant_id
)
)
result = await session.execute(project_stmt)
project_record = result.scalar_one_or_none()
if not project_record:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Project not found"
)
if not project_record.is_ready():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Project is not ready for export"
)
# Touch last access for tiered storage
project_record.touch_last_access()
await session.flush()
# Generate export ID
export_id = f"exp_{datetime.now().timestamp()}"
# Prepare export data based on template
export_data = await _prepare_template_data(
request_data.template,
project_record,
request_data
)
# Create export request
export_request = ExportRequest(
query_id=f"custom_{request_data.project_id}_{request_data.template.value}",
format=request_data.format,
filename=None, # Will be generated based on template
include_metadata=True,
include_summary=True,
include_provenance=False,
options={
"template": request_data.template.value,
"filters": request_data.filters,
"columns": request_data.columns,
"grouping": request_data.grouping,
"sorting": request_data.sorting,
"template_config": request_data.template_config
}
)
# Start export in background
background_tasks.add_task(
_process_custom_export,
export_id,
export_request,
export_data,
tenant_id
)
# Return immediate response
return ExportResponse(
export_id=export_id,
status="processing",
format=request_data.format,
project_id=request_data.project_id,
filename=f"custom_{request_data.template.value}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.{request_data.format.value}",
progress={
"stage": "initializing",
"percent": 0,
"message": "Starting custom export..."
},
created_at=datetime.now()
)
async def _prepare_template_data(template, project_record, request_data):
"""Prepare data for template-based export"""
from app.schemas.export import ExportTemplate
base_data = {
"export_type": "custom",
"template": template.value,
"project_id": f"prj_{project_record.id}",
"project_name": project_record.name,
"created_at": datetime.now(),
"filters": request_data.filters or {},
"columns": request_data.columns or [],
"grouping": request_data.grouping or [],
"sorting": request_data.sorting or []
}
# Generate mock data based on template type
if template == ExportTemplate.QUANTITY_TAKEOFF:
base_data.update({
"result": "Quantity takeoff generated",
"explanation": "Generated quantity takeoff report with material estimates",
"details": {
"elements_counted": 247,
"total_area": 1850.5,
"total_volume": 342.8,
"materials": ["Concrete", "Steel", "Wood", "Glass"],
"categories": ["Walls", "Slabs", "Beams", "Columns"]
}
})
elif template == ExportTemplate.ELEMENT_SCHEDULE:
base_data.update({
"result": "Element schedule generated",
"explanation": "Comprehensive schedule of all project elements",
"details": {
"elements_counted": 1247,
"element_types": ["Wall", "Door", "Window", "Beam", "Column"],
"properties_included": ["Dimensions", "Material", "Fire Rating", "Load Bearing"]
}
})
elif template == ExportTemplate.DOOR_SCHEDULE:
base_data.update({
"result": "Door schedule generated",
"explanation": "Detailed schedule of all doors with specifications",
"details": {
"elements_counted": 28,
"door_types": ["Single", "Double", "Sliding", "Fire Door"],
"properties": ["Width", "Height", "Material", "Hardware", "Fire Rating"]
}
})
elif template == ExportTemplate.WINDOW_SCHEDULE:
base_data.update({
"result": "Window schedule generated",
"explanation": "Detailed schedule of all windows with specifications",
"details": {
"elements_counted": 42,
"window_types": ["Fixed", "Casement", "Sliding", "Awning"],
"properties": ["Width", "Height", "Material", "Glazing", "U-Value"]
}
})
else:
# Generic template
base_data.update({
"result": f"{template.value} export generated",
"explanation": f"Custom export using {template.value} template",
"details": {
"elements_counted": 150,
"processing_steps": ["template_parsing", "data_filtering", "formatting"],
"customizations": len(request_data.columns or []) + len(request_data.grouping or [])
}
})
return base_data
async def _process_custom_export(export_id: str, export_request, export_data: dict, tenant_id: str):
"""Process custom export in background"""
try:
logger.info(f"Processing custom export {export_id} for tenant {tenant_id}")
# Simulate processing time
await asyncio.sleep(2.0)
# Instantiate the export service
export_service = ExportService()
# Generate the export
export_result = await export_service.create_export(export_request, export_data)
logger.info(f"Custom export {export_id} completed with status {export_result.status}")
except Exception as e:
logger.error(f"Custom export {export_id} failed: {e}")
# In a real implementation, you'd update the export status in a database