""" report_generator.py Generates comprehensive HTML and Markdown reports from accuracy analysis results. """ import json import logging from pathlib import Path from datetime import datetime from typing import Dict, List, Any, Optional logger = logging.getLogger(__name__) class ReportGenerator: """Generates human-readable accuracy reports.""" def __init__(self): """Initialize report generator.""" self.timestamp = datetime.now() def generate_html_report( self, batch_analysis: Dict[str, Any], output_file: Optional[str] = None ) -> str: """ Generate comprehensive HTML report. Args: batch_analysis: Output from AccuracyAnalyzer.analyze_batch() output_file: Output file path (auto-generated if None) Returns: Path to generated HTML file """ if output_file is None: output_dir = Path(__file__).parent / "test_reports" output_dir.mkdir(exist_ok=True) timestamp = self.timestamp.strftime("%Y%m%d_%H%M%S") output_file = output_dir / f"accuracy_report_{timestamp}.html" output_file = Path(output_file) # Generate HTML html_content = self._generate_html_content(batch_analysis) with open(output_file, 'w', encoding='utf-8') as f: f.write(html_content) logger.info(f"HTML report generated: {output_file}") return str(output_file) def generate_markdown_report( self, batch_analysis: Dict[str, Any], output_file: Optional[str] = None ) -> str: """ Generate Markdown report. Args: batch_analysis: Output from AccuracyAnalyzer.analyze_batch() output_file: Output file path (auto-generated if None) Returns: Path to generated Markdown file """ if output_file is None: output_dir = Path(__file__).parent / "test_reports" output_dir.mkdir(exist_ok=True) timestamp = self.timestamp.strftime("%Y%m%d_%H%M%S") output_file = output_dir / f"accuracy_report_{timestamp}.md" output_file = Path(output_file) # Generate Markdown md_content = self._generate_markdown_content(batch_analysis) with open(output_file, 'w', encoding='utf-8') as f: f.write(md_content) logger.info(f"Markdown report generated: {output_file}") return str(output_file) def _generate_html_content(self, batch_analysis: Dict[str, Any]) -> str: """Generate HTML content for report.""" timestamp = self.timestamp.strftime("%Y-%m-%d %H:%M:%S") total = batch_analysis.get("total_tests", 0) passed = batch_analysis.get("passed_tests", 0) failed = batch_analysis.get("failed_tests", 0) avg_score = batch_analysis.get("average_score", 0) pass_rate = batch_analysis.get("pass_rate", 0) # Determine overall status color status_color = "#22c55e" if pass_rate >= 0.8 else "#f59e0b" if pass_rate >= 0.6 else "#ef4444" status_text = "✅ EXCELLENT" if pass_rate >= 0.9 else "✅ GOOD" if pass_rate >= 0.8 else "⚠️ FAIR" if pass_rate >= 0.6 else "❌ POOR" html = f"""
Generated on {timestamp}