Fix: Handle MySQL datetime objects and Decimal division in analytics

parent b844c677
...@@ -309,8 +309,15 @@ class Analytics: ...@@ -309,8 +309,15 @@ class Analytics:
# This gives more accurate rate limiting metrics # This gives more accurate rate limiting metrics
if first_request and last_request and total_tokens > 0: if first_request and last_request and total_tokens > 0:
try: try:
first_dt = datetime.fromisoformat(first_request) # Handle both string ISO formats and native datetime objects (MySQL returns datetime objects)
last_dt = datetime.fromisoformat(last_request) if isinstance(first_request, datetime):
first_dt = first_request
else:
first_dt = datetime.fromisoformat(first_request)
if isinstance(last_request, datetime):
last_dt = last_request
else:
last_dt = datetime.fromisoformat(last_request)
# Use actual duration between first and last request # Use actual duration between first and last request
# Add a minimum of 1 minute to avoid division by very small numbers # Add a minimum of 1 minute to avoid division by very small numbers
...@@ -320,9 +327,9 @@ class Analytics: ...@@ -320,9 +327,9 @@ class Analytics:
duration_days = actual_duration_seconds / 86400 duration_days = actual_duration_seconds / 86400
# Calculate rates based on actual usage period # Calculate rates based on actual usage period
tpm = int(total_tokens / duration_minutes) if duration_minutes > 0 else 0 tpm = int(float(total_tokens) / duration_minutes) if duration_minutes > 0 else 0
tph = int(total_tokens / duration_hours) if duration_hours > 0 else 0 tph = int(float(total_tokens) / duration_hours) if duration_hours > 0 else 0
tpd = int(total_tokens / duration_days) if duration_days > 0 else 0 tpd = int(float(total_tokens) / duration_days) if duration_days > 0 else 0
except Exception as e: except Exception as e:
# Fallback to query window if parsing fails # Fallback to query window if parsing fails
duration_seconds = (to_datetime - from_datetime).total_seconds() duration_seconds = (to_datetime - from_datetime).total_seconds()
...@@ -330,9 +337,9 @@ class Analytics: ...@@ -330,9 +337,9 @@ class Analytics:
duration_hours = duration_seconds / 3600 duration_hours = duration_seconds / 3600
duration_days = duration_seconds / 86400 duration_days = duration_seconds / 86400
tpm = int(total_tokens / duration_minutes) if duration_minutes > 0 else 0 tpm = int(float(total_tokens) / duration_minutes) if duration_minutes > 0 else 0
tph = int(total_tokens / duration_hours) if duration_hours > 0 else 0 tph = int(float(total_tokens) / duration_hours) if duration_hours > 0 else 0
tpd = int(total_tokens / duration_days) if duration_days > 0 else 0 tpd = int(float(total_tokens) / duration_days) if duration_days > 0 else 0
else: else:
tpm = tph = tpd = 0 tpm = tph = tpd = 0
......
...@@ -6703,7 +6703,7 @@ async def dashboard_user_cache_settings(request: Request): ...@@ -6703,7 +6703,7 @@ async def dashboard_user_cache_settings(request: Request):
@app.get("/api/user/cache-settings") @app.get("/api/user/cache-settings")
async def api_get_user_cache_settings(request: Request): async def api_get_user_cache_settings(request: Request):
"""Get user's cache settings""" """Get user's cache settings"""
auth_check = require_dashboard_auth(request) auth_check = require_api_auth(request)
if auth_check: if auth_check:
return auth_check return auth_check
...@@ -6728,7 +6728,7 @@ async def api_get_user_cache_settings(request: Request): ...@@ -6728,7 +6728,7 @@ async def api_get_user_cache_settings(request: Request):
@app.post("/api/user/cache-settings") @app.post("/api/user/cache-settings")
async def api_set_user_cache_setting(request: Request): async def api_set_user_cache_setting(request: Request):
"""Set user's cache setting""" """Set user's cache setting"""
auth_check = require_dashboard_auth(request) auth_check = require_api_auth(request)
if auth_check: if auth_check:
return auth_check return auth_check
...@@ -6756,7 +6756,7 @@ async def api_set_user_cache_setting(request: Request): ...@@ -6756,7 +6756,7 @@ async def api_set_user_cache_setting(request: Request):
@app.delete("/api/user/cache-settings") @app.delete("/api/user/cache-settings")
async def api_delete_user_cache_setting(request: Request): async def api_delete_user_cache_setting(request: Request):
"""Delete user's cache setting""" """Delete user's cache setting"""
auth_check = require_dashboard_auth(request) auth_check = require_api_auth(request)
if auth_check: if auth_check:
return auth_check return auth_check
......
...@@ -44,58 +44,7 @@ ...@@ -44,58 +44,7 @@
</div> </div>
</div> </div>
<!-- Provider-specific Settings -->
<div style="background: #16213e; border: 2px solid #4a9eff; border-radius: 8px; padding: 20px; margin-bottom: 20px;">
<h3 style="margin: 0 0 20px 0; color: #4a9eff;">
<i class="fas fa-server me-2"></i>Provider-specific Settings
</h3>
<div style="background: #1a1a2e; padding: 15px; border-radius: 8px; margin-bottom: 15px;">
<h5 style="margin: 0 0 15px 0; color: #e0e0e0;">Add Provider Setting</h5>
<div style="display: flex; gap: 15px; align-items: center; flex-wrap: wrap;">
<div style="flex: 1; min-width: 200px;">
<label style="color: #888; font-size: 14px;">Provider</label>
<select id="providerSelect" class="form-control" style="background: #16213e; border: 1px solid #0f3460; color: #e0e0e0;">
<option value="">Select provider...</option>
</select>
</div>
<div style="flex: 1; min-width: 200px;">
<label style="color: #888; font-size: 14px;">Model (optional)</label>
<input type="text" id="modelInput" class="form-control" style="background: #16213e; border: 1px solid #0f3460; color: #e0e0e0;" placeholder="Leave empty for all models">
</div>
<div style="flex: 0 0 auto;">
<label style="color: #888; font-size: 14px;">Enable Cache</label>
<div style="margin-top: 5px;">
<label class="form-check-label" style="margin-right: 10px; color: #888;">No</label>
<input type="checkbox" id="addProviderCacheToggle" checked>
<label class="form-check-label" style="margin-left: 10px; color: #888;">Yes</label>
</div>
</div>
<div style="flex: 0 0 auto; align-self: flex-end;">
<button type="button" class="btn" style="background: #4a9eff; color: white;" onclick="addProviderSetting()">
<i class="fas fa-plus me-2"></i>Add
</button>
</div>
</div>
</div>
<table class="table table-dark" style="margin: 0;">
<thead>
<tr>
<th>Provider</th>
<th>Model</th>
<th>Cache Enabled</th>
<th>Updated</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="cacheSettingsTable">
<tr>
<td colspan="5" style="text-align: center; color: #888;">Loading...</td>
</tr>
</tbody>
</table>
</div>
<!-- Back Button --> <!-- Back Button -->
<div style="margin-top: 30px;"> <div style="margin-top: 30px;">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment