return error client side from spend endpoints

This commit is contained in:
Ishaan Jaff
2024-09-04 17:20:47 -07:00
parent 21a89f15de
commit 3eb0325063
@@ -1752,32 +1752,53 @@ async def global_spend_logs(
More efficient implementation of /spend/logs, by creating a view over the spend logs table.
"""
import traceback
from litellm.proxy.proxy_server import prisma_client
if prisma_client is None:
try:
if prisma_client is None:
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if api_key is None:
sql_query = """SELECT * FROM "MonthlyGlobalSpend" ORDER BY "date";"""
response = await prisma_client.db.query_raw(query=sql_query)
return response
else:
sql_query = """
SELECT * FROM "MonthlyGlobalSpendPerKey"
WHERE "api_key" = $1
ORDER BY "date";
"""
response = await prisma_client.db.query_raw(sql_query, api_key)
return response
except Exception as e:
error_trace = traceback.format_exc()
error_str = str(e) + "\n" + error_trace
if isinstance(e, HTTPException):
raise ProxyException(
message=getattr(e, "detail", f"/global/spend/logs Error({error_str})"),
type="internal_error",
param=getattr(e, "param", "None"),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
raise e
raise ProxyException(
message="Prisma Client is not initialized",
message="/global/spend/logs Error" + error_str,
type="internal_error",
param="None",
param=getattr(e, "param", "None"),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if api_key is None:
sql_query = """SELECT * FROM "MonthlyGlobalSpend" ORDER BY "date";"""
response = await prisma_client.db.query_raw(query=sql_query)
return response
else:
sql_query = """
SELECT * FROM "MonthlyGlobalSpendPerKey"
WHERE "api_key" = $1
ORDER BY "date";
"""
response = await prisma_client.db.query_raw(sql_query, api_key)
return response
return
@router.get(