Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Add per-operation breakdown to admin token-stats and surface in System Stats UI

Murtaza Nasirfdba792d9308
diff --git a/src/api/admin.py b/src/api/admin.py
index e7750435..8b0128c8 100644
--- a/src/api/admin.py
+++ b/src/api/admin.py
@@ -445,6 +445,10 @@ def admin_get_token_stats():
return jsonify({'error': 'Unauthorized'}), 403
try:
+ from src.models import TokenUsage
+ from sqlalchemy import func, extract
+ from datetime import date
+
# Get today's usage
today_usage = token_tracker.get_today_usage()
@@ -459,11 +463,34 @@ def admin_get_token_stats():
total_monthly_tokens = current_month.get('tokens', 0)
total_monthly_cost = current_month.get('cost', 0)
+ # Per-operation breakdown for the current month so the UI can show
+ # how the total splits across summarization, chat, embedding, etc.
+ today = date.today()
+ op_rows = db.session.query(
+ TokenUsage.operation_type,
+ func.sum(TokenUsage.total_tokens).label('tokens'),
+ func.sum(TokenUsage.cost).label('cost'),
+ func.sum(TokenUsage.request_count).label('requests'),
+ ).filter(
+ extract('year', TokenUsage.date) == today.year,
+ extract('month', TokenUsage.date) == today.month,
+ ).group_by(TokenUsage.operation_type).all()
+ by_operation = [
+ {
+ 'operation_type': r.operation_type,
+ 'tokens': int(r.tokens or 0),
+ 'cost': float(r.cost or 0.0),
+ 'requests': int(r.requests or 0),
+ }
+ for r in op_rows
+ ]
+
return jsonify({
'today': today_usage,
'current_month': {
'tokens': total_monthly_tokens,
- 'cost': total_monthly_cost
+ 'cost': total_monthly_cost,
+ 'by_operation': by_operation,
},
'user_count_with_usage': len([u for u in user_stats if u['current_usage'] > 0]),
'users_over_80_percent': len([u for u in user_stats if u['percentage'] >= 80]),
diff --git a/templates/admin.html b/templates/admin.html
index c514cdac..299465a7 100644
--- a/templates/admin.html
+++ b/templates/admin.html
@@ -464,6 +464,25 @@
</div>
</div>
+ <!-- Operation breakdown for the current month -->
+ <div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm lg:col-span-2">
+ <h5 class="text-md font-medium text-[var(--text-secondary)] mb-3">Tokens by Operation (This Month)</h5>
+ <div v-if="(tokenStats.current_month?.by_operation || []).length === 0" class="text-sm text-[var(--text-muted)]">
+ No usage recorded yet this month.
+ </div>
+ <div v-else class="grid grid-cols-2 md:grid-cols-4 gap-3">
+ <div v-for="op in (tokenStats.current_month?.by_operation || [])"
+ :key="op.operation_type"
+ class="p-3 bg-[var(--bg-secondary)] rounded-lg border border-[var(--border-primary)]">
+ <p class="text-xs text-[var(--text-muted)] capitalize">${ op.operation_type.replace('_', ' ') }</p>
+ <p class="text-base font-semibold text-[var(--text-primary)]">${ op.tokens.toLocaleString() }</p>
+ <p class="text-xs text-[var(--text-muted)]">
+ ${ op.requests } req<span v-if="op.cost > 0"> · $${ op.cost.toFixed(4) }</span>
+ </p>
+ </div>
+ </div>
+ </div>
+
<!-- Per-User Token Usage -->
<div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] shadow-sm">
<h5 class="text-md font-medium text-[var(--text-secondary)] mb-3">User Token Usage (This Month)</h5>