feat: expose /metrics endpoint in health daemon
This commit is contained in:
@@ -24,32 +24,52 @@ class HealthCheckHandler(BaseHTTPRequestHandler):
|
|||||||
# Suppress default logging
|
# Suppress default logging
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
"""Handle GET requests"""
|
"""Handle GET requests"""
|
||||||
if self.path == '/health':
|
if self.path == '/health':
|
||||||
self.send_health_response()
|
self.send_health_response()
|
||||||
elif self.path == '/status':
|
elif self.path == '/status':
|
||||||
self.send_full_status()
|
self.send_full_status()
|
||||||
|
elif self.path == '/metrics':
|
||||||
|
self.send_sovereign_metrics()
|
||||||
else:
|
else:
|
||||||
self.send_error(404)
|
self.send_error(404)
|
||||||
|
|
||||||
def send_health_response(self):
|
def send_sovereign_metrics(self):
|
||||||
"""Send simple health check"""
|
"""Send sovereign health metrics as JSON"""
|
||||||
harness = get_harness()
|
|
||||||
result = harness.execute("health_check")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
health_data = json.loads(result)
|
import sqlite3
|
||||||
status_code = 200 if health_data.get("overall") == "healthy" else 503
|
db_path = Path.home() / ".timmy" / "metrics" / "model_metrics.db"
|
||||||
except:
|
if not db_path.exists():
|
||||||
status_code = 503
|
data = {"error": "No database found"}
|
||||||
health_data = {"error": "Health check failed"}
|
else:
|
||||||
|
conn = sqlite3.connect(str(db_path))
|
||||||
self.send_response(status_code)
|
row = conn.execute("""
|
||||||
|
SELECT local_pct, total_sessions, local_sessions, cloud_sessions, est_cloud_cost, est_saved
|
||||||
|
FROM sovereignty_score ORDER BY timestamp DESC LIMIT 1
|
||||||
|
""").fetchone()
|
||||||
|
|
||||||
|
if row:
|
||||||
|
data = {
|
||||||
|
"sovereignty_score": row[0],
|
||||||
|
"total_sessions": row[1],
|
||||||
|
"local_sessions": row[2],
|
||||||
|
"cloud_sessions": row[3],
|
||||||
|
"est_cloud_cost": row[4],
|
||||||
|
"est_saved": row[5],
|
||||||
|
"timestamp": datetime.now().isoformat()
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
data = {"error": "No data"}
|
||||||
|
conn.close()
|
||||||
|
except Exception as e:
|
||||||
|
data = {"error": str(e)}
|
||||||
|
|
||||||
|
self.send_response(200)
|
||||||
self.send_header('Content-Type', 'application/json')
|
self.send_header('Content-Type', 'application/json')
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(json.dumps(health_data).encode())
|
self.wfile.write(json.dumps(data).encode())
|
||||||
|
|
||||||
def send_full_status(self):
|
def send_full_status(self):
|
||||||
"""Send full system status"""
|
"""Send full system status"""
|
||||||
harness = get_harness()
|
harness = get_harness()
|
||||||
|
|||||||
Reference in New Issue
Block a user