mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 19:54:13 +00:00
show /spend/logs for internal users
This commit is contained in:
parent
191dfc7e11
commit
0fc7d81bf4
1 changed files with 60 additions and 1 deletions
|
@ -1515,6 +1515,7 @@ async def view_spend_logs(
|
||||||
default=None,
|
default=None,
|
||||||
description="Time till which to view key spend",
|
description="Time till which to view key spend",
|
||||||
),
|
),
|
||||||
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
View all spend logs, if request_id is provided, only logs for that request_id will be returned
|
View all spend logs, if request_id is provided, only logs for that request_id will be returned
|
||||||
|
@ -1545,6 +1546,12 @@ async def view_spend_logs(
|
||||||
"""
|
"""
|
||||||
from litellm.proxy.proxy_server import prisma_client
|
from litellm.proxy.proxy_server import prisma_client
|
||||||
|
|
||||||
|
if (
|
||||||
|
user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER
|
||||||
|
or user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER_VIEW_ONLY
|
||||||
|
):
|
||||||
|
user_id = user_api_key_dict.user_id
|
||||||
|
|
||||||
try:
|
try:
|
||||||
verbose_proxy_logger.debug("inside view_spend_logs")
|
verbose_proxy_logger.debug("inside view_spend_logs")
|
||||||
if prisma_client is None:
|
if prisma_client is None:
|
||||||
|
@ -1733,6 +1740,45 @@ async def global_spend_reset():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def global_spend_for_internal_user(
|
||||||
|
api_key: Optional[str] = None,
|
||||||
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
||||||
|
):
|
||||||
|
from litellm.proxy.proxy_server import prisma_client
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
|
||||||
|
user_id = user_api_key_dict.user_id
|
||||||
|
if user_id is None:
|
||||||
|
raise ValueError(f"/global/spend/logs Error: User ID is None")
|
||||||
|
if api_key is not None:
|
||||||
|
sql_query = """
|
||||||
|
SELECT * FROM "MonthlyGlobalSpendPerUserPerKey"
|
||||||
|
WHERE "api_key" = $1 AND "user" = $2
|
||||||
|
ORDER BY "date";
|
||||||
|
"""
|
||||||
|
|
||||||
|
response = await prisma_client.db.query_raw(sql_query, api_key, user_id)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
sql_query = """SELECT * FROM "MonthlyGlobalSpendPerUserPerKey" WHERE "user" = $1 ORDER BY "date";"""
|
||||||
|
|
||||||
|
response = await prisma_client.db.query_raw(sql_query, user_id)
|
||||||
|
|
||||||
|
return response
|
||||||
|
except Exception as e:
|
||||||
|
verbose_proxy_logger.error(f"/global/spend/logs Error: {str(e)}")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/global/spend/logs",
|
"/global/spend/logs",
|
||||||
tags=["Budget & Spend Tracking"],
|
tags=["Budget & Spend Tracking"],
|
||||||
|
@ -1743,7 +1789,8 @@ async def global_spend_logs(
|
||||||
api_key: str = fastapi.Query(
|
api_key: str = fastapi.Query(
|
||||||
default=None,
|
default=None,
|
||||||
description="API Key to get global spend (spend per day for last 30d). Admin-only endpoint",
|
description="API Key to get global spend (spend per day for last 30d). Admin-only endpoint",
|
||||||
)
|
),
|
||||||
|
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
[BETA] This is a beta endpoint. It will change.
|
[BETA] This is a beta endpoint. It will change.
|
||||||
|
@ -1764,6 +1811,17 @@ async def global_spend_logs(
|
||||||
param="None",
|
param="None",
|
||||||
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER
|
||||||
|
or user_api_key_dict.user_role == LitellmUserRoles.INTERNAL_USER_VIEW_ONLY
|
||||||
|
):
|
||||||
|
response = await global_spend_for_internal_user(
|
||||||
|
api_key=api_key, user_api_key_dict=user_api_key_dict
|
||||||
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
if api_key is None:
|
if api_key is None:
|
||||||
sql_query = """SELECT * FROM "MonthlyGlobalSpend" ORDER BY "date";"""
|
sql_query = """SELECT * FROM "MonthlyGlobalSpend" ORDER BY "date";"""
|
||||||
|
|
||||||
|
@ -1784,6 +1842,7 @@ async def global_spend_logs(
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_trace = traceback.format_exc()
|
error_trace = traceback.format_exc()
|
||||||
error_str = str(e) + "\n" + error_trace
|
error_str = str(e) + "\n" + error_trace
|
||||||
|
verbose_proxy_logger.error(f"/global/spend/logs Error: {error_str}")
|
||||||
if isinstance(e, HTTPException):
|
if isinstance(e, HTTPException):
|
||||||
raise ProxyException(
|
raise ProxyException(
|
||||||
message=getattr(e, "detail", f"/global/spend/logs Error({error_str})"),
|
message=getattr(e, "detail", f"/global/spend/logs Error({error_str})"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue