"""Systemdiagnose und verstaendlicher Systembericht.""" from __future__ import annotations import os import shutil import sqlite3 from dataclasses import dataclass, field from pathlib import Path from rich.console import Console from rich.panel import Panel from rich.table import Table from ..config import Config from ..utils.formatting import format_size from ..utils.logging import get_logger from .gpu import ( GpuInfo, ResolvedExecution, RuntimeInfo, collect_runtime_info, detect_gpu, is_pascal_or_older, resolve_execution, ) logger = get_logger("system.diagnostics") CHECK = "[green]OK[/green]" WARN = "[yellow]Warnung[/yellow]" FAIL = "[red]nicht verfuegbar[/red]" @dataclass class SystemReport: """Gesammelte Systeminformationen.""" gpu: GpuInfo | None runtime: RuntimeInfo execution: ResolvedExecution db_path: Path db_exists: bool config_path: str | None model_dir: Path model_dir_writable: bool ffmpeg: str | None ffplay: str | None notes: list[str] = field(default_factory=list) open_tasks: list[str] = field(default_factory=list) @property def gpu_ready(self) -> bool: return self.runtime.cuda_runtime_usable and self.execution.device == "cuda" @property def verdict(self) -> str: if self.gpu_ready: return ( f"GPU-Transkription moeglich mit compute_type='{self.execution.compute_type}'." ) return "Keine GPU-Transkription moeglich - CPU-Fallback wird verwendet." def _is_writable(path: Path) -> bool: target = path while not target.exists() and target != target.parent: target = target.parent return target.is_dir() and os.access(target, os.W_OK) def collect_report(config: Config, *, requested_device: str | None = None, requested_compute_type: str | None = None) -> SystemReport: """Erstellt einen vollstaendigen Systembericht.""" gpu = detect_gpu() runtime = collect_runtime_info(check_fts=True) device = requested_device or config.transcription.device compute_type = requested_compute_type or config.transcription.compute_type execution = resolve_execution(device, compute_type, gpu, runtime) db_path = config.database_path model_dir = config.model_path notes: list[str] = [] open_tasks: list[str] = [] if gpu is not None and is_pascal_or_older(gpu): notes.append( f"{gpu.name} (Compute Capability {gpu.compute_capability}) ist eine Pascal-Karte: " "kein FP16/BF16, kein Tensor-Core-Betrieb. int8_float32 ist der passende Modus." ) if runtime.cudnn_libraries: notes.append( f"cuDNN gefunden ({len(runtime.cudnn_libraries)} Bibliothek(en)); " "SjAudioSearch laedt die pip-nvidia-Bibliotheken beim Start automatisch vor." ) else: notes.append( "Kein cuDNN gefunden. Fuer GPU-Betrieb die CUDA-12-Wheels installieren: " "pip install -e \".[cuda]\" (nvidia-cublas-cu12, nvidia-cudnn-cu12)." ) if not runtime.fts5_available: open_tasks.append( "SQLite ohne FTS5 - bitte Python mit FTS5-Unterstuetzung installieren." ) if not runtime.trigram_available: open_tasks.append( "Trigramm-Tokenizer nicht verfuegbar - die Kandidatensuche arbeitet nur ueber FTS5." ) return SystemReport( gpu=gpu, runtime=runtime, execution=execution, db_path=db_path, db_exists=db_path.exists(), config_path=str(config.source_path) if config.source_path else None, model_dir=model_dir, model_dir_writable=_is_writable(model_dir.parent if not model_dir.exists() else model_dir), ffmpeg=shutil.which("ffmpeg"), ffplay=shutil.which("ffplay"), notes=notes, open_tasks=open_tasks, ) def render_report(report: SystemReport, console: Console) -> None: """Gibt den Systembericht uebersichtlich im Terminal aus.""" console.print(Panel.fit("[bold]SjAudioSearch - Systemdiagnose[/bold]", border_style="cyan")) system = Table(show_header=False, box=None, padding=(0, 1)) system.add_column(style="bold cyan", no_wrap=True) system.add_column() system.add_row("Python", report.runtime.python_version) system.add_row("System", report.runtime.platform) system.add_row("SQLite", report.runtime.sqlite_version) system.add_row("FTS5", CHECK if report.runtime.fts5_available else FAIL) system.add_row("FTS5-Trigramme", CHECK if report.runtime.trigram_available else WARN) system.add_row("CTranslate2", report.runtime.ctranslate2_version or FAIL) system.add_row("FFmpeg", report.ffmpeg or WARN) system.add_row("ffplay", report.ffplay or WARN) system.add_row("Konfiguration", report.config_path or "eingebaute Defaults") system.add_row("Datenbank", f"{report.db_path} ({'vorhanden' if report.db_exists else 'wird angelegt'})") system.add_row("Modellordner", f"{report.model_dir} ({'beschreibbar' if report.model_dir_writable else 'NICHT beschreibbar'})") console.print(system) console.print() gpu_table = Table(title="GPU", show_header=False, box=None, padding=(0, 1)) gpu_table.add_column(style="bold cyan", no_wrap=True) gpu_table.add_column() if report.gpu is None: gpu_table.add_row("Status", FAIL + " (nvidia-smi lieferte keine Daten)") else: gpu_table.add_row("Modell", report.gpu.name) gpu_table.add_row("Treiber", report.gpu.driver_version or "unbekannt") gpu_table.add_row("CUDA (Treiber)", report.gpu.cuda_driver_version or "unbekannt") gpu_table.add_row( "VRAM", f"{report.gpu.memory_total_mb} MB" if report.gpu.memory_total_mb else "unbekannt" ) gpu_table.add_row("Compute Capability", report.gpu.compute_capability or "unbekannt") gpu_table.add_row("Architektur", "Pascal oder aelter" if is_pascal_or_older(report.gpu) else "neuere Architektur") gpu_table.add_row( "CUDA-Geraete (CTranslate2)", str(report.runtime.cuda_device_count) ) gpu_table.add_row( "Compute Types (CUDA)", ", ".join(report.runtime.cuda_compute_types) or "keine", ) gpu_table.add_row( "Compute Types (CPU)", ", ".join(report.runtime.cpu_compute_types) or "keine", ) gpu_table.add_row( "CUDA-Bibliotheken", ", ".join(Path(lib).name for lib in report.runtime.cuda_libraries) or "keine im System", ) gpu_table.add_row( "cuDNN-Bibliotheken", ", ".join(Path(lib).name for lib in report.runtime.cudnn_libraries) or "keine im System", ) console.print(gpu_table) console.print() plan = Table(title="Empfohlener Ausfuehrungsmodus", show_header=False, box=None, padding=(0, 1)) plan.add_column(style="bold cyan", no_wrap=True) plan.add_column() plan.add_row("Angefragt", f"{report.execution.requested_device} / {report.execution.requested_compute_type}") plan.add_row("Verwendet", f"{report.execution.device} / {report.execution.compute_type}") plan.add_row("FP16/BF16", "nicht verwendet" if report.execution.device == "cuda" else "irrelevant (CPU)") console.print(plan) for note in report.execution.notes: console.print(f"[yellow]-[/yellow] {note}") for note in report.notes: console.print(f"[cyan]-[/cyan] {note}") console.print() color = "green" if report.gpu_ready else "yellow" console.print(Panel(report.verdict, border_style=color, title="Ergebnis")) if report.open_tasks: console.print() console.print("[bold red]Offene Punkte:[/bold red]") for task in report.open_tasks: console.print(f" [red]*[/red] {task}") def database_summary(db_path: Path) -> dict[str, object] | None: """Kurze Zusammenfassung einer vorhandenen Datenbank (fuer die Diagnose).""" if not db_path.exists(): return None try: conn = sqlite3.connect(str(db_path)) try: tables = { row[0] for row in conn.execute( "SELECT name FROM sqlite_master WHERE type IN ('table','view')" ) } audio_count = 0 if "audio_files" in tables: audio_count = int(conn.execute("SELECT COUNT(*) FROM audio_files").fetchone()[0]) return { "tables": sorted(tables), "audio_files": audio_count, "size": format_size(db_path.stat().st_size), } finally: conn.close() except sqlite3.Error as exc: logger.warning("Datenbank konnte nicht gelesen werden: %s", exc) return None