The FastAPI backend for the EXP System Dashboard. Handles AI usage logging, dashboard statistics, GitHub language detection, and AI activity analysis.
- FastAPI
- Uvicorn
- Python
requestslibrary - GitHub REST API
src/backend/ai-monitoring/
├── router.py # All API route definitions
├── run.py # Uvicorn server entry point
└── app.py # FastAPI app factory (create_app)
1. Navigate to the backend directory:
cd src/backend/ai-monitoring2. Create and activate a virtual environment (recommended):
python -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # Mac/Linux3. Install dependencies:
pip install fastapi uvicorn requestspython run.pyServer runs on: http://127.0.0.1:8000
⚠️ Always usepython run.py— NOTpython -m flask runoruvicorndirectly, as environment variables may not load correctly.
GET /api/
Returns:
{ "message": "server is working" }Get all logs:
GET /api/ai-logs
Returns:
[
{
"user": "Samuel",
"feature": "JavaScript",
"success": true,
"timestamp": "2026-03-20T10:00:00"
}
]Add a log:
POST /api/ai-logs
Body:
{
"user": "Samuel",
"feature": "JavaScript",
"success": true
}Returns:
{ "message": "Log added", "log": { ... } }GET /api/dashboard-stats
Returns:
{
"total_logs": 15,
"successful": 12,
"failed": 3
}GET /api/github-stats?username=devbysamcloudy
Returns:
{
"username": "devbysamcloudy",
"total_repos": 12,
"languages": {
"JavaScript": 5,
"Python": 3,
"TypeScript": 2
}
}GET /api/ai-detection
Returns:
{
"languages": { "JavaScript": 8, "Python": 4 },
"success_rates": {
"JavaScript": { "success": 6, "fail": 2 }
},
"most_active": "JavaScript",
"total_logs": 12
}from fastapi import APIRouter, Request
from fastapi.responses import JSONResponse
from datetime import datetime
import requests as req
router = APIRouter()
ai_logs = []
@router.get("/api/dashboard-stats")
def dashboard_stats():
return {
"total_logs": len(ai_logs),
"successful": sum(1 for log in ai_logs if log["success"]),
"failed": sum(1 for log in ai_logs if not log["success"]),
}from app import create_app
import uvicorn
app = create_app()
if __name__ == "__main__":
uvicorn.run("run:app", host="0.0.0.0", port=8000, reload=True)- All logs are stored in-memory — they reset when the server restarts
- For production, replace the in-memory
ai_logslist with a database (PostgreSQL, Supabase, etc.) - The GitHub API has rate limits — authenticated requests allow more calls per hour
- CORS is enabled for all origins in development — restrict in production
In app.py, CORS is configured to allow the React frontend:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)For production deployment:
- Replace in-memory storage with a database
- Set up environment variables for sensitive config
- Use a production WSGI server like Gunicorn with Uvicorn workers:
gunicorn run:app -w 4 -k uvicorn.workers.UvicornWorker- Update the frontend
BASE_URLinapiservices.jsto your production URL
- Samuel Nganga — Frontend, AI API Integration, Team Lead
- Partner — Backend, FastAPI, Project Architecture