diff --git a/main.py b/main.py index 34ca6d1..68a2a77 100644 --- a/main.py +++ b/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) diff --git a/templates/dashboard.html b/templates/dashboard.html index 6249c0d..c845ae9 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -62,7 +62,7 @@
Total Cost
-

${{ stats.total_cost }}

+

${{ "%.6f"|format(stats.total_cost) }}

Last 24 hours
diff --git a/templates/logs.html b/templates/logs.html index ba12ca0..262b014 100644 --- a/templates/logs.html +++ b/templates/logs.html @@ -20,12 +20,54 @@
- Showing {{ logs|length }} most recent API calls. + Showing {{ logs|length }} API calls on page {{ pagination.current_page }} of {{ pagination.total_pages }}. + Total: {{ pagination.total_count }} records. Logs are ordered by timestamp (newest first).
+ +
+
+
+
+
+ Current Page Statistics +
+
+
+
+
+
+

{{ logs|length }}

+ Total Calls +
+
+
+
+

{{ "{:,}".format(logs|sum(attribute='input_tokens')) }}

+ Input Tokens +
+
+
+
+

{{ "{:,}".format(logs|sum(attribute='output_tokens')) }}

+ Output Tokens +
+
+
+
+

${{ "%.6f"|format(logs|sum(attribute='cost_usd')) }}

+ Total Cost +
+
+
+
+
+
+
+
@@ -34,8 +76,7 @@
-
- +
{% endfor %}
@@ -83,56 +124,89 @@ {{ "{:,}".format(log.total_tokens) }} - ${{ log.cost_usd }} + ${{ "%.6f"|format(log.cost_usd) }}
-
- -
+ +{% if pagination.total_pages > 1 %} +
-
-
-
- Current Page Statistics -
-
-
-
-
-
-

{{ logs|length }}

- Total Calls -
-
-
-
-

{{ "{:,}".format(logs|sum(attribute='input_tokens')) }}

- Input Tokens -
-
-
-
-

{{ "{:,}".format(logs|sum(attribute='output_tokens')) }}

- Output Tokens -
-
-
-
-

${{ "%.4f"|format(logs|sum(attribute='cost_usd')) }}

- Total Cost -
-
-
-
-
+
+{% endif %} + + {% else %} @@ -155,9 +229,7 @@ {% block scripts %} {% endblock %} \ No newline at end of file diff --git a/templates/users_report.html b/templates/users_report.html index ad0bb7a..6bb51ec 100644 --- a/templates/users_report.html +++ b/templates/users_report.html @@ -64,7 +64,7 @@
Total Cost
-

${{ "%.4f"|format(users|sum(attribute='total_cost')) }}

+

${{ "%.6f"|format(users|sum(attribute='total_cost')) }}

@@ -78,8 +78,7 @@
-
- +
{% endfor %}
@@ -143,16 +142,15 @@ {{ "{:,}".format(user.total_tokens) }} - ${{ user.total_cost }} + ${{ "%.6f"|format(user.total_cost) }} - ${{ "%.4f"|format(user.total_cost / user.total_calls) }} + ${{ "%.6f"|format(user.total_cost / user.total_calls) }}
-
@@ -172,7 +170,7 @@
{{ user.email }} - ${{ user.total_cost }} + ${{ "%.6f"|format(user.total_cost) }}