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
|
@ -48,15 +48,12 @@ async def ui_get_spend_by_tags(
|
||||||
# Get spend for all tags
|
# Get spend for all tags
|
||||||
sql_query = """
|
sql_query = """
|
||||||
SELECT
|
SELECT
|
||||||
jsonb_array_elements_text(request_tags) AS individual_request_tag,
|
individual_request_tag,
|
||||||
DATE(s."startTime") AS spend_date,
|
spend_date,
|
||||||
COUNT(*) AS log_count,
|
log_count,
|
||||||
SUM(spend) AS total_spend
|
total_spend
|
||||||
FROM "LiteLLM_SpendLogs" s
|
FROM DailyTagSpend
|
||||||
WHERE
|
WHERE spend_date >= $1::date AND spend_date <= $2::date
|
||||||
DATE(s."startTime") >= $1::date
|
|
||||||
AND DATE(s."startTime") <= $2::date
|
|
||||||
GROUP BY individual_request_tag, spend_date
|
|
||||||
ORDER BY total_spend DESC;
|
ORDER BY total_spend DESC;
|
||||||
"""
|
"""
|
||||||
response = await prisma_client.db.query_raw(
|
response = await prisma_client.db.query_raw(
|
||||||
|
@ -69,19 +66,11 @@ async def ui_get_spend_by_tags(
|
||||||
sql_query = """
|
sql_query = """
|
||||||
SELECT
|
SELECT
|
||||||
individual_request_tag,
|
individual_request_tag,
|
||||||
COUNT(*) AS log_count,
|
SUM(log_count) AS log_count,
|
||||||
SUM(spend) AS total_spend
|
SUM(total_spend) AS total_spend
|
||||||
FROM (
|
FROM DailyTagSpend
|
||||||
SELECT
|
WHERE spend_date >= $1::date AND spend_date <= $2::date
|
||||||
jsonb_array_elements_text(request_tags) AS individual_request_tag,
|
AND individual_request_tag = ANY($3::text[])
|
||||||
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
|
GROUP BY individual_request_tag
|
||||||
ORDER BY total_spend DESC;
|
ORDER BY total_spend DESC;
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -974,7 +974,7 @@ class PrismaClient:
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
if ret[0]["sum"] == 7:
|
if ret[0]["sum"] == 8:
|
||||||
print("All necessary views exist!") # noqa
|
print("All necessary views exist!") # noqa
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -1124,6 +1124,24 @@ class PrismaClient:
|
||||||
|
|
||||||
print("MonthlyGlobalSpendPerUserPerKey Created!") # noqa
|
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:
|
try:
|
||||||
await self.db.query_raw(
|
await self.db.query_raw(
|
||||||
"""SELECT 1 FROM "Last30dTopEndUsersSpend" LIMIT 1"""
|
"""SELECT 1 FROM "Last30dTopEndUsersSpend" LIMIT 1"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue