feat: Implement Max RPM tracking API, Codex API authentication, and a new dashboard pool status component.
This commit is contained in:
112
backend/internal/api/max_rpm.go
Normal file
112
backend/internal/api/max_rpm.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"codex-pool/internal/database"
|
||||
)
|
||||
|
||||
// MaxRPMData 最高 RPM 数据
|
||||
type MaxRPMData struct {
|
||||
MaxRPM int `json:"max_rpm"`
|
||||
Date string `json:"date"`
|
||||
UpdatedAt string `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
// GetMaxRPMToday 获取今日最高 RPM
|
||||
func GetMaxRPMToday() MaxRPMData {
|
||||
if database.Instance == nil {
|
||||
return MaxRPMData{MaxRPM: 0, Date: time.Now().Format("2006-01-02")}
|
||||
}
|
||||
|
||||
today := time.Now().Format("2006-01-02")
|
||||
|
||||
// 获取存储的日期
|
||||
storedDate, _ := database.Instance.GetConfig("max_rpm_date")
|
||||
|
||||
// 如果日期不是今天,重置
|
||||
if storedDate != today {
|
||||
return MaxRPMData{MaxRPM: 0, Date: today}
|
||||
}
|
||||
|
||||
// 获取今日最高 RPM
|
||||
maxRPMStr, _ := database.Instance.GetConfig("max_rpm_today")
|
||||
maxRPM, _ := strconv.Atoi(maxRPMStr)
|
||||
|
||||
updatedAt, _ := database.Instance.GetConfig("max_rpm_updated_at")
|
||||
|
||||
return MaxRPMData{
|
||||
MaxRPM: maxRPM,
|
||||
Date: today,
|
||||
UpdatedAt: updatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMaxRPM 更新今日最高 RPM(如果当前值更高)
|
||||
func UpdateMaxRPM(currentRPM int) bool {
|
||||
if database.Instance == nil || currentRPM <= 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
today := time.Now().Format("2006-01-02")
|
||||
|
||||
// 获取存储的日期
|
||||
storedDate, _ := database.Instance.GetConfig("max_rpm_date")
|
||||
|
||||
var currentMax int
|
||||
if storedDate == today {
|
||||
// 同一天,获取当前最高值
|
||||
maxRPMStr, _ := database.Instance.GetConfig("max_rpm_today")
|
||||
currentMax, _ = strconv.Atoi(maxRPMStr)
|
||||
} else {
|
||||
// 新的一天,重置
|
||||
currentMax = 0
|
||||
database.Instance.SetConfig("max_rpm_date", today)
|
||||
}
|
||||
|
||||
// 如果当前 RPM 更高,更新记录
|
||||
if currentRPM > currentMax {
|
||||
database.Instance.SetConfig("max_rpm_today", strconv.Itoa(currentRPM))
|
||||
database.Instance.SetConfig("max_rpm_updated_at", time.Now().Format("15:04:05"))
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// HandleGetMaxRPM GET /api/stats/max-rpm - 获取今日最高 RPM
|
||||
func HandleGetMaxRPM(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
Error(w, http.StatusMethodNotAllowed, "仅支持 GET")
|
||||
return
|
||||
}
|
||||
|
||||
data := GetMaxRPMToday()
|
||||
Success(w, data)
|
||||
}
|
||||
|
||||
// ExtractRPMFromDashboard 从仪表盘响应中提取 RPM 值
|
||||
func ExtractRPMFromDashboard(body []byte) int {
|
||||
var data map[string]interface{}
|
||||
if err := json.Unmarshal(body, &data); err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 尝试从不同的响应结构中提取 RPM
|
||||
// 格式1: { "rpm": 123 }
|
||||
if rpm, ok := data["rpm"].(float64); ok {
|
||||
return int(rpm)
|
||||
}
|
||||
|
||||
// 格式2: { "data": { "rpm": 123 } }
|
||||
if dataObj, ok := data["data"].(map[string]interface{}); ok {
|
||||
if rpm, ok := dataObj["rpm"].(float64); ok {
|
||||
return int(rpm)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user