Spaces:
Sleeping
Sleeping
File size: 5,052 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 121 122 123 124 125 126 127 128 129 130 131 132 | #!/usr/bin/env python3
"""
Test chat request to see if analytics are collected
"""
import asyncio
import httpx
import json
async def test_chat_request():
"""Make a chat request and check if analytics are collected"""
try:
print("π Testing Chat Request with Analytics Collection")
print("=" * 60)
# Make a chat request
chat_data = {
"prompt": "Hello, this is a test message for analytics",
"max_new_tokens": 100,
"use_search": True,
"temperature": 0.7
}
print("π€ Sending chat request...")
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
"http://localhost:7860/chat",
json=chat_data,
headers={"Content-Type": "application/json"}
)
print(f"π₯ Response status: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"β
Chat response received (length: {len(result.get('response', ''))} chars)")
# Check for session ID in headers
session_id = response.headers.get('X-Session-ID')
if session_id:
print(f"π Session ID: {session_id}")
else:
print("β οΈ No session ID in response headers")
return True
else:
print(f"β Chat request failed: {response.text}")
return False
except Exception as e:
print(f"β Error making chat request: {e}")
return False
async def check_data_after_request():
"""Check if data was collected after the chat request"""
try:
print("\nπ Checking Data Collection After Chat Request")
print("=" * 60)
# Wait a moment for data to be written
await asyncio.sleep(2)
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 None or messages_collection is None:
print("β Database collections not available")
return
# Check sessions
sessions_count = await sessions_collection.count_documents({})
print(f"π Sessions in database: {sessions_count}")
if sessions_count > 0:
latest_session = await sessions_collection.find_one({}, sort=[("start_time", -1)])
print(f" Latest session ID: {latest_session.get('session_id', 'N/A')}")
print(f" Message count: {latest_session.get('message_count', 0)}")
print(f" Status: {latest_session.get('status', 'N/A')}")
# Check messages
messages_count = await messages_collection.count_documents({})
print(f"π¬ Messages in database: {messages_count}")
if messages_count > 0:
latest_message = await messages_collection.find_one({}, sort=[("timestamp", -1)])
print(f" Latest message ID: {latest_message.get('message_id', 'N/A')}")
print(f" Session ID: {latest_message.get('session_id', 'N/A')}")
print(f" Prompt length: {latest_message.get('prompt_length', 0)}")
print(f" Response time: {latest_message.get('response_time_ms', 0)}ms")
print(f" Used search: {latest_message.get('used_search', False)}")
print(f" Success: {latest_message.get('success', False)}")
return sessions_count > 0 and messages_count > 0
except Exception as e:
print(f"β Error checking data: {e}")
import traceback
traceback.print_exc()
return False
async def main():
# Test chat request
chat_success = await test_chat_request()
if chat_success:
# Check if data was collected
data_collected = await check_data_after_request()
if data_collected:
print("\nβ
SUCCESS: Analytics data is being collected!")
# Test dashboard again
print("\nπ― Testing Dashboard After Data Collection")
print("=" * 60)
from analytics.dashboard import get_basic_stats
stats = await get_basic_stats()
print("Updated dashboard stats:")
for key, value in stats.items():
print(f" {key}: {value}")
else:
print("\nβ PROBLEM: No analytics data was collected!")
print(" The chat request worked but analytics collection failed.")
else:
print("\nβ PROBLEM: Chat request failed!")
print(" Cannot test analytics collection.")
if __name__ == "__main__":
asyncio.run(main()) |