Add overall metrics to the hallucination detector
This commit is contained in:
@@ -12,6 +12,7 @@ from collections import defaultdict
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from datetime import datetime
|
||||||
from middleware.monitoring_middleware import monitoring_middleware
|
from middleware.monitoring_middleware import monitoring_middleware
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
@@ -175,6 +176,62 @@ async def health():
|
|||||||
"""Health check endpoint."""
|
"""Health check endpoint."""
|
||||||
return health_check()
|
return health_check()
|
||||||
|
|
||||||
|
@app.get("/health/database")
|
||||||
|
async def database_health_check():
|
||||||
|
"""Database health check endpoint including persona tables verification."""
|
||||||
|
try:
|
||||||
|
from services.database import get_db_session
|
||||||
|
from models.persona_models import WritingPersona, PlatformPersona, PersonaAnalysisResult, PersonaValidationResult
|
||||||
|
|
||||||
|
session = get_db_session()
|
||||||
|
if not session:
|
||||||
|
return {"status": "error", "message": "Could not get database session"}
|
||||||
|
|
||||||
|
# Test all persona tables
|
||||||
|
tables_status = {}
|
||||||
|
try:
|
||||||
|
session.query(WritingPersona).first()
|
||||||
|
tables_status["writing_personas"] = "ok"
|
||||||
|
except Exception as e:
|
||||||
|
tables_status["writing_personas"] = f"error: {str(e)}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
session.query(PlatformPersona).first()
|
||||||
|
tables_status["platform_personas"] = "ok"
|
||||||
|
except Exception as e:
|
||||||
|
tables_status["platform_personas"] = f"error: {str(e)}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
session.query(PersonaAnalysisResult).first()
|
||||||
|
tables_status["persona_analysis_results"] = "ok"
|
||||||
|
except Exception as e:
|
||||||
|
tables_status["persona_analysis_results"] = f"error: {str(e)}"
|
||||||
|
|
||||||
|
try:
|
||||||
|
session.query(PersonaValidationResult).first()
|
||||||
|
tables_status["persona_validation_results"] = "ok"
|
||||||
|
except Exception as e:
|
||||||
|
tables_status["persona_validation_results"] = f"error: {str(e)}"
|
||||||
|
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
# Check if all tables are ok
|
||||||
|
all_ok = all(status == "ok" for status in tables_status.values())
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "healthy" if all_ok else "warning",
|
||||||
|
"message": "Database connection successful" if all_ok else "Some persona tables may have issues",
|
||||||
|
"persona_tables": tables_status,
|
||||||
|
"timestamp": datetime.utcnow().isoformat()
|
||||||
|
}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {
|
||||||
|
"status": "error",
|
||||||
|
"message": f"Database health check failed: {str(e)}",
|
||||||
|
"timestamp": datetime.utcnow().isoformat()
|
||||||
|
}
|
||||||
|
|
||||||
# Onboarding status endpoints
|
# Onboarding status endpoints
|
||||||
@app.get("/api/onboarding/status")
|
@app.get("/api/onboarding/status")
|
||||||
async def onboarding_status():
|
async def onboarding_status():
|
||||||
|
|||||||
@@ -214,6 +214,30 @@ def setup_environment():
|
|||||||
|
|
||||||
print("✅ Environment setup complete")
|
print("✅ Environment setup complete")
|
||||||
|
|
||||||
|
def verify_persona_tables():
|
||||||
|
"""Verify that persona tables exist and are accessible."""
|
||||||
|
print("🔍 Verifying persona tables...")
|
||||||
|
try:
|
||||||
|
from services.database import get_db_session
|
||||||
|
from models.persona_models import WritingPersona, PlatformPersona, PersonaAnalysisResult, PersonaValidationResult
|
||||||
|
|
||||||
|
session = get_db_session()
|
||||||
|
if session:
|
||||||
|
# Try to query all persona tables to verify they exist
|
||||||
|
session.query(WritingPersona).first()
|
||||||
|
session.query(PlatformPersona).first()
|
||||||
|
session.query(PersonaAnalysisResult).first()
|
||||||
|
session.query(PersonaValidationResult).first()
|
||||||
|
session.close()
|
||||||
|
print("✅ All persona tables verified successfully")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("⚠️ Warning: Could not get database session")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Warning: Could not verify persona tables: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
def start_backend(enable_reload=False):
|
def start_backend(enable_reload=False):
|
||||||
"""Start the backend server."""
|
"""Start the backend server."""
|
||||||
print("🚀 Starting ALwrity Backend...")
|
print("🚀 Starting ALwrity Backend...")
|
||||||
@@ -241,8 +265,17 @@ def start_backend(enable_reload=False):
|
|||||||
try:
|
try:
|
||||||
# Import and run the app
|
# Import and run the app
|
||||||
from app import app
|
from app import app
|
||||||
|
from services.database import init_database
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
|
# Explicitly initialize database before starting server
|
||||||
|
print("🗄️ Initializing database...")
|
||||||
|
init_database()
|
||||||
|
print("✅ Database initialized successfully")
|
||||||
|
|
||||||
|
# Verify persona tables exist
|
||||||
|
verify_persona_tables()
|
||||||
|
|
||||||
print("\n🌐 Backend is starting...")
|
print("\n🌐 Backend is starting...")
|
||||||
print(" 📖 API Documentation: http://localhost:8000/api/docs")
|
print(" 📖 API Documentation: http://localhost:8000/api/docs")
|
||||||
print(" 🔍 Health Check: http://localhost:8000/health")
|
print(" 🔍 Health Check: http://localhost:8000/health")
|
||||||
|
|||||||
Reference in New Issue
Block a user