Reporting API
This module provides tools for generating compliance and audit reports from SecureML operations, helping to document and visualize privacy compliance for various regulations.
ReportGenerator Class
- class secureml.reporting.ReportGenerator(templates_dir: str | None = None, custom_css: str | None = None)
Class for generating compliance and audit reports.
The ReportGenerator class provides methods for creating HTML and PDF reports from audit logs and compliance checks.
- __init__(templates_dir: str | None = None, custom_css: str | None = None)
Initialize a report generator.
- Args:
templates_dir: Directory containing report templates custom_css: Custom CSS for HTML reports
- generate_audit_report(logs: List[Dict[str, Any]] | str | Dict[str, Any], output_file: str, title: str = 'Audit Trail Report', logo_path: str | None = None, include_charts: bool = True, additional_context: Dict[str, Any] | None = None) str
Generate an audit report.
- Args:
logs: The audit logs, operation ID, or operation name output_file: Path to write the report to title: Title for the report logo_path: Path to a logo image include_charts: Whether to include charts additional_context: Additional context data for the template
- Returns:
Path to the generated report
- generate_compliance_report(report: ComplianceReport, output_file: str, logo_path: str | None = None, include_charts: bool = True, additional_context: Dict[str, Any] | None = None) str
Generate a compliance report.
- Args:
report: The compliance report output_file: Path to write the report to logo_path: Path to a logo image include_charts: Whether to include charts additional_context: Additional context data for the template
- Returns:
Path to the generated report
The ReportGenerator class provides methods for creating HTML and PDF reports from audit logs and compliance checks.
Basic Usage Example:
from secureml.reporting import ReportGenerator
# Create a report generator
generator = ReportGenerator()
# Generate a compliance report
report_path = generator.generate_compliance_report(
report=compliance_report, # A ComplianceReport instance
output_file="compliance_report.html",
logo_path="company_logo.png", # Optional
include_charts=True # Include visualizations
)
print(f"Compliance report generated at: {report_path}")
Generating Audit Reports
# Generate an audit report from audit logs
audit_report_path = generator.generate_audit_report(
logs=audit_logs, # List of audit log entries
output_file="audit_report.pdf",
title="GDPR Compliance Audit Report",
logo_path="company_logo.png",
include_charts=True
)
You can also generate audit reports directly from operation IDs or query parameters:
# Generate an audit report from an operation ID
report_path = generator.generate_audit_report(
logs="12345-abcde-67890", # Operation ID
output_file="model_training_audit.html",
title="Model Training Audit Report"
)
# Generate an audit report using query parameters
report_path = generator.generate_audit_report(
logs={
"operation_name": "model_training",
"start_time": "2023-01-01T00:00:00",
"end_time": "2023-01-31T23:59:59"
},
output_file="january_training_audit.pdf"
)
Custom Report Templates
The ReportGenerator uses Jinja2 templates to render reports. By default, templates are stored in the templates directory within the SecureML package. You can customize these templates:
# Create a report generator with custom templates
generator = ReportGenerator(
templates_dir="/path/to/custom/templates",
custom_css="path/to/custom/style.css"
)
The default templates include:
- compliance_report.html: Template for compliance reports
- audit_report.html: Template for audit reports
- report_style.css: CSS styling for reports
ComplianceReport Extension
The module extends the ComplianceReport class with a generate_report method:
from secureml.compliance import check_compliance
# Run a compliance check
compliance_report = check_compliance(
data=my_dataset,
regulation="GDPR"
)
# Generate a report directly from the compliance report
report_path = compliance_report.generate_report(
output_file="compliance_report.pdf",
format="pdf", # or "html"
logo_path="logo.png",
include_charts=True
)
PDF Generation
The ReportGenerator can generate both HTML and PDF reports. PDF generation requires WeasyPrint:
# Generate a PDF report
pdf_path = generator.generate_compliance_report(
report=compliance_report,
output_file="report.pdf" # .pdf extension triggers PDF generation
)
Charts and Visualizations
The ReportGenerator can include visualizations in reports:
For compliance reports: Charts showing issues by severity
For audit reports: Charts showing events by type
These visualizations help to quickly understand the compliance status and audit activity.
Best Practices
Use descriptive titles: Provide clear titles for reports to make them easier to identify later
Include logos: Add organizational logos for official reports
Enable charts: Include visualizations for easier interpretation
Use PDF format: For official reports that need to be shared externally
Customize templates: Adapt templates to match organizational branding and requirements
Include context: Add additional context information to enhance report usefulness