122 lines
3.9 KiB
Python
122 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify usage counter functionality.
|
|
This script demonstrates how to query the usage counters table.
|
|
"""
|
|
|
|
import asyncio
|
|
import asyncpg
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
|
|
async def test_usage_counters():
|
|
"""Test the usage counters functionality."""
|
|
|
|
# Create database connection
|
|
try:
|
|
conn_string = f"postgresql://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@{os.getenv('POSTGRES_HOST')}:{os.getenv('POSTGRES_PORT')}/{os.getenv('POSTGRES_SETTINGS_DB')}"
|
|
conn = await asyncpg.connect(conn_string)
|
|
print("✅ Connected to database successfully")
|
|
except Exception as e:
|
|
print(f"❌ Failed to connect to database: {e}")
|
|
return
|
|
|
|
try:
|
|
# Check if the table exists
|
|
table_exists = await conn.fetchval(
|
|
"""
|
|
SELECT EXISTS (
|
|
SELECT FROM information_schema.tables
|
|
WHERE table_name = 'command_usage_counters'
|
|
)
|
|
"""
|
|
)
|
|
|
|
if table_exists:
|
|
print("✅ command_usage_counters table exists")
|
|
|
|
# Get some sample data
|
|
records = await conn.fetch(
|
|
"""
|
|
SELECT user1_id, user2_id, command_name, usage_count
|
|
FROM command_usage_counters
|
|
ORDER BY usage_count DESC
|
|
LIMIT 10
|
|
"""
|
|
)
|
|
|
|
if records:
|
|
print("\n📊 Top 10 command usages:")
|
|
print("User1 ID | User2 ID | Command | Count")
|
|
print("-" * 45)
|
|
for record in records:
|
|
print(
|
|
f"{record['user1_id']} | {record['user2_id']} | {record['command_name']} | {record['usage_count']}"
|
|
)
|
|
else:
|
|
print("📝 No usage data found yet (table is empty)")
|
|
|
|
# Get total count
|
|
total_count = await conn.fetchval(
|
|
"SELECT COUNT(*) FROM command_usage_counters"
|
|
)
|
|
print(f"\n📈 Total unique user-command combinations: {total_count}")
|
|
|
|
else:
|
|
print("⚠️ command_usage_counters table does not exist yet")
|
|
print(" It will be created automatically when a command is first used")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error querying database: {e}")
|
|
finally:
|
|
await conn.close()
|
|
print("🔌 Database connection closed")
|
|
|
|
|
|
async def get_usage_for_users(user1_id: int, user2_id: int):
|
|
"""Get usage statistics for a specific pair of users."""
|
|
|
|
try:
|
|
conn_string = f"postgresql://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@{os.getenv('POSTGRES_HOST')}:{os.getenv('POSTGRES_PORT')}/{os.getenv('POSTGRES_SETTINGS_DB')}"
|
|
conn = await asyncpg.connect(conn_string)
|
|
|
|
records = await conn.fetch(
|
|
"""
|
|
SELECT command_name, usage_count
|
|
FROM command_usage_counters
|
|
WHERE user1_id = $1 AND user2_id = $2
|
|
ORDER BY usage_count DESC
|
|
""",
|
|
user1_id,
|
|
user2_id,
|
|
)
|
|
|
|
if records:
|
|
print(f"\n👥 Usage between users {user1_id} and {user2_id}:")
|
|
print("Command | Count")
|
|
print("-" * 20)
|
|
for record in records:
|
|
print(f"{record['command_name']} | {record['usage_count']}")
|
|
else:
|
|
print(f"📝 No usage data found between users {user1_id} and {user2_id}")
|
|
|
|
await conn.close()
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error querying user data: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("🧪 Testing Usage Counters Functionality")
|
|
print("=" * 40)
|
|
|
|
# Test basic functionality
|
|
asyncio.run(test_usage_counters())
|
|
|
|
# Example: Get usage for specific users (replace with actual user IDs)
|
|
# asyncio.run(get_usage_for_users(123456789, 987654321))
|