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