optmize UI
This commit is contained in:
45
main.py
45
main.py
@ -137,18 +137,25 @@ async def get_24h_stats():
|
||||
return {
|
||||
"total_chats": total_chats,
|
||||
"total_tokens": total_tokens,
|
||||
"total_cost": round(total_cost, 4)
|
||||
"total_cost": round(total_cost, 6)
|
||||
}
|
||||
|
||||
async def get_recent_logs(limit: int = 100):
|
||||
"""Get recent API call logs"""
|
||||
async def get_recent_logs(page: int = 1, page_size: int = 100):
|
||||
"""Get recent API call logs with pagination"""
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
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("""
|
||||
SELECT timestamp, model_id, user_email, input_tokens, output_tokens, cost_usd
|
||||
FROM api_calls
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT ?
|
||||
""", (limit,))
|
||||
LIMIT ? OFFSET ?
|
||||
""", (page_size, offset))
|
||||
|
||||
logs = []
|
||||
async for row in cursor:
|
||||
@ -160,10 +167,22 @@ async def get_recent_logs(limit: int = 100):
|
||||
"input_tokens": row[3],
|
||||
"output_tokens": 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():
|
||||
"""Get user consumption statistics"""
|
||||
@ -189,7 +208,7 @@ async def get_users_report():
|
||||
"total_input_tokens": row[2],
|
||||
"total_output_tokens": row[3],
|
||||
"total_tokens": row[4],
|
||||
"total_cost": round(row[5], 4)
|
||||
"total_cost": round(row[5], 6)
|
||||
})
|
||||
|
||||
return users
|
||||
@ -227,12 +246,16 @@ async def dashboard(request: Request, _: str = Depends(verify_admin_basic_auth))
|
||||
})
|
||||
|
||||
@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"""
|
||||
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", {
|
||||
"request": request,
|
||||
"logs": logs
|
||||
"logs": result["logs"],
|
||||
"pagination": result["pagination"]
|
||||
})
|
||||
|
||||
@app.get("/dashboard/users_report", response_class=HTMLResponse)
|
||||
|
@ -62,7 +62,7 @@
|
||||
<h5 class="card-title text-warning">
|
||||
<i class="bi bi-currency-dollar"></i> Total Cost
|
||||
</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>
|
||||
</div>
|
||||
<div class="text-warning">
|
||||
|
@ -20,12 +20,54 @@
|
||||
<div class="col-12">
|
||||
<div class="alert alert-info">
|
||||
<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).
|
||||
</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 -->
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
@ -34,7 +76,6 @@
|
||||
</h5>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover mb-0">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
@ -83,56 +124,89 @@
|
||||
<strong>{{ "{:,}".format(log.total_tokens) }}</strong>
|
||||
</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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Statistics Summary -->
|
||||
<div class="row mt-4">
|
||||
<!-- Pagination -->
|
||||
{% if pagination.total_pages > 1 %}
|
||||
<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">${{ "%.4f"|format(logs|sum(attribute='cost_usd')) }}</h4>
|
||||
<small class="text-muted">Total Cost</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<nav aria-label="Logs pagination">
|
||||
<ul class="pagination justify-content-center">
|
||||
<!-- Previous button -->
|
||||
<li class="page-item {{ 'disabled' if not pagination.has_prev }}">
|
||||
{% if pagination.has_prev %}
|
||||
<a class="page-link" href="?page={{ pagination.current_page - 1 }}">
|
||||
<i class="bi bi-chevron-left"></i> Previous
|
||||
</a>
|
||||
{% else %}
|
||||
<span class="page-link">
|
||||
<i class="bi bi-chevron-left"></i> Previous
|
||||
</span>
|
||||
{% endif %}
|
||||
</li>
|
||||
|
||||
<!-- Page numbers -->
|
||||
{% set start_page = [1, pagination.current_page - 2]|max %}
|
||||
{% set end_page = [pagination.total_pages, pagination.current_page + 2]|min %}
|
||||
|
||||
{% if start_page > 1 %}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="?page=1">1</a>
|
||||
</li>
|
||||
{% if start_page > 2 %}
|
||||
<li class="page-item disabled">
|
||||
<span class="page-link">...</span>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for page_num in range(start_page, end_page + 1) %}
|
||||
<li class="page-item {{ 'active' if page_num == pagination.current_page }}">
|
||||
{% if page_num == pagination.current_page %}
|
||||
<span class="page-link">{{ page_num }}</span>
|
||||
{% 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>
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
{% else %}
|
||||
<!-- No Data -->
|
||||
@ -155,9 +229,7 @@
|
||||
|
||||
{% block scripts %}
|
||||
<script>
|
||||
// Auto-refresh every 30 seconds
|
||||
setInterval(() => {
|
||||
window.location.reload();
|
||||
}, 30000);
|
||||
// Note: Auto-refresh disabled for pagination functionality
|
||||
// Users can manually refresh using the refresh button
|
||||
</script>
|
||||
{% endblock %}
|
@ -64,7 +64,7 @@
|
||||
<h5 class="card-title text-warning">
|
||||
<i class="bi bi-currency-dollar"></i> Total Cost
|
||||
</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>
|
||||
@ -78,7 +78,6 @@
|
||||
</h5>
|
||||
</div>
|
||||
<div class="card-body p-0">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover mb-0">
|
||||
<thead class="table-dark">
|
||||
<tr>
|
||||
@ -143,17 +142,16 @@
|
||||
<strong>{{ "{:,}".format(user.total_tokens) }}</strong>
|
||||
</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>
|
||||
<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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Top Users Chart (Simple Visual) -->
|
||||
@ -172,7 +170,7 @@
|
||||
<div class="mb-3">
|
||||
<div class="d-flex justify-content-between align-items-center mb-1">
|
||||
<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 class="progress" style="height: 20px;">
|
||||
<div class="progress-bar bg-primary" role="progressbar"
|
||||
|
Reference in New Issue
Block a user