(UI) - Fix show correct count of internal user keys on Users Page (#9082)

* get_user_key_counts

* fix get_user_key_counts

* fix get_user_key_counts

* test_get_users_filters_dashboard_keys

* remove unused func
This commit is contained in:
Ishaan Jaff 2025-03-08 16:13:18 -08:00 committed by GitHub
parent 73df319f4e
commit b41311bb21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 125 additions and 7 deletions

View file

@ -739,6 +739,43 @@ async def user_update(
)
async def get_user_key_counts(
prisma_client,
user_ids: Optional[List[str]] = None,
):
"""
Helper function to get the count of keys for each user using Prisma's count method.
Args:
prisma_client: The Prisma client instance
user_ids: List of user IDs to get key counts for
Returns:
Dictionary mapping user_id to key count
"""
from litellm.constants import UI_SESSION_TOKEN_TEAM_ID
if not user_ids or len(user_ids) == 0:
return {}
result = {}
# Get count for each user_id individually
for user_id in user_ids:
count = await prisma_client.db.litellm_verificationtoken.count(
where={
"user_id": user_id,
"OR": [
{"team_id": None},
{"team_id": {"not": UI_SESSION_TOKEN_TEAM_ID}},
],
}
)
result[user_id] = count
return result
@router.get(
"/user/get_users",
tags=["Internal User management"],
@ -830,14 +867,9 @@ async def get_users(
# Get key count for each user
if users is not None:
user_keys = await prisma_client.db.litellm_verificationtoken.group_by(
by=["user_id"],
count={"user_id": True},
where={"user_id": {"in": [user.user_id for user in users]}},
user_key_counts = await get_user_key_counts(
prisma_client, [user.user_id for user in users]
)
user_key_counts = {
item["user_id"]: item["_count"]["user_id"] for item in user_keys
}
else:
user_key_counts = {}