Spaces:
Sleeping
Sleeping
File size: 10,886 Bytes
b491c15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | """
database_router.py — Router Query Intelligente per Separazione Workload Supabase A/B/C/D
Architettura:
A: Analytics/Cache/Read-Heavy (reporting, dashboard, cache distribuito)
B: Sync/State/Transazioni (stato globale, sincronizzazione cluster — PRIMARY)
C: Memory/RAG/Embeddings (backend memoria, vector search, skill index)
D: Audit/Logging/Compliance (event log, audit trail, compliance records)
Routing Logic:
1. Query di LETTURA (SELECT) → Preferisci A (read replica), fallback a B
2. Query di SCRITTURA (INSERT/UPDATE) → Usa B (PRIMARY)
3. Query su MEMORIA/RAG (skill_memory, embeddings, conversations) → Usa C
4. Query su AUDIT/LOG (audit_events, compliance_log) → Usa D
5. Query di SINCRONIZZAZIONE (cluster_state, global_state) → Usa B
"""
import asyncio
import os
import logging
import re as _re
from typing import Optional, Literal
from enum import Enum
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel
router = APIRouter(prefix="/api/database", tags=["database"])
_logger = logging.getLogger("database_router")
# ─── Enumerazione Nodi Supabase ───────────────────────────────────────────
class SupabaseNode(str, Enum):
A = "A" # Analytics/Cache
B = "B" # PRIMARY (Sync/State)
C = "C" # Memory/RAG
D = "D" # Audit/Logging
# ─── Configurazione Nodi ──────────────────────────────────────────────────
SUPABASE_CONFIG = {
"A": {
"url": os.getenv("SUPABASE_URL_A", ""),
"key": os.getenv("SUPABASE_KEY_A", ""),
"role": "Analytics/Cache (Read-Heavy)",
"priority": 1, # Preferito per letture
},
"B": {
"url": os.getenv("SUPABASE_URL", ""), # PRIMARY
"key": os.getenv("SUPABASE_KEY", ""),
"role": "Sync/State (PRIMARY)",
"priority": 0, # Fallback universale
},
"C": {
"url": os.getenv("SUPABASE_URL_C", ""),
"key": os.getenv("SUPABASE_KEY_C", ""),
"role": "Memory/RAG/Embeddings",
"priority": 2,
},
"D": {
"url": os.getenv("SUPABASE_URL_D", ""),
"key": os.getenv("SUPABASE_KEY_D", ""),
"role": "Audit/Logging/Compliance",
"priority": 3,
},
}
# ─── Keyword Pericolosi (per read-only) ───────────────────────────────────
_DANGEROUS = frozenset(
{"drop", "truncate", "delete", "update", "insert", "alter", "create", "grant", "revoke"}
)
_MAX_ROWS = 500
# ─── Modelli Pydantic ─────────────────────────────────────────────────────
class QueryRequest(BaseModel):
sql: str
params: list = []
read_only: bool = True
preferred_node: Optional[SupabaseNode] = None # Override routing logic
class QueryResponse(BaseModel):
ok: bool
rows: list = []
columns: list = []
count: int = 0
truncated: bool = False
node_used: Optional[str] = None
error: Optional[str] = None
# ─── Funzioni Utility ─────────────────────────────────────────────────────
def _is_dangerous(sql: str) -> Optional[str]:
"""Rilevamento keyword pericolose robusto contro CTE e multi-spazio."""
s_norm = " ".join(sql.strip().lower().split())
tokens = _re.split(r"[\s\(\),;]+", s_norm)
for tok in tokens:
if tok in _DANGEROUS:
return tok
for m in _re.finditer(r"\bas\s*\(\s*(\w+)", s_norm):
first_word = m.group(1).lower()
if first_word in _DANGEROUS:
return first_word
return None
def _detect_query_type(sql: str) -> Literal["SELECT", "INSERT", "UPDATE", "DELETE", "OTHER"]:
"""Rileva il tipo di query (SELECT, INSERT, UPDATE, DELETE, OTHER)."""
s_norm = " ".join(sql.strip().upper().split())
if s_norm.startswith("SELECT"):
return "SELECT"
elif s_norm.startswith("INSERT"):
return "INSERT"
elif s_norm.startswith("UPDATE"):
return "UPDATE"
elif s_norm.startswith("DELETE"):
return "DELETE"
return "OTHER"
def _detect_table_context(sql: str) -> Optional[str]:
"""Rileva il contesto della tabella per routing intelligente."""
sql_lower = sql.lower()
# Tabelle di memoria/RAG → Nodo C
if any(t in sql_lower for t in ["skill_memory", "embeddings", "conversations", "rag_index", "vector_store"]):
return "C"
# Tabelle di audit/logging → Nodo D
if any(t in sql_lower for t in ["audit_events", "audit_log", "compliance_log", "event_log", "activity_log"]):
return "D"
# Tabelle di stato globale → Nodo B
if any(t in sql_lower for t in ["cluster_state", "global_state", "sync_state", "agent_state", "daemon_status"]):
return "B"
return None
def _choose_node(
query_type: Literal["SELECT", "INSERT", "UPDATE", "DELETE", "OTHER"],
table_context: Optional[str],
preferred_node: Optional[SupabaseNode],
) -> SupabaseNode:
"""
Logica di routing intelligente per scegliere il nodo Supabase.
Priorità:
1. preferred_node (override esplicito)
2. table_context (rilevamento tabella)
3. query_type (tipo di query)
4. Fallback a B (PRIMARY)
"""
# 1. Override esplicito
if preferred_node:
return preferred_node
# 2. Routing per contesto tabella
if table_context:
return SupabaseNode(table_context)
# 3. Routing per tipo query
if query_type == "SELECT":
# Preferisci A (read replica) se disponibile, altrimenti B
if SUPABASE_CONFIG["A"]["url"]:
return SupabaseNode.A
return SupabaseNode.B
elif query_type in ("INSERT", "UPDATE", "DELETE"):
# Sempre su B (PRIMARY)
return SupabaseNode.B
# 4. Fallback a B (PRIMARY)
return SupabaseNode.B
# ─── Endpoint Principale ──────────────────────────────────────────────────
@router.post("/query", response_model=QueryResponse)
async def database_query(req: QueryRequest, request: Request):
"""
Endpoint query con routing intelligente tra nodi Supabase A/B/C/D.
Parametri:
- sql: query SQL
- params: parametri query
- read_only: blocca query pericolose (default: true)
- preferred_node: forza un nodo specifico (opzionale)
Ritorna:
- ok: successo
- rows: righe risultato
- columns: nomi colonne
- count: numero righe
- truncated: se risultato è stato troncato
- node_used: nodo Supabase utilizzato
"""
# Verifica token interno
_internal_token = os.getenv("INTERNAL_TOKEN", "")
if _internal_token and request.headers.get("X-Internal-Token") != _internal_token:
raise HTTPException(401, "Unauthorized")
# Rileva tipo query e contesto
query_type = _detect_query_type(req.sql)
table_context = _detect_table_context(req.sql)
# Scegli nodo
chosen_node = _choose_node(query_type, table_context, req.preferred_node)
# Verifica configurazione nodo
node_config = SUPABASE_CONFIG.get(chosen_node.value)
if not node_config or not node_config["url"]:
# Fallback a B se nodo non configurato
if chosen_node != SupabaseNode.B:
_logger.warning(
f"Nodo {chosen_node.value} non configurato, fallback a B. "
f"Configura SUPABASE_URL_{chosen_node.value} e SUPABASE_KEY_{chosen_node.value}."
)
chosen_node = SupabaseNode.B
node_config = SUPABASE_CONFIG["B"]
if not node_config["url"]:
return QueryResponse(
ok=False,
error=f"Nodo {chosen_node.value} non configurato. Imposta SUPABASE_URL_{chosen_node.value}.",
node_used=chosen_node.value,
)
# Verifica read-only
if req.read_only:
kw = _is_dangerous(req.sql)
if kw:
return QueryResponse(
ok=False,
error=f"Query bloccata (read-only): '{kw.upper()}' non consentito.",
node_used=chosen_node.value,
)
# Esegui query
try:
result = await _execute_query(
node_config["url"],
node_config["key"],
req.sql,
req.params,
)
result["node_used"] = chosen_node.value
return QueryResponse(**result)
except Exception as e:
_logger.error(f"Errore query su nodo {chosen_node.value}: {str(e)}")
return QueryResponse(
ok=False,
error=str(e)[:400],
node_used=chosen_node.value,
)
# ─── Esecuzione Query (Supabase PostgreSQL) ───────────────────────────────
async def _execute_query(url: str, key: str, sql: str, params: list) -> dict:
"""Esegue query su Supabase PostgreSQL."""
try:
import psycopg2
import psycopg2.extras
except ImportError:
return {
"ok": False,
"error": "psycopg2 non installato. Aggiungi 'psycopg2-binary' a requirements.txt.",
}
def _run():
conn = psycopg2.connect(url)
try:
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cur.execute(sql, params or None)
try:
rows = [dict(r) for r in cur.fetchmany(_MAX_ROWS)]
cols = [d.name for d in (cur.description or [])]
except psycopg2.ProgrammingError:
rows, cols = [], []
conn.commit()
finally:
conn.close()
return rows, cols
rows, cols = await asyncio.to_thread(_run)
return {
"ok": True,
"rows": rows,
"columns": cols,
"count": len(rows),
"truncated": len(rows) == _MAX_ROWS,
}
# ─── Endpoint Debug (info nodi) ───────────────────────────────────────────
@router.get("/nodes/status")
async def nodes_status():
"""Ritorna lo stato di configurazione di tutti i nodi Supabase."""
status = {}
for node_id, config in SUPABASE_CONFIG.items():
status[node_id] = {
"role": config["role"],
"configured": bool(config["url"]),
"url_preview": config["url"][:30] + "..." if config["url"] else "NOT SET",
}
return {"nodes": status}
|