Skip to content

Commit c76d2c6

Browse files
Refactor 'check_view_exists' logic (BerriAI#5659)
* fix(proxy/utils.py): comment out auto-upsert logic in check_view_exists Prevents proxy from failing on startup due to faulty logic * fix(db/migration_scripts/create_views.py): fix 'DailyTagSpend' quotation on check * fix(create_views.py): mongly global spend time period should be 30d not 20d * fix(schema.prisma): index on startTime and endUser for efficient UI querying
1 parent 5c1a70b commit c76d2c6

File tree

5 files changed

+214
-221
lines changed

5 files changed

+214
-221
lines changed

Diff for: litellm/proxy/db/create_views.py renamed to db_scripts/create_views.py

+2-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""
22
python script to pre-create all views required by LiteLLM Proxy Server
3-
4-
53
"""
64

75
import asyncio
@@ -27,30 +25,6 @@ async def check_view_exists():
2725

2826
# connect to dB
2927
await db.connect()
30-
# Check to see if all of the necessary views exist and if they do, simply return
31-
# This is more efficient because it lets us check for all views in one
32-
# query instead of multiple queries.
33-
try:
34-
ret = await db.query_raw(
35-
"""
36-
SELECT SUM(1) FROM pg_views
37-
WHERE schemaname = 'public' AND viewname IN (
38-
'LiteLLM_VerificationTokenView',
39-
'MonthlyGlobalSpend',
40-
'Last30dKeysBySpend',
41-
'Last30dModelsBySpend',
42-
'MonthlyGlobalSpendPerKey',
43-
'MonthlyGlobalSpendPerUserPerKey',
44-
'Last30dTopEndUsersSpend'
45-
)
46-
"""
47-
)
48-
if ret[0]["sum"] == 8:
49-
print("All necessary views exist!") # noqa
50-
return
51-
except Exception:
52-
pass
53-
5428
try:
5529
# Try to select one row from the view
5630
await db.query_raw("""SELECT 1 FROM "LiteLLM_VerificationTokenView" LIMIT 1""")
@@ -180,7 +154,7 @@ async def check_view_exists():
180154
FROM
181155
"LiteLLM_SpendLogs"
182156
WHERE
183-
"startTime" >= (CURRENT_DATE - INTERVAL '20 days')
157+
"startTime" >= (CURRENT_DATE - INTERVAL '30 days')
184158
GROUP BY
185159
DATE("startTime"),
186160
"user",
@@ -191,7 +165,7 @@ async def check_view_exists():
191165
print("MonthlyGlobalSpendPerUserPerKey Created!") # noqa
192166

193167
try:
194-
await db.query_raw("""SELECT 1 FROM "DailyTagSpend" LIMIT 1""")
168+
await db.query_raw("""SELECT 1 FROM DailyTagSpend LIMIT 1""")
195169
print("DailyTagSpend Exists!") # noqa
196170
except Exception as e:
197171
sql_query = """

Diff for: litellm/proxy/db/dynamo_db.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Deprecated. Only PostgresSQL is supported.
3+
"""
4+
15
import json
26
from datetime import datetime
37
from typing import Any, List, Literal, Optional, Union

Diff for: litellm/proxy/schema.prisma

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ model LiteLLM_Config {
168168
param_value Json?
169169
}
170170

171-
// View spend, model, hashed api_key per request
171+
// View spend, model, api_key per request
172172
model LiteLLM_SpendLogs {
173173
request_id String @id
174174
call_type String
@@ -192,6 +192,8 @@ model LiteLLM_SpendLogs {
192192
team_id String?
193193
end_user String?
194194
requester_ip_address String?
195+
@@index([startTime])
196+
@@index([end_user])
195197
}
196198

197199
// View spend, model, api_key per request

0 commit comments

Comments
 (0)