Spaces:
Runtime error
Runtime error
Delete event.py
Browse files
event.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Event models for the reliability framework.
|
| 3 |
-
Includes ReliabilityEvent, HealingAction, PolicyCondition, etc.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from pydantic import BaseModel, Field, field_validator, computed_field, ConfigDict
|
| 7 |
-
from typing import Optional, List, Literal, Tuple
|
| 8 |
-
from enum import Enum
|
| 9 |
-
from datetime import datetime, timezone
|
| 10 |
-
import hashlib
|
| 11 |
-
import re
|
| 12 |
-
|
| 13 |
-
# Note: The following constants are not used directly in this file,
|
| 14 |
-
# but they are kept for potential future extensions or consistency with other modules.
|
| 15 |
-
# from agentic_reliability_framework.core.config.constants import (
|
| 16 |
-
# LATENCY_WARNING, LATENCY_CRITICAL, LATENCY_EXTREME,
|
| 17 |
-
# ERROR_RATE_WARNING, ERROR_RATE_HIGH, ERROR_RATE_CRITICAL,
|
| 18 |
-
# CPU_WARNING, CPU_CRITICAL,
|
| 19 |
-
# MEMORY_WARNING, MEMORY_CRITICAL
|
| 20 |
-
# )
|
| 21 |
-
|
| 22 |
-
def validate_component_id(component: str) -> Tuple[bool, str]:
|
| 23 |
-
"""
|
| 24 |
-
Validate component ID format (alphanumeric and hyphens only).
|
| 25 |
-
Returns (is_valid, error_message).
|
| 26 |
-
"""
|
| 27 |
-
if not isinstance(component, str):
|
| 28 |
-
return False, "Component ID must be a string"
|
| 29 |
-
if not (1 <= len(component) <= 255):
|
| 30 |
-
return False, "Component ID must be 1-255 characters"
|
| 31 |
-
if not re.match(r"^[a-z0-9-]+$", component):
|
| 32 |
-
return False, "Component ID must contain only lowercase letters, numbers, and hyphens"
|
| 33 |
-
return True, ""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
class EventSeverity(str, Enum):
|
| 37 |
-
LOW = "low"
|
| 38 |
-
MEDIUM = "medium"
|
| 39 |
-
HIGH = "high"
|
| 40 |
-
CRITICAL = "critical"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
class HealingAction(str, Enum):
|
| 44 |
-
RESTART_CONTAINER = "restart_container"
|
| 45 |
-
SCALE_OUT = "scale_out"
|
| 46 |
-
TRAFFIC_SHIFT = "traffic_shift"
|
| 47 |
-
CIRCUIT_BREAKER = "circuit_breaker"
|
| 48 |
-
ROLLBACK = "rollback"
|
| 49 |
-
ALERT_TEAM = "alert_team"
|
| 50 |
-
NO_ACTION = "no_action"
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
class HealthStatus(str, Enum):
|
| 54 |
-
HEALTHY = "healthy"
|
| 55 |
-
DEGRADED = "degraded"
|
| 56 |
-
UNHEALTHY = "unhealthy"
|
| 57 |
-
UNKNOWN = "unknown"
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
class PolicyCondition(BaseModel):
|
| 61 |
-
metric: Literal["latency_p99", "error_rate", "cpu_util", "memory_util", "throughput"]
|
| 62 |
-
operator: Literal["gt", "lt", "eq", "gte", "lte"]
|
| 63 |
-
threshold: float = Field(ge=0)
|
| 64 |
-
model_config = ConfigDict(frozen=True)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
class ReliabilityEvent(BaseModel):
|
| 68 |
-
timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
| 69 |
-
component: str = Field(min_length=1, max_length=255)
|
| 70 |
-
service_mesh: str = Field(default="default", min_length=1, max_length=100)
|
| 71 |
-
latency_p99: float = Field(ge=0, lt=300000)
|
| 72 |
-
error_rate: float = Field(ge=0, le=1)
|
| 73 |
-
throughput: float = Field(ge=0)
|
| 74 |
-
cpu_util: Optional[float] = Field(default=None, ge=0, le=1)
|
| 75 |
-
memory_util: Optional[float] = Field(default=None, ge=0, le=1)
|
| 76 |
-
revenue_impact: Optional[float] = Field(default=None, ge=0)
|
| 77 |
-
user_impact: Optional[int] = Field(default=None, ge=0)
|
| 78 |
-
upstream_deps: List[str] = Field(default_factory=list)
|
| 79 |
-
downstream_deps: List[str] = Field(default_factory=list)
|
| 80 |
-
severity: EventSeverity = EventSeverity.LOW
|
| 81 |
-
model_config = ConfigDict(frozen=True, validate_assignment=True)
|
| 82 |
-
|
| 83 |
-
@field_validator("component")
|
| 84 |
-
@classmethod
|
| 85 |
-
def validate_component_id(cls, v: str) -> str:
|
| 86 |
-
if not re.match(r"^[a-z0-9-]+$", v):
|
| 87 |
-
raise ValueError("Component ID must contain only lowercase letters, numbers, and hyphens")
|
| 88 |
-
return v
|
| 89 |
-
|
| 90 |
-
@field_validator("upstream_deps", "downstream_deps")
|
| 91 |
-
@classmethod
|
| 92 |
-
def validate_dependency_format(cls, v: List[str]) -> List[str]:
|
| 93 |
-
for dep in v:
|
| 94 |
-
if not re.match(r"^[a-z0-9-]+$", dep):
|
| 95 |
-
raise ValueError(f"Dependency '{dep}' must contain only lowercase letters, numbers, and hyphens")
|
| 96 |
-
return v
|
| 97 |
-
|
| 98 |
-
@computed_field
|
| 99 |
-
@property
|
| 100 |
-
def fingerprint(self) -> str:
|
| 101 |
-
components = [
|
| 102 |
-
self.component,
|
| 103 |
-
self.service_mesh,
|
| 104 |
-
f"{self.latency_p99:.2f}",
|
| 105 |
-
f"{self.error_rate:.4f}",
|
| 106 |
-
f"{self.throughput:.2f}"
|
| 107 |
-
]
|
| 108 |
-
return hashlib.sha256(":".join(components).encode()).hexdigest()
|
| 109 |
-
|
| 110 |
-
def model_post_init(self, __context) -> None:
|
| 111 |
-
circular = set(self.upstream_deps) & set(self.downstream_deps)
|
| 112 |
-
if circular:
|
| 113 |
-
raise ValueError(f"Circular dependencies detected: {circular}")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
class HealingPolicy(BaseModel):
|
| 117 |
-
name: str = Field(min_length=1, max_length=255)
|
| 118 |
-
conditions: List[PolicyCondition] = Field(min_length=1)
|
| 119 |
-
actions: List[HealingAction] = Field(min_length=1)
|
| 120 |
-
priority: int = Field(ge=1, le=5, default=3)
|
| 121 |
-
cool_down_seconds: int = Field(ge=0, default=300)
|
| 122 |
-
enabled: bool = Field(default=True)
|
| 123 |
-
max_executions_per_hour: int = Field(ge=1, default=10)
|
| 124 |
-
model_config = ConfigDict(frozen=True)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
class AnomalyResult(BaseModel):
|
| 128 |
-
is_anomaly: bool
|
| 129 |
-
confidence: float = Field(ge=0, le=1)
|
| 130 |
-
anomaly_score: float = Field(ge=0, le=1)
|
| 131 |
-
affected_metrics: List[str] = Field(default_factory=list)
|
| 132 |
-
detection_timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
| 133 |
-
model_config = ConfigDict(frozen=True)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
class ForecastResult(BaseModel):
|
| 137 |
-
metric: str
|
| 138 |
-
predicted_value: float
|
| 139 |
-
confidence: float = Field(ge=0, le=1)
|
| 140 |
-
trend: Literal["increasing", "decreasing", "stable"]
|
| 141 |
-
time_to_threshold: Optional[float] = Field(default=None)
|
| 142 |
-
risk_level: Literal["low", "medium", "high", "critical"]
|
| 143 |
-
forecast_timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
| 144 |
-
model_config = ConfigDict(frozen=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|