Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions app/api/routes/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from datetime import datetime, timedelta, UTC
from enum import StrEnum

from app.api.dependencies import get_async_db, get_current_active_user
from app.api.dependencies import get_async_db, get_current_active_user, get_current_active_user_from_clerk
from app.models.user import User
from app.models.usage_tracker import UsageTracker
from app.models.provider_key import ProviderKey
from app.models.forge_api_key import ForgeApiKey
from app.api.schemas.statistic import (
UsageRealtimeResponse,
UsageSummaryResponse,
ForgeKeyUsageSummaryResponse,
ForgeKeysUsageSummaryResponse,
)

router = APIRouter()
Expand Down Expand Up @@ -79,6 +79,16 @@ async def get_usage_realtime(

return [UsageRealtimeResponse(**usage_stat) for usage_stat in usage_stats]

@router.get("/usage/realtime/clerk", response_model=list[UsageRealtimeResponse])
async def get_usage_realtime_clerk(
current_user: User = Depends(get_current_active_user_from_clerk),
db: AsyncSession = Depends(get_async_db),
offset: int = Query(0, ge=0),
limit: int = Query(10, ge=1),
):
return await get_usage_realtime(current_user, db, offset, limit)



class UsageSummaryTimeSpan(StrEnum):
day = "day"
Expand Down Expand Up @@ -151,31 +161,40 @@ async def get_usage_summary(
]


class ForgeKeyUsageTimeSpan(StrEnum):
@router.get("/usage/summary/clerk", response_model=list[UsageSummaryResponse])
async def get_usage_summary_clerk(
current_user: User = Depends(get_current_active_user_from_clerk),
db: AsyncSession = Depends(get_async_db),
span: UsageSummaryTimeSpan = Query(UsageSummaryTimeSpan.week),
):
return await get_usage_summary(current_user, db, span)


class ForgeKeysUsageTimeSpan(StrEnum):
day = "day"
week = "week"
month = "month"
year = "year"
all = "all"


@router.get("/forge-key/usage", response_model=list[ForgeKeyUsageSummaryResponse])
async def get_forge_key_usage(
@router.get("/forge-keys/usage", response_model=list[ForgeKeysUsageSummaryResponse])
async def get_forge_keys_usage(
current_user: User = Depends(get_current_active_user),
db: AsyncSession = Depends(get_async_db),
span: ForgeKeyUsageTimeSpan = Query(ForgeKeyUsageTimeSpan.week),
span: ForgeKeysUsageTimeSpan = Query(ForgeKeysUsageTimeSpan.week),
):
"""
Get usage summary for all the forge keys for the past day/week/month/year/all
"""
start_time = None
if span == ForgeKeyUsageTimeSpan.day:
if span == ForgeKeysUsageTimeSpan.day:
start_time = datetime.now(UTC) - timedelta(days=1)
elif span == ForgeKeyUsageTimeSpan.week:
elif span == ForgeKeysUsageTimeSpan.week:
start_time = datetime.now(UTC) - timedelta(weeks=1)
elif span == ForgeKeyUsageTimeSpan.month:
elif span == ForgeKeysUsageTimeSpan.month:
start_time = datetime.now(UTC) - timedelta(days=30)
elif span == ForgeKeyUsageTimeSpan.year:
elif span == ForgeKeysUsageTimeSpan.year:
start_time = datetime.now(UTC) - timedelta(days=365)

query = (
Expand All @@ -198,6 +217,15 @@ async def get_forge_key_usage(
rows = result.fetchall()

return [
ForgeKeyUsageSummaryResponse(forge_key=row.forge_key, tokens=row.tokens)
ForgeKeysUsageSummaryResponse(forge_key=row.forge_key, tokens=row.tokens)
for row in rows
]


@router.get("/forge-keys/usage/clerk", response_model=list[ForgeKeysUsageSummaryResponse])
async def get_forge_keys_usage_clerk(
current_user: User = Depends(get_current_active_user_from_clerk),
db: AsyncSession = Depends(get_async_db),
span: ForgeKeysUsageTimeSpan = Query(ForgeKeysUsageTimeSpan.week),
):
return await get_forge_keys_usage(current_user, db, span)
6 changes: 0 additions & 6 deletions app/api/routes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ async def update_user_me(
db.add(current_user)
await db.commit()
await db.refresh(current_user)
invalidate_user_cache(
current_user.id
) # Assuming user_id is the cache key for user object
# If API key was part of user model directly and changed, invalidate its cache too.
# However, API keys are now separate, so user update doesn't directly affect API key string caches
# unless an API key string was derived directly from user fields that changed.
return current_user


Expand Down
2 changes: 1 addition & 1 deletion app/api/schemas/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def convert_timestamp_to_iso(cls, v: datetime) -> str:
return v.isoformat()


class ForgeKeyUsageSummaryResponse(BaseModel):
class ForgeKeysUsageSummaryResponse(BaseModel):
forge_key: str
tokens: int

Expand Down
Loading