feat(dashboard): 添加 RPM/TPM 性能指标
在 Dashboard 中用 RPM/TPM 卡片替换原来的"今日缓存"卡片, 实时显示最近1分钟的请求数和 Token 吞吐量。
This commit is contained in:
@@ -42,6 +42,10 @@ type DashboardStats struct {
|
||||
|
||||
// 系统运行统计
|
||||
AverageDurationMs float64 `json:"average_duration_ms"` // 平均响应时间
|
||||
|
||||
// 性能指标
|
||||
Rpm int64 `json:"rpm"` // 最近1分钟的请求数
|
||||
Tpm int64 `json:"tpm"` // 最近1分钟的Token数
|
||||
}
|
||||
|
||||
// TrendDataPoint represents a single point in trend data
|
||||
@@ -115,6 +119,10 @@ type UserDashboardStats struct {
|
||||
|
||||
// 性能统计
|
||||
AverageDurationMs float64 `json:"average_duration_ms"`
|
||||
|
||||
// 性能指标
|
||||
Rpm int64 `json:"rpm"` // 最近1分钟的请求数
|
||||
Tpm int64 `json:"tpm"` // 最近1分钟的Token数
|
||||
}
|
||||
|
||||
// UsageLogFilters represents filters for usage log queries
|
||||
|
||||
@@ -19,6 +19,29 @@ func NewUsageLogRepository(db *gorm.DB) *UsageLogRepository {
|
||||
return &UsageLogRepository{db: db}
|
||||
}
|
||||
|
||||
// getPerformanceStats 获取 RPM 和 TPM(可选按用户过滤)
|
||||
func (r *UsageLogRepository) getPerformanceStats(ctx context.Context, userID int64) (rpm, tpm int64) {
|
||||
oneMinuteAgo := time.Now().Add(-1 * time.Minute)
|
||||
var perfStats struct {
|
||||
RequestCount int64 `gorm:"column:request_count"`
|
||||
TokenCount int64 `gorm:"column:token_count"`
|
||||
}
|
||||
|
||||
db := r.db.WithContext(ctx).Model(&model.UsageLog{}).
|
||||
Select(`
|
||||
COUNT(*) as request_count,
|
||||
COALESCE(SUM(input_tokens + output_tokens), 0) as token_count
|
||||
`).
|
||||
Where("created_at >= ?", oneMinuteAgo)
|
||||
|
||||
if userID > 0 {
|
||||
db = db.Where("user_id = ?", userID)
|
||||
}
|
||||
|
||||
db.Scan(&perfStats)
|
||||
return perfStats.RequestCount, perfStats.TokenCount
|
||||
}
|
||||
|
||||
func (r *UsageLogRepository) Create(ctx context.Context, log *model.UsageLog) error {
|
||||
return r.db.WithContext(ctx).Create(log).Error
|
||||
}
|
||||
@@ -230,6 +253,9 @@ func (r *UsageLogRepository) GetDashboardStats(ctx context.Context) (*DashboardS
|
||||
stats.TodayCost = todayStats.TodayCost
|
||||
stats.TodayActualCost = todayStats.TodayActualCost
|
||||
|
||||
// 性能指标:RPM 和 TPM(最近1分钟,全局)
|
||||
stats.Rpm, stats.Tpm = r.getPerformanceStats(ctx, 0)
|
||||
|
||||
return &stats, nil
|
||||
}
|
||||
|
||||
@@ -544,6 +570,9 @@ func (r *UsageLogRepository) GetUserDashboardStats(ctx context.Context, userID i
|
||||
stats.TodayCost = todayStats.TodayCost
|
||||
stats.TodayActualCost = todayStats.TodayActualCost
|
||||
|
||||
// 性能指标:RPM 和 TPM(最近1分钟,仅统计该用户的请求)
|
||||
stats.Rpm, stats.Tpm = r.getPerformanceStats(ctx, userID)
|
||||
|
||||
return &stats, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user