forked from phoenix/litellm-mirror
Merge pull request #5574 from BerriAI/litellm_tags_use_views
[Feat-Proxy] Use DB Views to Get spend per Tag (Usage endpoints)
This commit is contained in:
commit
09a4568172
2 changed files with 36 additions and 29 deletions
|
@ -47,17 +47,14 @@ async def ui_get_spend_by_tags(
|
|||
if tags_list is None or (isinstance(tags_list, list) and "all-tags" in tags_list):
|
||||
# Get spend for all tags
|
||||
sql_query = """
|
||||
SELECT
|
||||
jsonb_array_elements_text(request_tags) AS individual_request_tag,
|
||||
DATE(s."startTime") AS spend_date,
|
||||
COUNT(*) AS log_count,
|
||||
SUM(spend) AS total_spend
|
||||
FROM "LiteLLM_SpendLogs" s
|
||||
WHERE
|
||||
DATE(s."startTime") >= $1::date
|
||||
AND DATE(s."startTime") <= $2::date
|
||||
GROUP BY individual_request_tag, spend_date
|
||||
ORDER BY total_spend DESC;
|
||||
SELECT
|
||||
individual_request_tag,
|
||||
spend_date,
|
||||
log_count,
|
||||
total_spend
|
||||
FROM DailyTagSpend
|
||||
WHERE spend_date >= $1::date AND spend_date <= $2::date
|
||||
ORDER BY total_spend DESC;
|
||||
"""
|
||||
response = await prisma_client.db.query_raw(
|
||||
sql_query,
|
||||
|
@ -67,23 +64,15 @@ async def ui_get_spend_by_tags(
|
|||
else:
|
||||
# filter by tags list
|
||||
sql_query = """
|
||||
SELECT
|
||||
individual_request_tag,
|
||||
COUNT(*) AS log_count,
|
||||
SUM(spend) AS total_spend
|
||||
FROM (
|
||||
SELECT
|
||||
jsonb_array_elements_text(request_tags) AS individual_request_tag,
|
||||
DATE(s."startTime") AS spend_date,
|
||||
spend
|
||||
FROM "LiteLLM_SpendLogs" s
|
||||
WHERE
|
||||
DATE(s."startTime") >= $1::date
|
||||
AND DATE(s."startTime") <= $2::date
|
||||
) AS subquery
|
||||
WHERE individual_request_tag = ANY($3::text[])
|
||||
GROUP BY individual_request_tag
|
||||
ORDER BY total_spend DESC;
|
||||
SELECT
|
||||
individual_request_tag,
|
||||
SUM(log_count) AS log_count,
|
||||
SUM(total_spend) AS total_spend
|
||||
FROM DailyTagSpend
|
||||
WHERE spend_date >= $1::date AND spend_date <= $2::date
|
||||
AND individual_request_tag = ANY($3::text[])
|
||||
GROUP BY individual_request_tag
|
||||
ORDER BY total_spend DESC;
|
||||
"""
|
||||
response = await prisma_client.db.query_raw(
|
||||
sql_query,
|
||||
|
|
|
@ -974,7 +974,7 @@ class PrismaClient:
|
|||
)
|
||||
"""
|
||||
)
|
||||
if ret[0]["sum"] == 7:
|
||||
if ret[0]["sum"] == 8:
|
||||
print("All necessary views exist!") # noqa
|
||||
return
|
||||
except Exception:
|
||||
|
@ -1124,6 +1124,24 @@ class PrismaClient:
|
|||
|
||||
print("MonthlyGlobalSpendPerUserPerKey Created!") # noqa
|
||||
|
||||
try:
|
||||
await self.db.query_raw("""SELECT 1 FROM "DailyTagSpend" LIMIT 1""")
|
||||
print("DailyTagSpend Exists!") # noqa
|
||||
except Exception as e:
|
||||
sql_query = """
|
||||
CREATE OR REPLACE VIEW DailyTagSpend AS
|
||||
SELECT
|
||||
jsonb_array_elements_text(request_tags) AS individual_request_tag,
|
||||
DATE(s."startTime") AS spend_date,
|
||||
COUNT(*) AS log_count,
|
||||
SUM(spend) AS total_spend
|
||||
FROM "LiteLLM_SpendLogs" s
|
||||
GROUP BY individual_request_tag, DATE(s."startTime");
|
||||
"""
|
||||
await self.db.execute_raw(query=sql_query)
|
||||
|
||||
print("DailyTagSpend Created!") # noqa
|
||||
|
||||
try:
|
||||
await self.db.query_raw(
|
||||
"""SELECT 1 FROM "Last30dTopEndUsersSpend" LIMIT 1"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue