mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
fix /global/spend/end_users
This commit is contained in:
parent
faab704d28
commit
1eea4d1c90
1 changed files with 21 additions and 28 deletions
|
@ -5792,35 +5792,28 @@ async def global_spend_end_users(data: Optional[GlobalEndUsersSpend] = None):
|
||||||
if prisma_client is None:
|
if prisma_client is None:
|
||||||
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
||||||
|
|
||||||
if data is None:
|
"""
|
||||||
sql_query = f"""SELECT * FROM "Last30dTopEndUsersSpend";"""
|
Gets the top 100 end-users for a given api key
|
||||||
|
"""
|
||||||
|
startTime = datetime.now() - timedelta(days=30)
|
||||||
|
endTime = datetime.now()
|
||||||
|
|
||||||
response = await prisma_client.db.query_raw(query=sql_query)
|
sql_query = """
|
||||||
else:
|
SELECT
|
||||||
"""
|
end_user,
|
||||||
Gets the top 100 end-users for a given api key
|
COUNT(*) AS total_count,
|
||||||
"""
|
SUM(spend) AS total_spend
|
||||||
current_date = datetime.now()
|
FROM
|
||||||
past_date = current_date - timedelta(days=30)
|
"LiteLLM_SpendLogs"
|
||||||
response = await prisma_client.db.litellm_spendlogs.group_by( # type: ignore
|
GROUP BY
|
||||||
by=["end_user"],
|
end_user
|
||||||
where={
|
WHERE
|
||||||
"AND": [{"startTime": {"gte": past_date}}, {"api_key": data.api_key}] # type: ignore
|
"startTime" >= $1 AND "startTime" < $2
|
||||||
},
|
ORDER BY
|
||||||
sum={"spend": True},
|
total_spend DESC
|
||||||
order={"_sum": {"spend": "desc"}}, # type: ignore
|
LIMIT 100
|
||||||
take=100,
|
"""
|
||||||
count=True,
|
response = await prisma_client.db.query_raw(sql_query, startTime, endTime)
|
||||||
)
|
|
||||||
if response is not None and isinstance(response, list):
|
|
||||||
new_response = []
|
|
||||||
for r in response:
|
|
||||||
new_r = r
|
|
||||||
new_r["total_spend"] = r["_sum"]["spend"]
|
|
||||||
new_r["total_count"] = r["_count"]["_all"]
|
|
||||||
new_r.pop("_sum")
|
|
||||||
new_r.pop("_count")
|
|
||||||
new_response.append(new_r)
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue