Update app/api/deps.py
Browse files- app/api/deps.py +62 -41
app/api/deps.py
CHANGED
|
@@ -4,16 +4,66 @@ from slowapi import Limiter
|
|
| 4 |
from slowapi.util import get_remote_address
|
| 5 |
from app.core.config import settings
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
-
# Dependency to get DB session
|
| 17 |
def get_db():
|
| 18 |
db = SessionLocal()
|
| 19 |
try:
|
|
@@ -22,11 +72,10 @@ def get_db():
|
|
| 22 |
db.close()
|
| 23 |
|
| 24 |
|
| 25 |
-
# Rate limiter with default limit from settings
|
| 26 |
limiter = Limiter(key_func=get_remote_address, default_limits=[settings.RATE_LIMIT])
|
| 27 |
|
| 28 |
|
| 29 |
-
#
|
| 30 |
_risk_engine = None
|
| 31 |
_decision_engine = None
|
| 32 |
_stability_controller = None
|
|
@@ -35,36 +84,8 @@ _rag_graph = None
|
|
| 35 |
|
| 36 |
|
| 37 |
def _seed_rag_graph(rag):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
("seed_restart_1", "test", HealingAction.RESTART_CONTAINER.value, True, 2),
|
| 41 |
-
("seed_restart_2", "test", HealingAction.RESTART_CONTAINER.value, True, 3),
|
| 42 |
-
("seed_restart_3", "test", HealingAction.RESTART_CONTAINER.value, False, 10),
|
| 43 |
-
("seed_rollback_1", "test", HealingAction.ROLLBACK.value, True, 1),
|
| 44 |
-
("seed_rollback_2", "test", HealingAction.ROLLBACK.value, True, 2),
|
| 45 |
-
("seed_rollback_3", "test", HealingAction.ROLLBACK.value, False, 5),
|
| 46 |
-
("seed_scale_1", "test", HealingAction.SCALE_OUT.value, True, 5),
|
| 47 |
-
("seed_scale_2", "test", HealingAction.SCALE_OUT.value, False, 15),
|
| 48 |
-
("seed_cb_1", "test", HealingAction.CIRCUIT_BREAKER.value, True, 1),
|
| 49 |
-
("seed_cb_2", "test", HealingAction.CIRCUIT_BREAKER.value, True, 2),
|
| 50 |
-
("seed_ts_1", "test", HealingAction.TRAFFIC_SHIFT.value, True, 4),
|
| 51 |
-
("seed_ts_2", "test", HealingAction.TRAFFIC_SHIFT.value, False, 8),
|
| 52 |
-
]
|
| 53 |
-
for inc_id, comp, action, success, res_time in seed_data:
|
| 54 |
-
event = ReliabilityEvent(
|
| 55 |
-
component=comp,
|
| 56 |
-
latency_p99=500,
|
| 57 |
-
error_rate=0.1,
|
| 58 |
-
service_mesh="default"
|
| 59 |
-
)
|
| 60 |
-
rag.record_outcome(
|
| 61 |
-
incident_id=inc_id,
|
| 62 |
-
event=event,
|
| 63 |
-
action_taken=action,
|
| 64 |
-
success=success,
|
| 65 |
-
resolution_time_minutes=res_time
|
| 66 |
-
)
|
| 67 |
-
print("Seeded RAG graph with historical data", file=sys.stderr)
|
| 68 |
|
| 69 |
|
| 70 |
def get_rag_graph():
|
|
@@ -101,4 +122,4 @@ def get_causal_explainer():
|
|
| 101 |
global _causal_explainer
|
| 102 |
if _causal_explainer is None:
|
| 103 |
_causal_explainer = CausalExplainer()
|
| 104 |
-
return _causal_explainer
|
|
|
|
| 4 |
from slowapi.util import get_remote_address
|
| 5 |
from app.core.config import settings
|
| 6 |
|
| 7 |
+
# ---------------------------------------------------------------------------
|
| 8 |
+
# Local dummy implementations that replace the private engine classes.
|
| 9 |
+
# They provide the same interface as the originals but perform no real work.
|
| 10 |
+
# ---------------------------------------------------------------------------
|
| 11 |
+
class RiskEngine:
|
| 12 |
+
def __init__(self, *args, **kwargs):
|
| 13 |
+
pass
|
| 14 |
+
def calculate_risk(self, *args, **kwargs):
|
| 15 |
+
return (0.38, "mock", {"conjugate_mean": 0.38})
|
| 16 |
+
def update_outcome(self, *args, **kwargs):
|
| 17 |
+
pass
|
| 18 |
+
|
| 19 |
+
class DecisionEngine:
|
| 20 |
+
def __init__(self, *args, **kwargs):
|
| 21 |
+
pass
|
| 22 |
+
def select_optimal_action(self, *args, **kwargs):
|
| 23 |
+
class Result:
|
| 24 |
+
best_action = type('Action', (), {'value': 'NO_ACTION'})()
|
| 25 |
+
expected_utility = 0.0
|
| 26 |
+
alternatives = []
|
| 27 |
+
explanation = "mock"
|
| 28 |
+
raw_data = {}
|
| 29 |
+
return Result()
|
| 30 |
+
def compute_risk(self, *args, **kwargs):
|
| 31 |
+
return 0.0
|
| 32 |
+
|
| 33 |
+
class LyapunovStabilityController:
|
| 34 |
+
def __init__(self, *args, **kwargs):
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
class CausalExplainer:
|
| 38 |
+
def __init__(self, *args, **kwargs):
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
class RAGGraphMemory:
|
| 42 |
+
def __init__(self, *args, **kwargs):
|
| 43 |
+
pass
|
| 44 |
+
def has_historical_data(self):
|
| 45 |
+
return False
|
| 46 |
+
def record_outcome(self, *args, **kwargs):
|
| 47 |
+
pass
|
| 48 |
+
|
| 49 |
+
class ReliabilityEvent:
|
| 50 |
+
def __init__(self, component, latency_p99, error_rate, service_mesh="default"):
|
| 51 |
+
self.component = component
|
| 52 |
+
self.latency_p99 = latency_p99
|
| 53 |
+
self.error_rate = error_rate
|
| 54 |
+
self.service_mesh = service_mesh
|
| 55 |
+
|
| 56 |
+
class HealingAction:
|
| 57 |
+
NO_ACTION = "NO_ACTION"
|
| 58 |
+
RESTART_CONTAINER = "RESTART_CONTAINER"
|
| 59 |
+
SCALE_OUT = "SCALE_OUT"
|
| 60 |
+
ROLLBACK = "ROLLBACK"
|
| 61 |
+
CIRCUIT_BREAKER = "CIRCUIT_BREAKER"
|
| 62 |
+
TRAFFIC_SHIFT = "TRAFFIC_SHIFT"
|
| 63 |
+
ALERT_TEAM = "ALERT_TEAM"
|
| 64 |
+
# ---------------------------------------------------------------------------
|
| 65 |
|
| 66 |
|
|
|
|
| 67 |
def get_db():
|
| 68 |
db = SessionLocal()
|
| 69 |
try:
|
|
|
|
| 72 |
db.close()
|
| 73 |
|
| 74 |
|
|
|
|
| 75 |
limiter = Limiter(key_func=get_remote_address, default_limits=[settings.RATE_LIMIT])
|
| 76 |
|
| 77 |
|
| 78 |
+
# Singletons (now using local dummies)
|
| 79 |
_risk_engine = None
|
| 80 |
_decision_engine = None
|
| 81 |
_stability_controller = None
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
def _seed_rag_graph(rag):
|
| 87 |
+
# Mock seed – no real data
|
| 88 |
+
print("RAG seed skipped (sandbox mode)", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def get_rag_graph():
|
|
|
|
| 122 |
global _causal_explainer
|
| 123 |
if _causal_explainer is None:
|
| 124 |
_causal_explainer = CausalExplainer()
|
| 125 |
+
return _causal_explainer
|