optmize UI
This commit is contained in:
45
main.py
45
main.py
@ -137,18 +137,25 @@ async def get_24h_stats():
|
|||||||
return {
|
return {
|
||||||
"total_chats": total_chats,
|
"total_chats": total_chats,
|
||||||
"total_tokens": total_tokens,
|
"total_tokens": total_tokens,
|
||||||
"total_cost": round(total_cost, 4)
|
"total_cost": round(total_cost, 6)
|
||||||
}
|
}
|
||||||
|
|
||||||
async def get_recent_logs(limit: int = 100):
|
async def get_recent_logs(page: int = 1, page_size: int = 100):
|
||||||
"""Get recent API call logs"""
|
"""Get recent API call logs with pagination"""
|
||||||
|
offset = (page - 1) * page_size
|
||||||
|
|
||||||
async with aiosqlite.connect(database_path) as db:
|
async with aiosqlite.connect(database_path) as db:
|
||||||
|
# Get total count for pagination
|
||||||
|
cursor = await db.execute("SELECT COUNT(*) FROM api_calls")
|
||||||
|
total_count = (await cursor.fetchone())[0]
|
||||||
|
|
||||||
|
# Get paginated results
|
||||||
cursor = await db.execute("""
|
cursor = await db.execute("""
|
||||||
SELECT timestamp, model_id, user_email, input_tokens, output_tokens, cost_usd
|
SELECT timestamp, model_id, user_email, input_tokens, output_tokens, cost_usd
|
||||||
FROM api_calls
|
FROM api_calls
|
||||||
ORDER BY timestamp DESC
|
ORDER BY timestamp DESC
|
||||||
LIMIT ?
|
LIMIT ? OFFSET ?
|
||||||
""", (limit,))
|
""", (page_size, offset))
|
||||||
|
|
||||||
logs = []
|
logs = []
|
||||||
async for row in cursor:
|
async for row in cursor:
|
||||||
@ -160,10 +167,22 @@ async def get_recent_logs(limit: int = 100):
|
|||||||
"input_tokens": row[3],
|
"input_tokens": row[3],
|
||||||
"output_tokens": row[4],
|
"output_tokens": row[4],
|
||||||
"total_tokens": row[3] + row[4],
|
"total_tokens": row[3] + row[4],
|
||||||
"cost_usd": round(row[5], 4)
|
"cost_usd": round(row[5], 6)
|
||||||
})
|
})
|
||||||
|
|
||||||
return logs
|
total_pages = (total_count + page_size - 1) // page_size # Ceiling division
|
||||||
|
|
||||||
|
return {
|
||||||
|
"logs": logs,
|
||||||
|
"pagination": {
|
||||||
|
"current_page": page,
|
||||||
|
"total_pages": total_pages,
|
||||||
|
"total_count": total_count,
|
||||||
|
"page_size": page_size,
|
||||||
|
"has_prev": page > 1,
|
||||||
|
"has_next": page < total_pages
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async def get_users_report():
|
async def get_users_report():
|
||||||
"""Get user consumption statistics"""
|
"""Get user consumption statistics"""
|
||||||
@ -189,7 +208,7 @@ async def get_users_report():
|
|||||||
"total_input_tokens": row[2],
|
"total_input_tokens": row[2],
|
||||||
"total_output_tokens": row[3],
|
"total_output_tokens": row[3],
|
||||||
"total_tokens": row[4],
|
"total_tokens": row[4],
|
||||||
"total_cost": round(row[5], 4)
|
"total_cost": round(row[5], 6)
|
||||||
})
|
})
|
||||||
|
|
||||||
return users
|
return users
|
||||||
@ -227,12 +246,16 @@ async def dashboard(request: Request, _: str = Depends(verify_admin_basic_auth))
|
|||||||
})
|
})
|
||||||
|
|
||||||
@app.get("/dashboard/logs", response_class=HTMLResponse)
|
@app.get("/dashboard/logs", response_class=HTMLResponse)
|
||||||
async def dashboard_logs(request: Request, _: str = Depends(verify_admin_basic_auth)):
|
async def dashboard_logs(request: Request, page: int = 1, _: str = Depends(verify_admin_basic_auth)):
|
||||||
"""Dashboard logs page"""
|
"""Dashboard logs page"""
|
||||||
logs = await get_recent_logs(200) # Get more logs for the logs page
|
if page < 1:
|
||||||
|
page = 1
|
||||||
|
|
||||||
|
result = await get_recent_logs(page=page, page_size=100)
|
||||||
return templates.TemplateResponse("logs.html", {
|
return templates.TemplateResponse("logs.html", {
|
||||||
"request": request,
|
"request": request,
|
||||||
"logs": logs
|
"logs": result["logs"],
|
||||||
|
"pagination": result["pagination"]
|
||||||
})
|
})
|
||||||
|
|
||||||
@app.get("/dashboard/users_report", response_class=HTMLResponse)
|
@app.get("/dashboard/users_report", response_class=HTMLResponse)
|
||||||
|
@ -62,7 +62,7 @@
|
|||||||
<h5 class="card-title text-warning">
|
<h5 class="card-title text-warning">
|
||||||
<i class="bi bi-currency-dollar"></i> Total Cost
|
<i class="bi bi-currency-dollar"></i> Total Cost
|
||||||
</h5>
|
</h5>
|
||||||
<h2 class="card-text">${{ stats.total_cost }}</h2>
|
<h2 class="card-text">${{ "%.6f"|format(stats.total_cost) }}</h2>
|
||||||
<small class="text-muted">Last 24 hours</small>
|
<small class="text-muted">Last 24 hours</small>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-warning">
|
<div class="text-warning">
|
||||||
|
@ -20,12 +20,54 @@
|
|||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<div class="alert alert-info">
|
<div class="alert alert-info">
|
||||||
<i class="bi bi-info-circle"></i>
|
<i class="bi bi-info-circle"></i>
|
||||||
Showing <strong>{{ logs|length }}</strong> most recent API calls.
|
Showing <strong>{{ logs|length }}</strong> API calls on page <strong>{{ pagination.current_page }}</strong> of <strong>{{ pagination.total_pages }}</strong>.
|
||||||
|
Total: <strong>{{ pagination.total_count }}</strong> records.
|
||||||
Logs are ordered by timestamp (newest first).
|
Logs are ordered by timestamp (newest first).
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Statistics Summary -->
|
||||||
|
<div class="row mb-4">
|
||||||
|
<div class="col-12">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h5 class="card-title mb-0">
|
||||||
|
<i class="bi bi-bar-chart"></i> Current Page Statistics
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="text-center">
|
||||||
|
<h4 class="text-primary">{{ logs|length }}</h4>
|
||||||
|
<small class="text-muted">Total Calls</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="text-center">
|
||||||
|
<h4 class="text-success">{{ "{:,}".format(logs|sum(attribute='input_tokens')) }}</h4>
|
||||||
|
<small class="text-muted">Input Tokens</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="text-center">
|
||||||
|
<h4 class="text-info">{{ "{:,}".format(logs|sum(attribute='output_tokens')) }}</h4>
|
||||||
|
<small class="text-muted">Output Tokens</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-3">
|
||||||
|
<div class="text-center">
|
||||||
|
<h4 class="text-warning">${{ "%.6f"|format(logs|sum(attribute='cost_usd')) }}</h4>
|
||||||
|
<small class="text-muted">Total Cost</small>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Logs Table -->
|
<!-- Logs Table -->
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
@ -34,8 +76,7 @@
|
|||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body p-0">
|
<div class="card-body p-0">
|
||||||
<div class="table-responsive">
|
<table class="table table-striped table-hover mb-0">
|
||||||
<table class="table table-striped table-hover mb-0">
|
|
||||||
<thead class="table-dark">
|
<thead class="table-dark">
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col">
|
<th scope="col">
|
||||||
@ -83,56 +124,89 @@
|
|||||||
<strong>{{ "{:,}".format(log.total_tokens) }}</strong>
|
<strong>{{ "{:,}".format(log.total_tokens) }}</strong>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<span class="badge bg-warning text-dark">${{ log.cost_usd }}</span>
|
<span class="badge bg-warning text-dark">${{ "%.6f"|format(log.cost_usd) }}</span>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Statistics Summary -->
|
<!-- Pagination -->
|
||||||
<div class="row mt-4">
|
{% if pagination.total_pages > 1 %}
|
||||||
|
<div class="row mb-4">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<div class="card">
|
<nav aria-label="Logs pagination">
|
||||||
<div class="card-header">
|
<ul class="pagination justify-content-center">
|
||||||
<h5 class="card-title mb-0">
|
<!-- Previous button -->
|
||||||
<i class="bi bi-bar-chart"></i> Current Page Statistics
|
<li class="page-item {{ 'disabled' if not pagination.has_prev }}">
|
||||||
</h5>
|
{% if pagination.has_prev %}
|
||||||
</div>
|
<a class="page-link" href="?page={{ pagination.current_page - 1 }}">
|
||||||
<div class="card-body">
|
<i class="bi bi-chevron-left"></i> Previous
|
||||||
<div class="row">
|
</a>
|
||||||
<div class="col-md-3">
|
{% else %}
|
||||||
<div class="text-center">
|
<span class="page-link">
|
||||||
<h4 class="text-primary">{{ logs|length }}</h4>
|
<i class="bi bi-chevron-left"></i> Previous
|
||||||
<small class="text-muted">Total Calls</small>
|
</span>
|
||||||
</div>
|
{% endif %}
|
||||||
</div>
|
</li>
|
||||||
<div class="col-md-3">
|
|
||||||
<div class="text-center">
|
<!-- Page numbers -->
|
||||||
<h4 class="text-success">{{ "{:,}".format(logs|sum(attribute='input_tokens')) }}</h4>
|
{% set start_page = [1, pagination.current_page - 2]|max %}
|
||||||
<small class="text-muted">Input Tokens</small>
|
{% set end_page = [pagination.total_pages, pagination.current_page + 2]|min %}
|
||||||
</div>
|
|
||||||
</div>
|
{% if start_page > 1 %}
|
||||||
<div class="col-md-3">
|
<li class="page-item">
|
||||||
<div class="text-center">
|
<a class="page-link" href="?page=1">1</a>
|
||||||
<h4 class="text-info">{{ "{:,}".format(logs|sum(attribute='output_tokens')) }}</h4>
|
</li>
|
||||||
<small class="text-muted">Output Tokens</small>
|
{% if start_page > 2 %}
|
||||||
</div>
|
<li class="page-item disabled">
|
||||||
</div>
|
<span class="page-link">...</span>
|
||||||
<div class="col-md-3">
|
</li>
|
||||||
<div class="text-center">
|
{% endif %}
|
||||||
<h4 class="text-warning">${{ "%.4f"|format(logs|sum(attribute='cost_usd')) }}</h4>
|
{% endif %}
|
||||||
<small class="text-muted">Total Cost</small>
|
|
||||||
</div>
|
{% for page_num in range(start_page, end_page + 1) %}
|
||||||
</div>
|
<li class="page-item {{ 'active' if page_num == pagination.current_page }}">
|
||||||
</div>
|
{% if page_num == pagination.current_page %}
|
||||||
</div>
|
<span class="page-link">{{ page_num }}</span>
|
||||||
</div>
|
{% else %}
|
||||||
|
<a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a>
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
{% if end_page < pagination.total_pages %}
|
||||||
|
{% if end_page < pagination.total_pages - 1 %}
|
||||||
|
<li class="page-item disabled">
|
||||||
|
<span class="page-link">...</span>
|
||||||
|
</li>
|
||||||
|
{% endif %}
|
||||||
|
<li class="page-item">
|
||||||
|
<a class="page-link" href="?page={{ pagination.total_pages }}">{{ pagination.total_pages }}</a>
|
||||||
|
</li>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<!-- Next button -->
|
||||||
|
<li class="page-item {{ 'disabled' if not pagination.has_next }}">
|
||||||
|
{% if pagination.has_next %}
|
||||||
|
<a class="page-link" href="?page={{ pagination.current_page + 1 }}">
|
||||||
|
Next <i class="bi bi-chevron-right"></i>
|
||||||
|
</a>
|
||||||
|
{% else %}
|
||||||
|
<span class="page-link">
|
||||||
|
Next <i class="bi bi-chevron-right"></i>
|
||||||
|
</span>
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{% else %}
|
{% else %}
|
||||||
<!-- No Data -->
|
<!-- No Data -->
|
||||||
@ -155,9 +229,7 @@
|
|||||||
|
|
||||||
{% block scripts %}
|
{% block scripts %}
|
||||||
<script>
|
<script>
|
||||||
// Auto-refresh every 30 seconds
|
// Note: Auto-refresh disabled for pagination functionality
|
||||||
setInterval(() => {
|
// Users can manually refresh using the refresh button
|
||||||
window.location.reload();
|
|
||||||
}, 30000);
|
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -64,7 +64,7 @@
|
|||||||
<h5 class="card-title text-warning">
|
<h5 class="card-title text-warning">
|
||||||
<i class="bi bi-currency-dollar"></i> Total Cost
|
<i class="bi bi-currency-dollar"></i> Total Cost
|
||||||
</h5>
|
</h5>
|
||||||
<h3 class="card-text">${{ "%.4f"|format(users|sum(attribute='total_cost')) }}</h3>
|
<h3 class="card-text">${{ "%.6f"|format(users|sum(attribute='total_cost')) }}</h3>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -78,8 +78,7 @@
|
|||||||
</h5>
|
</h5>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body p-0">
|
<div class="card-body p-0">
|
||||||
<div class="table-responsive">
|
<table class="table table-striped table-hover mb-0">
|
||||||
<table class="table table-striped table-hover mb-0">
|
|
||||||
<thead class="table-dark">
|
<thead class="table-dark">
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col">
|
<th scope="col">
|
||||||
@ -143,16 +142,15 @@
|
|||||||
<strong>{{ "{:,}".format(user.total_tokens) }}</strong>
|
<strong>{{ "{:,}".format(user.total_tokens) }}</strong>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<span class="badge bg-warning text-dark">${{ user.total_cost }}</span>
|
<span class="badge bg-warning text-dark">${{ "%.6f"|format(user.total_cost) }}</span>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<small class="text-muted">${{ "%.4f"|format(user.total_cost / user.total_calls) }}</small>
|
<small class="text-muted">${{ "%.6f"|format(user.total_cost / user.total_calls) }}</small>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -172,7 +170,7 @@
|
|||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<div class="d-flex justify-content-between align-items-center mb-1">
|
<div class="d-flex justify-content-between align-items-center mb-1">
|
||||||
<small class="fw-bold">{{ user.email }}</small>
|
<small class="fw-bold">{{ user.email }}</small>
|
||||||
<small class="text-muted">${{ user.total_cost }}</small>
|
<small class="text-muted">${{ "%.6f"|format(user.total_cost) }}</small>
|
||||||
</div>
|
</div>
|
||||||
<div class="progress" style="height: 20px;">
|
<div class="progress" style="height: 20px;">
|
||||||
<div class="progress-bar bg-primary" role="progressbar"
|
<div class="progress-bar bg-primary" role="progressbar"
|
||||||
|
Reference in New Issue
Block a user