agent_tracker/database.py
nickviljoen 6c231cb094 Add read-only admin role, client verification workflow, and email notifications
- Three-tier role system: user, admin, readonly_admin with dashboard gating
- Client field (Yes/No) on registration with conditional Client Name and Studio Name
- Auto-tag client agents as "needs_verification" with Verification tab on admin dashboard
- Client agent email notification via Mailgun to configured recipients
- Daily agent digest email scheduled via APScheduler (configurable hour)
- Manual digest trigger endpoint: POST /api/admin/digest/send
- Role dropdown replaces is_admin checkbox in user edit modal
- Registration form reordered: Name, Description, Purpose, Client, Client Name, Studio, Tool
- Stat card CSS fix for text truncation on admin dashboard
- Updated CLAUDE.md documentation and PLAN file

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 20:47:02 +02:00

37 lines
1.3 KiB
Python

from motor.motor_asyncio import AsyncIOMotorClient
from dotenv import load_dotenv
import os
load_dotenv()
MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017")
MONGODB_DBNAME = os.getenv("MONGODB_DBNAME", "agenthub_db")
client = AsyncIOMotorClient(MONGODB_URI)
db = client[MONGODB_DBNAME]
users_collection = db.get_collection("users")
agents_collection = db.get_collection("agents")
agent_usage_collection = db.get_collection("agent_usage")
notifications_collection = db.get_collection("token_notifications")
agent_ratings_collection = db.get_collection("agent_ratings")
async def ensure_indexes():
"""Create database indexes for performance"""
try:
await agent_ratings_collection.create_index(
[("agent_id", 1), ("user_id", 1)],
unique=True
)
await agents_collection.create_index([("verification_status", 1)])
print("Database indexes ensured successfully")
except Exception as e:
print(f"Warning: Failed to create indexes: {e}")
async def check_database_health():
"""Check MongoDB connection health"""
try:
# Attempt to ping the database
await client.admin.command('ping')
return {"status": "connected", "healthy": True}
except Exception as e:
return {"status": "disconnected", "healthy": False, "error": str(e)}