mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
func usablePerfMetricGroups(c *gin.Context) []string {
|
|
userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup)
|
|
usableGroups := service.GetUserUsableGroups(userGroup)
|
|
// A non-nil empty list restricts the metrics query to zero groups.
|
|
groups := make([]string, 0, len(usableGroups))
|
|
for group := range usableGroups {
|
|
if group == "auto" || ratio_setting.ContainsGroupRatio(group) {
|
|
groups = append(groups, group)
|
|
}
|
|
}
|
|
return groups
|
|
}
|
|
|
|
func GetPerfMetricsSummary(c *gin.Context) {
|
|
getPerfMetricsSummary(c, usablePerfMetricGroups(c))
|
|
}
|
|
|
|
func GetDashboardPerfMetricsSummary(c *gin.Context) {
|
|
getPerfMetricsSummary(c, append(lo.Keys(ratio_setting.GetGroupRatioCopy()), "auto"))
|
|
}
|
|
|
|
func getPerfMetricsSummary(c *gin.Context, groups []string) {
|
|
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
|
|
}
|
|
result, err := perfmetrics.QuerySummaryAll(hours, groups, 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: usablePerfMetricGroups(c),
|
|
})
|
|
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,
|
|
})
|
|
}
|