Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /dashboard /coverage_widget.py
| """ | |
| Coverage Dashboard Widget | |
| Real-time coverage visualization for Zen-AI-Pentest | |
| """ | |
| import json | |
| from dataclasses import dataclass | |
| from datetime import datetime | |
| from pathlib import Path | |
| from typing import Dict, List | |
| class CoverageMetric: | |
| """Single coverage metric""" | |
| module: str | |
| coverage: float | |
| lines_total: int | |
| lines_covered: int | |
| timestamp: datetime | |
| def to_dict(self) -> dict: | |
| return { | |
| "module": self.module, | |
| "coverage": round(self.coverage, 2), | |
| "lines_total": self.lines_total, | |
| "lines_covered": self.lines_covered, | |
| "timestamp": self.timestamp.isoformat(), | |
| } | |
| class CoverageDashboard: | |
| """Dashboard for coverage metrics""" | |
| def __init__(self, data_dir: str = "dashboard/data"): | |
| self.data_dir = Path(data_dir) | |
| self.data_dir.mkdir(parents=True, exist_ok=True) | |
| self.coverage_file = self.data_dir / "coverage_history.json" | |
| def load_history(self) -> List[dict]: | |
| """Load coverage history from file""" | |
| if not self.coverage_file.exists(): | |
| return [] | |
| with open(self.coverage_file, "r") as f: | |
| return json.load(f) | |
| def save_metric(self, metric: CoverageMetric): | |
| """Save a coverage metric""" | |
| history = self.load_history() | |
| history.append(metric.to_dict()) | |
| # Keep last 100 entries | |
| history = history[-100:] | |
| with open(self.coverage_file, "w") as f: | |
| json.dump(history, f, indent=2) | |
| def get_current_coverage(self) -> Dict[str, float]: | |
| """Get current coverage by module""" | |
| # These would be dynamically loaded in production | |
| return { | |
| "modules/cve_database.py": 99.0, | |
| "core/false_positive_filter.py": 90.0, | |
| "risk_engine/false_positive_engine.py": 87.0, | |
| "tools/integrations": 84.0, | |
| "modules/enhanced_recon.py": 57.0, | |
| "autonomous/memory.py": 57.0, | |
| "api/routes": 33.0, | |
| "database/models.py": 33.0, | |
| "core/cache.py": 24.0, | |
| "autonomous/tool_executor.py": 29.0, | |
| } | |
| def get_coverage_trend(self, days: int = 7) -> List[dict]: | |
| """Get coverage trend over time""" | |
| history = self.load_history() | |
| # Filter by date | |
| cutoff = datetime.now().timestamp() - (days * 86400) | |
| recent = [h for h in history if datetime.fromisoformat(h["timestamp"]).timestamp() > cutoff] | |
| return recent | |
| def generate_report(self) -> dict: | |
| """Generate comprehensive coverage report""" | |
| current = self.get_current_coverage() | |
| # Calculate overall | |
| overall = sum(current.values()) / len(current) if current else 0 | |
| # Categorize | |
| excellent = {k: v for k, v in current.items() if v >= 90} | |
| good = {k: v for k, v in current.items() if 70 <= v < 90} | |
| needs_work = {k: v for k, v in current.items() if 50 <= v < 70} | |
| critical = {k: v for k, v in current.items() if v < 50} | |
| return { | |
| "generated_at": datetime.now().isoformat(), | |
| "overall_coverage": round(overall, 2), | |
| "modules": { | |
| "excellent": len(excellent), | |
| "good": len(good), | |
| "needs_work": len(needs_work), | |
| "critical": len(critical), | |
| }, | |
| "coverage_by_module": current, | |
| "priority_targets": list(critical.keys())[:5], | |
| "recommendations": self._generate_recommendations(critical, needs_work), | |
| } | |
| def _generate_recommendations(self, critical: dict, needs_work: dict) -> List[str]: | |
| """Generate improvement recommendations""" | |
| recommendations = [] | |
| if critical: | |
| recommendations.append(f"🚨 Focus on {len(critical)} critical modules (<50% coverage)") | |
| recommendations.append(f" Top priority: {list(critical.keys())[0]}") | |
| if needs_work: | |
| recommendations.append(f"⚠️ Improve {len(needs_work)} modules (50-70% coverage)") | |
| if not critical and not needs_work: | |
| recommendations.append("✅ All modules above 50% - focus on 90% target") | |
| return recommendations | |
| def render_html(self) -> str: | |
| """Render HTML dashboard""" | |
| report = self.generate_report() | |
| html = f""" | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Zen-AI-Pentest Coverage Dashboard</title> | |
| <style> | |
| body {{ font-family: Arial, sans-serif; margin: 20px; background: #1a1a2e; color: #eee; }} | |
| .header {{ text-align: center; margin-bottom: 30px; }} | |
| .overall {{ font-size: 48px; text-align: center; margin: 20px 0; }} | |
| .overall.excellent {{ color: #4caf50; }} | |
| .overall.good {{ color: #8bc34a; }} | |
| .overall.warning {{ color: #ff9800; }} | |
| .overall.critical {{ color: #f44336; }} | |
| .modules {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 15px; }} | |
| .module {{ background: #16213e; padding: 15px; border-radius: 8px; }} | |
| .module-name {{ font-weight: bold; margin-bottom: 5px; }} | |
| .coverage-bar {{ height: 20px; background: #0f3460; border-radius: 10px; overflow: hidden; }} | |
| .coverage-fill {{ height: 100%; transition: width 0.3s; }} | |
| .coverage-fill.excellent {{ background: #4caf50; }} | |
| .coverage-fill.good {{ background: #8bc34a; }} | |
| .coverage-fill.warning {{ background: #ff9800; }} | |
| .coverage-fill.critical {{ background: #f44336; }} | |
| .stats {{ display: flex; justify-content: space-around; margin: 20px 0; }} | |
| .stat {{ text-align: center; }} | |
| .stat-value {{ font-size: 32px; font-weight: bold; }} | |
| .recommendations {{ background: #16213e; padding: 15px; border-radius: 8px; margin-top: 20px; }} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="header"> | |
| <h1>🛡️ Zen-AI-Pentest Coverage Dashboard</h1> | |
| <p>Generated: {report["generated_at"]}</p> | |
| </div> | |
| <div class="overall {"excellent" if report["overall_coverage"] >= 80 else "good" if report["overall_coverage"] >= 60 else "warning" if report["overall_coverage"] >= 40 else "critical"}"> | |
| {report["overall_coverage"]}% | |
| </div> | |
| <div class="stats"> | |
| <div class="stat"> | |
| <div class="stat-value" style="color: #4caf50">{report["modules"]["excellent"]}</div> | |
| <div>Excellent (90%+)</div> | |
| </div> | |
| <div class="stat"> | |
| <div class="stat-value" style="color: #8bc34a">{report["modules"]["good"]}</div> | |
| <div>Good (70-90%)</div> | |
| </div> | |
| <div class="stat"> | |
| <div class="stat-value" style="color: #ff9800">{report["modules"]["needs_work"]}</div> | |
| <div>Needs Work (50-70%)</div> | |
| </div> | |
| <div class="stat"> | |
| <div class="stat-value" style="color: #f44336">{report["modules"]["critical"]}</div> | |
| <div>Critical (<50%)</div> | |
| </div> | |
| </div> | |
| <h2>Module Coverage</h2> | |
| <div class="modules"> | |
| """ | |
| for module, coverage in report["coverage_by_module"].items(): | |
| css_class = ( | |
| "excellent" if coverage >= 90 else ("good" if coverage >= 70 else "warning" if coverage >= 50 else "critical") | |
| ) | |
| html += f""" | |
| <div class="module"> | |
| <div class="module-name">{module}</div> | |
| <div class="coverage-bar"> | |
| <div class="coverage-fill {css_class}" style="width: {coverage}%"></div> | |
| </div> | |
| <div style="text-align: right; margin-top: 5px">{coverage}%</div> | |
| </div> | |
| """ | |
| html += """ | |
| </div> | |
| <div class="recommendations"> | |
| <h2>📋 Recommendations</h2> | |
| <ul> | |
| """ | |
| for rec in report["recommendations"]: | |
| html += f" <li>{rec}</li>\n" | |
| html += """ | |
| </ul> | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| return html | |
| def save_html(self, output_path: str = "dashboard/coverage_report.html"): | |
| """Save HTML report to file""" | |
| html = self.render_html() | |
| with open(output_path, "w") as f: | |
| f.write(html) | |
| print(f"Coverage report saved to {output_path}") | |
| # CLI interface | |
| if __name__ == "__main__": | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Coverage Dashboard") | |
| parser.add_argument("--report", action="store_true", help="Generate JSON report") | |
| parser.add_argument("--html", action="store_true", help="Generate HTML report") | |
| parser.add_argument( | |
| "--output", | |
| default="dashboard/coverage_report.html", | |
| help="Output path", | |
| ) | |
| args = parser.parse_args() | |
| dashboard = CoverageDashboard() | |
| if args.report: | |
| report = dashboard.generate_report() | |
| print(json.dumps(report, indent=2)) | |
| if args.html: | |
| dashboard.save_html(args.output) | |
Xet Storage Details
- Size:
- 8.92 kB
- Xet hash:
- bab89a1c96a03066aeae47d175d5deafaafe3717ba52f178e3871db8629cf571
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.