mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-05 08:07:05 +00:00
Use direct LiteLLM_SpendLogs query for global spend to reduce DB CPU and impact on other APIs
This commit is contained in:
parent
4314cf5029
commit
093fccde2e
2 changed files with 4 additions and 2 deletions
|
|
@ -227,7 +227,8 @@ async def _fetch_global_spend_with_event_coordination(
|
|||
"""
|
||||
|
||||
async def _load_global_spend() -> Optional[float]:
|
||||
sql_query = """SELECT SUM(spend) AS total_spend FROM "MonthlyGlobalSpend";"""
|
||||
# Single aggregation on base table (same result, less CPU than SUM over MonthlyGlobalSpend view)
|
||||
sql_query = """SELECT SUM("spend") AS total_spend FROM "LiteLLM_SpendLogs" WHERE "startTime" >= (CURRENT_DATE - INTERVAL '30 days')"""
|
||||
response = await prisma_client.db.query_raw(query=sql_query)
|
||||
val = response[0]["total_spend"]
|
||||
return float(val) if val is not None else None
|
||||
|
|
|
|||
|
|
@ -2423,7 +2423,8 @@ async def global_spend():
|
|||
|
||||
if prisma_client is None:
|
||||
raise HTTPException(status_code=500, detail={"error": "No db connected"})
|
||||
sql_query = """SELECT SUM(spend) as total_spend FROM "MonthlyGlobalSpend";"""
|
||||
# Single aggregation on base table (same result, less CPU than SUM over MonthlyGlobalSpend view)
|
||||
sql_query = """SELECT SUM("spend") AS total_spend FROM "LiteLLM_SpendLogs" WHERE "startTime" >= (CURRENT_DATE - INTERVAL '30 days')"""
|
||||
response = await prisma_client.db.query_raw(query=sql_query)
|
||||
if response is not None:
|
||||
if isinstance(response, list) and len(response) > 0:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue