"""
Worker Pool Performance-Metriken.

Gemeinsame Datenstrukturen für Performance- und Ressourcenverbrauch-Metriken
der Worker-Pools (Saxon, FOP).
"""

from dataclasses import dataclass, field


@dataclass
class WorkerPoolMetrics:
    """
    Metriken für Worker-Pool Performance und Ressourcenverbrauch.
    """

    # Kompilierungszeit
    compilation_time_seconds: float = 0.0

    # Worker-Start-Zeiten
    worker_start_times: list[float] = field(default_factory=list)
    total_worker_start_time_seconds: float = 0.0
    average_worker_start_time_seconds: float = 0.0

    # RAM-Verbrauch (in MB)
    ram_before_transform_mb_per_worker: list[float] = field(default_factory=list)
    ram_after_transform_mb_per_worker: list[float] = field(default_factory=list)
    total_ram_before_mb: float = 0.0
    total_ram_after_mb: float = 0.0
    average_ram_before_mb: float = 0.0
    average_ram_after_mb: float = 0.0

    # XSL-Kompilierungszeiten (nur für Saxon)
    xsl_compilation_times: list[float] = field(default_factory=list)
    total_xsl_compilation_time_seconds: float = 0.0
    average_xsl_compilation_time_seconds: float = 0.0

    def calculate_aggregates(self):
        """Berechnet aggregierte Werte (Summen, Durchschnitte)."""
        # Worker-Start-Zeiten
        if self.worker_start_times:
            self.total_worker_start_time_seconds = sum(self.worker_start_times)
            self.average_worker_start_time_seconds = self.total_worker_start_time_seconds / len(
                self.worker_start_times
            )

        # RAM vor Transformation
        if self.ram_before_transform_mb_per_worker:
            self.total_ram_before_mb = sum(self.ram_before_transform_mb_per_worker)
            self.average_ram_before_mb = self.total_ram_before_mb / len(self.ram_before_transform_mb_per_worker)

        # RAM nach Transformation
        if self.ram_after_transform_mb_per_worker:
            self.total_ram_after_mb = sum(self.ram_after_transform_mb_per_worker)
            self.average_ram_after_mb = self.total_ram_after_mb / len(self.ram_after_transform_mb_per_worker)

        # XSL-Kompilierungszeiten
        if self.xsl_compilation_times:
            self.total_xsl_compilation_time_seconds = sum(self.xsl_compilation_times)
            self.average_xsl_compilation_time_seconds = self.total_xsl_compilation_time_seconds / len(
                self.xsl_compilation_times
            )
