mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-10-28 08:35:33 +08:00
Fetch the latest 24 nonempty hourly samples within 7 days for the visible models, while preserving the 24-hour summary metrics. Render only available samples without gray placeholders. Validated perf-metrics tests with SQLite 3.50.4, MySQL 5.7.44, and PostgreSQL 9.6.24; Go build; 36 frontend tests; frontend typecheck, lint, formatting, and production build.
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
|
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
func GetPerfMetricsSummary(c *gin.Context) {
|
|
hours := 24
|
|
if rawHours := c.Query("hours"); rawHours != "" {
|
|
if parsed, err := strconv.Atoi(rawHours); err == nil {
|
|
hours = parsed
|
|
}
|
|
}
|
|
|
|
recentModels := c.QueryArray("recent_model")
|
|
if len(recentModels) > perfmetrics.MaxRecentModels {
|
|
common.ApiErrorStatus(c, http.StatusBadRequest, common.NewMessage("Invalid parameters"))
|
|
return
|
|
}
|
|
activeGroups := append(lo.Keys(ratio_setting.GetGroupRatioCopy()), "auto")
|
|
result, err := perfmetrics.QuerySummaryAll(hours, activeGroups, recentModels...)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"data": result,
|
|
})
|
|
}
|
|
|
|
func GetPerfMetrics(c *gin.Context) {
|
|
modelName := c.Query("model")
|
|
if modelName == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"success": false,
|
|
"message": "model is required",
|
|
})
|
|
return
|
|
}
|
|
|
|
hours := 24
|
|
if rawHours := c.Query("hours"); rawHours != "" {
|
|
if parsed, err := strconv.Atoi(rawHours); err == nil {
|
|
hours = parsed
|
|
}
|
|
}
|
|
|
|
result, err := perfmetrics.Query(perfmetrics.QueryParams{
|
|
Model: modelName,
|
|
Group: c.Query("group"),
|
|
Hours: hours,
|
|
AllowedGroups: append(lo.Keys(ratio_setting.GetGroupRatioCopy()), "auto"),
|
|
})
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"data": result,
|
|
})
|
|
}
|