diff --git a/src/routers/token_usage.py b/src/routers/token_usage.py index 6fcecfd..c586635 100644 --- a/src/routers/token_usage.py +++ b/src/routers/token_usage.py @@ -76,6 +76,7 @@ async def get_org_usage(org_id: int, admin=Depends(get_current_admin)): return [{ "year_month": row["year_month"], + "source": row["source"] if "source" in row.keys() else "monitor", "input_tokens": row["input_tokens"], "output_tokens": row["output_tokens"], "cache_creation_tokens": row["cache_creation_tokens"], @@ -98,7 +99,26 @@ async def get_org_current_usage(org_id: int, admin=Depends(get_current_admin)): cursor = await db.execute( "SELECT * FROM token_usage_monthly WHERE organization_id = ? AND year_month = ?", (org_id, year_month)) - usage = await cursor.fetchone() + usage_rows = await cursor.fetchall() + # Summe ueber alle Sources + usage = { + "input_tokens": sum(r["input_tokens"] for r in usage_rows), + "output_tokens": sum(r["output_tokens"] for r in usage_rows), + "total_cost_usd": sum(r["total_cost_usd"] for r in usage_rows), + "api_calls": sum(r["api_calls"] for r in usage_rows), + "refresh_count": sum(r["refresh_count"] for r in usage_rows), + } + # Per-Source Aufschluesselung + usage_by_source = {} + for r in usage_rows: + src = r["source"] if "source" in r.keys() else "monitor" + usage_by_source[src] = { + "input_tokens": r["input_tokens"], + "output_tokens": r["output_tokens"], + "total_cost_usd": round(r["total_cost_usd"], 2), + "api_calls": r["api_calls"], + "refresh_count": r["refresh_count"], + } cursor = await db.execute( "SELECT credits_total, credits_used, cost_per_credit, token_budget_usd, budget_warning_percent FROM licenses WHERE organization_id = ? AND status = 'active' ORDER BY id DESC LIMIT 1", @@ -111,12 +131,13 @@ async def get_org_current_usage(org_id: int, admin=Depends(get_current_admin)): return { "year_month": year_month, "usage": { - "input_tokens": usage["input_tokens"] if usage else 0, - "output_tokens": usage["output_tokens"] if usage else 0, - "total_cost_usd": round(usage["total_cost_usd"], 2) if usage else 0, - "api_calls": usage["api_calls"] if usage else 0, - "refresh_count": usage["refresh_count"] if usage else 0, + "input_tokens": usage["input_tokens"], + "output_tokens": usage["output_tokens"], + "total_cost_usd": round(usage["total_cost_usd"], 2), + "api_calls": usage["api_calls"], + "refresh_count": usage["refresh_count"], }, + "usage_by_source": usage_by_source, "budget": { "credits_total": credits_total, "credits_used": round(credits_used, 1) if credits_used else 0, diff --git a/src/static/dashboard.html b/src/static/dashboard.html index a629e83..1bb3692 100644 --- a/src/static/dashboard.html +++ b/src/static/dashboard.html @@ -7,7 +7,13 @@ - + + +