Spaces:
Sleeping
Sleeping
File size: 4,603 Bytes
1d4dc07 | 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 | #!/usr/bin/env python3
"""
Debug analytics collection step by step
"""
import asyncio
import httpx
import os
from dotenv import load_dotenv
load_dotenv()
async def debug_analytics():
"""Debug the analytics collection process"""
try:
print("π DEBUGGING ANALYTICS COLLECTION")
print("=" * 50)
# Check environment variables
print("1. Environment Variables:")
mongodb_url = os.getenv("MONGODB_URL")
if mongodb_url:
print(f" β
MONGODB_URL is set (length: {len(mongodb_url)})")
else:
print(" β MONGODB_URL is not set")
# Test database connection
print("\n2. Database Connection:")
try:
from analytics.database import test_connection
connected = await test_connection()
if connected:
print(" β
Database connection successful")
else:
print(" β Database connection failed")
except Exception as e:
print(f" β Database connection error: {e}")
# Test analytics functions
print("\n3. Analytics Functions:")
try:
from analytics.collectors import create_session, track_message
# Test session creation
session = await create_session(user_agent="Debug Test")
if session:
print(f" β
Session creation works: {session.session_id}")
# Test message tracking
message = await track_message(
session_id=session.session_id,
prompt_length=20,
response_length=100,
response_time_ms=1500,
used_search=True,
success=True
)
if message:
print(f" β
Message tracking works: {message.message_id}")
else:
print(" β Message tracking failed")
else:
print(" β Session creation failed")
except Exception as e:
print(f" β Analytics functions error: {e}")
import traceback
traceback.print_exc()
# Check data in database
print("\n4. Database Data Check:")
try:
from analytics.database import get_sessions_collection, get_messages_collection
sessions_collection = await get_sessions_collection()
messages_collection = await get_messages_collection()
if sessions_collection is not None and messages_collection is not None:
sessions_count = await sessions_collection.count_documents({})
messages_count = await messages_collection.count_documents({})
print(f" π Sessions in DB: {sessions_count}")
print(f" π¬ Messages in DB: {messages_count}")
if sessions_count > 0:
latest_session = await sessions_collection.find_one({}, sort=[("start_time", -1)])
print(f" π Latest session: {latest_session.get('session_id', 'N/A')}")
if messages_count > 0:
latest_message = await messages_collection.find_one({}, sort=[("timestamp", -1)])
print(f" π Latest message: {latest_message.get('message_id', 'N/A')}")
else:
print(" β Could not access database collections")
except Exception as e:
print(f" β Database check error: {e}")
# Test dashboard
print("\n5. Dashboard Data:")
try:
from analytics.dashboard import get_basic_stats
stats = await get_basic_stats()
if "error" in stats:
print(f" β Dashboard error: {stats['error']}")
else:
print(f" π Total sessions: {stats.get('total_sessions', 0)}")
print(f" π¬ Total messages: {stats.get('total_messages', 0)}")
print(f" π’ Active sessions: {stats.get('active_sessions', 0)}")
except Exception as e:
print(f" β Dashboard error: {e}")
except Exception as e:
print(f"β Debug error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(debug_analytics()) |