-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetl_processor.py
More file actions
92 lines (77 loc) · 2.89 KB
/
Copy pathetl_processor.py
File metadata and controls
92 lines (77 loc) · 2.89 KB
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
"""
FinQueryFlow - Pipeline Health Check
Pings all infrastructure components and returns unified status
"""
from fastapi import APIRouter
from models.schemas import PipelineHealth, ComponentStatus
from datetime import datetime
import chromadb
from chromadb.config import Settings
import psycopg2
import redis as redis_lib
from kafka import KafkaAdminClient
from kafka.errors import KafkaError
import os
router = APIRouter()
KAFKA_BROKER = os.getenv("KAFKA_BROKER", "localhost:9092")
CHROMA_HOST = os.getenv("CHROMA_HOST", "localhost")
CHROMA_PORT = int(os.getenv("CHROMA_PORT","8001"))
PG_DSN = os.getenv("DATABASE_URL", "postgresql://finuser:finpass@localhost:5432/finqueryflow")
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
def check_kafka() -> ComponentStatus:
try:
admin = KafkaAdminClient(bootstrap_servers=KAFKA_BROKER, request_timeout_ms=3000)
admin.close()
return ComponentStatus.healthy
except KafkaError:
return ComponentStatus.down
except Exception:
return ComponentStatus.degraded
def check_chroma() -> ComponentStatus:
try:
client = chromadb.HttpClient(
host=CHROMA_HOST, port=CHROMA_PORT,
settings=Settings(anonymized_telemetry=False),
)
client.heartbeat()
return ComponentStatus.healthy
except Exception:
return ComponentStatus.down
def check_postgres() -> ComponentStatus:
try:
conn = psycopg2.connect(PG_DSN, connect_timeout=3)
conn.close()
return ComponentStatus.healthy
except Exception:
return ComponentStatus.down
def check_redis() -> ComponentStatus:
try:
r = redis_lib.from_url(REDIS_URL, socket_timeout=2)
r.ping()
return ComponentStatus.healthy
except Exception:
return ComponentStatus.down
@router.get("/health/pipeline", response_model=PipelineHealth)
async def pipeline_health():
"""
Returns health status of every pipeline component.
Called by the dashboard every 30s to update status indicators.
"""
kafka = check_kafka()
chroma = check_chroma()
postgres = check_postgres()
redis = check_redis()
# Derived statuses
ingestion = ComponentStatus.healthy if kafka == ComponentStatus.healthy else ComponentStatus.degraded
etl = ComponentStatus.healthy if (kafka == ComponentStatus.healthy and chroma == ComponentStatus.healthy and postgres == ComponentStatus.healthy) else ComponentStatus.degraded
rag = ComponentStatus.healthy if (chroma == ComponentStatus.healthy and redis == ComponentStatus.healthy) else ComponentStatus.degraded
return PipelineHealth(
kafka = kafka,
chromadb = chroma,
postgres = postgres,
redis = redis,
ingestion = ingestion,
etl = etl,
rag = rag,
checked_at = datetime.utcnow(),
)