Records a single strategy usage.
| 23 | |
| 24 | |
| 25 | class StrategyRecord: |
| 26 | """Records a single strategy usage.""" |
| 27 | |
| 28 | def __init__(self, strategy: str, error_type: str, success: bool, |
| 29 | duration: float = 0.0, context: Dict = None): |
| 30 | self.strategy = strategy |
| 31 | self.error_type = error_type |
| 32 | self.success = success |
| 33 | self.duration = duration |
| 34 | self.context = context or {} |
| 35 | self.timestamp = datetime.now().isoformat() |
| 36 | |
| 37 | def to_dict(self) -> Dict: |
| 38 | return { |
| 39 | "strategy": self.strategy, |
| 40 | "error_type": self.error_type, |
| 41 | "success": self.success, |
| 42 | "duration": self.duration, |
| 43 | "context": self.context, |
| 44 | "timestamp": self.timestamp, |
| 45 | } |
| 46 | |
| 47 | @classmethod |
| 48 | def from_dict(cls, data: Dict) -> "StrategyRecord": |
| 49 | return cls( |
| 50 | strategy=data["strategy"], |
| 51 | error_type=data["error_type"], |
| 52 | success=data.get("success", False), |
| 53 | duration=data.get("duration", 0.0), |
| 54 | context=data.get("context", {}), |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | class StrategyStats: |