Statistics for a single strategy.
| 56 | |
| 57 | |
| 58 | class StrategyStats: |
| 59 | """Statistics for a single strategy.""" |
| 60 | |
| 61 | def __init__(self, strategy: str): |
| 62 | self.strategy = strategy |
| 63 | self.total_attempts = 0 |
| 64 | self.successes = 0 |
| 65 | self.failures = 0 |
| 66 | self.total_duration = 0.0 |
| 67 | self.last_used: Optional[str] = None |
| 68 | self.error_breakdown: Dict[str, int] = defaultdict(int) |
| 69 | |
| 70 | @property |
| 71 | def success_rate(self) -> float: |
| 72 | if self.total_attempts == 0: |
| 73 | return 0.0 |
| 74 | return self.successes / self.total_attempts |
| 75 | |
| 76 | @property |
| 77 | def avg_duration(self) -> float: |
| 78 | if self.total_attempts == 0: |
| 79 | return 0.0 |
| 80 | return self.total_duration / self.total_attempts |
| 81 | |
| 82 | def record(self, success: bool, duration: float, error_type: str): |
| 83 | self.total_attempts += 1 |
| 84 | if success: |
| 85 | self.successes += 1 |
| 86 | else: |
| 87 | self.failures += 1 |
| 88 | self.total_duration += duration |
| 89 | self.last_used = datetime.now().isoformat() |
| 90 | self.error_breakdown[error_type] += 1 |
| 91 | |
| 92 | def to_dict(self) -> Dict: |
| 93 | return { |
| 94 | "strategy": self.strategy, |
| 95 | "total_attempts": self.total_attempts, |
| 96 | "successes": self.successes, |
| 97 | "failures": self.failures, |
| 98 | "success_rate": self.success_rate, |
| 99 | "avg_duration": self.avg_duration, |
| 100 | "last_used": self.last_used, |
| 101 | "error_breakdown": dict(self.error_breakdown), |
| 102 | } |
| 103 | |
| 104 | @classmethod |
| 105 | def from_dict(cls, data: Dict) -> "StrategyStats": |
| 106 | stats = cls(data["strategy"]) |
| 107 | stats.total_attempts = data["total_attempts"] |
| 108 | stats.successes = data["successes"] |
| 109 | stats.failures = data["failures"] |
| 110 | stats.total_duration = data.get("total_duration", 0.0) |
| 111 | stats.last_used = data.get("last_used") |
| 112 | stats.error_breakdown = defaultdict(int, data.get("error_breakdown", {})) |
| 113 | return stats |
| 114 | |
| 115 |
no outgoing calls
no test coverage detected