apac-ops-bot/backend/app/schemas/token.py
SamoilenkoVadym 2e6597ee08 Add admin analytics and update OpenAI integration
Backend changes:
- Add admin analytics endpoints for daily usage per user
- Add GET /tokens/daily-users endpoint with date/user breakdown
- Update OpenAI SDK from 1.58.1 to 2.6.1
- Switch from Assistants API to Responses API with file_search tool
- Implement strict RAG-only system instructions
- Add citation validation to prevent hallucinations
- Add get_daily_usage_by_user repository method
- Add DailyUserUsage schema for admin analytics

Frontend changes:
- Implement comprehensive admin usage dashboard
- Add overall system statistics (users, conversations, messages, tokens, cost)
- Add daily usage table with per-user breakdown
- Add chat state clearing on logout and user change for isolation
- Center welcome message and input field in chat interface
- Add admin-specific styling for usage analytics tables
- Fix useCallback dependencies to prevent infinite loops

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-27 21:36:36 +00:00

53 lines
1.1 KiB
Python

"""
Token Usage Schemas
Pydantic models for token usage analytics
"""
from pydantic import BaseModel
from typing import List, Optional
class TokenUsageSummary(BaseModel):
"""Token usage summary for user"""
total_tokens: int
total_cost_usd: float
daily_breakdown: List[dict]
period_days: Optional[int] = None
class DailyUsage(BaseModel):
"""Daily token usage"""
date: str
tokens: int
cost: float
class UserTokenStats(BaseModel):
"""Token usage statistics per user (for admin)"""
user_id: str
user_email: str
user_name: str
total_tokens: int
prompt_tokens: int
completion_tokens: int
cached_tokens: int
total_cost_usd: float
message_count: int
conversation_count: int
avg_tokens_per_message: float
last_activity: Optional[str] = None
class DailyUserUsage(BaseModel):
"""Daily token usage per user (for admin)"""
date: str
user_id: str
user_email: str
user_name: str
total_tokens: int
prompt_tokens: int
completion_tokens: int
cached_tokens: int
total_cost_usd: float
message_count: int