修复两个问题: 1. Token使用趋势图和模型分布图未响应筛选条件 2. 上午时段选择今天刷新后日期回退到前一天 前端修改: - 更新 dashboard API 类型定义,添加 model、account_id、group_id、stream 参数支持 - 修改 UsageView 趋势图加载逻辑,传递所有筛选参数到后端 - 修复日期格式化函数,使用本地时区避免 UTC 转换导致的日期偏移 后端修改: - Handler 层:接收并解析所有筛选参数(model、account_id、group_id、stream) - Service 层:传递完整的筛选参数到 Repository 层 - Repository 层:SQL 查询动态添加所有过滤条件 - 更新接口定义和所有调用点以保持一致性 影响范围: - /admin/dashboard/trend 端点现支持完整筛选 - /admin/dashboard/models 端点现支持完整筛选 - 用户在后台使用记录页面选择任意筛选条件时,趋势图和模型分布图会实时响应 - 日期选择器在任何时区下都能正确保持今天的选择
208 lines
4.7 KiB
TypeScript
208 lines
4.7 KiB
TypeScript
/**
|
|
* Admin Dashboard API endpoints
|
|
* Provides system-wide statistics and metrics
|
|
*/
|
|
|
|
import { apiClient } from '../client'
|
|
import type {
|
|
DashboardStats,
|
|
TrendDataPoint,
|
|
ModelStat,
|
|
ApiKeyUsageTrendPoint,
|
|
UserUsageTrendPoint
|
|
} from '@/types'
|
|
|
|
/**
|
|
* Get dashboard statistics
|
|
* @returns Dashboard statistics including users, keys, accounts, and token usage
|
|
*/
|
|
export async function getStats(): Promise<DashboardStats> {
|
|
const { data } = await apiClient.get<DashboardStats>('/admin/dashboard/stats')
|
|
return data
|
|
}
|
|
|
|
/**
|
|
* Get real-time metrics
|
|
* @returns Real-time system metrics
|
|
*/
|
|
export async function getRealtimeMetrics(): Promise<{
|
|
active_requests: number
|
|
requests_per_minute: number
|
|
average_response_time: number
|
|
error_rate: number
|
|
}> {
|
|
const { data } = await apiClient.get<{
|
|
active_requests: number
|
|
requests_per_minute: number
|
|
average_response_time: number
|
|
error_rate: number
|
|
}>('/admin/dashboard/realtime')
|
|
return data
|
|
}
|
|
|
|
export interface TrendParams {
|
|
start_date?: string
|
|
end_date?: string
|
|
granularity?: 'day' | 'hour'
|
|
user_id?: number
|
|
api_key_id?: number
|
|
model?: string
|
|
account_id?: number
|
|
group_id?: number
|
|
stream?: boolean
|
|
}
|
|
|
|
export interface TrendResponse {
|
|
trend: TrendDataPoint[]
|
|
start_date: string
|
|
end_date: string
|
|
granularity: string
|
|
}
|
|
|
|
/**
|
|
* Get usage trend data
|
|
* @param params - Query parameters for filtering
|
|
* @returns Usage trend data
|
|
*/
|
|
export async function getUsageTrend(params?: TrendParams): Promise<TrendResponse> {
|
|
const { data } = await apiClient.get<TrendResponse>('/admin/dashboard/trend', { params })
|
|
return data
|
|
}
|
|
|
|
export interface ModelStatsParams {
|
|
start_date?: string
|
|
end_date?: string
|
|
user_id?: number
|
|
api_key_id?: number
|
|
model?: string
|
|
account_id?: number
|
|
group_id?: number
|
|
stream?: boolean
|
|
}
|
|
|
|
export interface ModelStatsResponse {
|
|
models: ModelStat[]
|
|
start_date: string
|
|
end_date: string
|
|
}
|
|
|
|
/**
|
|
* Get model usage statistics
|
|
* @param params - Query parameters for filtering
|
|
* @returns Model usage statistics
|
|
*/
|
|
export async function getModelStats(params?: ModelStatsParams): Promise<ModelStatsResponse> {
|
|
const { data } = await apiClient.get<ModelStatsResponse>('/admin/dashboard/models', { params })
|
|
return data
|
|
}
|
|
|
|
export interface ApiKeyTrendParams extends TrendParams {
|
|
limit?: number
|
|
}
|
|
|
|
export interface ApiKeyTrendResponse {
|
|
trend: ApiKeyUsageTrendPoint[]
|
|
start_date: string
|
|
end_date: string
|
|
granularity: string
|
|
}
|
|
|
|
/**
|
|
* Get API key usage trend data
|
|
* @param params - Query parameters for filtering
|
|
* @returns API key usage trend data
|
|
*/
|
|
export async function getApiKeyUsageTrend(
|
|
params?: ApiKeyTrendParams
|
|
): Promise<ApiKeyTrendResponse> {
|
|
const { data } = await apiClient.get<ApiKeyTrendResponse>('/admin/dashboard/api-keys-trend', {
|
|
params
|
|
})
|
|
return data
|
|
}
|
|
|
|
export interface UserTrendParams extends TrendParams {
|
|
limit?: number
|
|
}
|
|
|
|
export interface UserTrendResponse {
|
|
trend: UserUsageTrendPoint[]
|
|
start_date: string
|
|
end_date: string
|
|
granularity: string
|
|
}
|
|
|
|
/**
|
|
* Get user usage trend data
|
|
* @param params - Query parameters for filtering
|
|
* @returns User usage trend data
|
|
*/
|
|
export async function getUserUsageTrend(params?: UserTrendParams): Promise<UserTrendResponse> {
|
|
const { data } = await apiClient.get<UserTrendResponse>('/admin/dashboard/users-trend', {
|
|
params
|
|
})
|
|
return data
|
|
}
|
|
|
|
export interface BatchUserUsageStats {
|
|
user_id: number
|
|
today_actual_cost: number
|
|
total_actual_cost: number
|
|
}
|
|
|
|
export interface BatchUsersUsageResponse {
|
|
stats: Record<string, BatchUserUsageStats>
|
|
}
|
|
|
|
/**
|
|
* Get batch usage stats for multiple users
|
|
* @param userIds - Array of user IDs
|
|
* @returns Usage stats map keyed by user ID
|
|
*/
|
|
export async function getBatchUsersUsage(userIds: number[]): Promise<BatchUsersUsageResponse> {
|
|
const { data } = await apiClient.post<BatchUsersUsageResponse>('/admin/dashboard/users-usage', {
|
|
user_ids: userIds
|
|
})
|
|
return data
|
|
}
|
|
|
|
export interface BatchApiKeyUsageStats {
|
|
api_key_id: number
|
|
today_actual_cost: number
|
|
total_actual_cost: number
|
|
}
|
|
|
|
export interface BatchApiKeysUsageResponse {
|
|
stats: Record<string, BatchApiKeyUsageStats>
|
|
}
|
|
|
|
/**
|
|
* Get batch usage stats for multiple API keys
|
|
* @param apiKeyIds - Array of API key IDs
|
|
* @returns Usage stats map keyed by API key ID
|
|
*/
|
|
export async function getBatchApiKeysUsage(
|
|
apiKeyIds: number[]
|
|
): Promise<BatchApiKeysUsageResponse> {
|
|
const { data } = await apiClient.post<BatchApiKeysUsageResponse>(
|
|
'/admin/dashboard/api-keys-usage',
|
|
{
|
|
api_key_ids: apiKeyIds
|
|
}
|
|
)
|
|
return data
|
|
}
|
|
|
|
export const dashboardAPI = {
|
|
getStats,
|
|
getRealtimeMetrics,
|
|
getUsageTrend,
|
|
getModelStats,
|
|
getApiKeyUsageTrend,
|
|
getUserUsageTrend,
|
|
getBatchUsersUsage,
|
|
getBatchApiKeysUsage
|
|
}
|
|
|
|
export default dashboardAPI
|