(fix) show global spend on UI

This commit is contained in:
Ishaan Jaff 2024-03-18 18:15:08 -07:00
parent be6fb579df
commit 87dd3f1235

View file

@ -863,6 +863,7 @@ async def user_api_key_auth(
"/v2/key/info",
"/models",
"/v1/models",
"/global/spend",
"/global/spend/logs",
"/global/spend/keys",
"/global/spend/models",
@ -4567,6 +4568,50 @@ async def global_spend_logs(
return
@router.get(
"/global/spend",
tags=["Budget & Spend Tracking"],
dependencies=[Depends(user_api_key_auth)],
)
async def global_spend():
"""
[BETA] This is a beta endpoint. It will change.
View total spend across all proxy keys
"""
global prisma_client
total_spend = 0.0
total_proxy_budget = 0.0
if prisma_client is None:
raise HTTPException(status_code=500, detail={"error": "No db connected"})
sql_query = f"""
SELECT SUM(spend) AS total_spend
FROM "LiteLLM_VerificationToken";
;
"""
response = await prisma_client.db.query_raw(query=sql_query)
if response is not None:
if isinstance(response, list) and len(response) > 0:
total_spend = response[0].get("total_spend", 0.0)
sql_query = f"""
SELECT
*
FROM
"LiteLLM_UserTable"
WHERE
user_id = 'litellm-proxy-budget';
"""
user_response = await prisma_client.db.query_raw(query=sql_query)
if user_response is not None:
if isinstance(user_response, list) and len(user_response) > 0:
total_proxy_budget = user_response[0].get("max_budget", 0.0)
return {"spend": total_spend, "max_budget": total_proxy_budget}
@router.get(
"/global/spend/keys",
tags=["Budget & Spend Tracking"],