Muse now forwards business context alongside each per-user token request, so new-api needs to ingest those tags for relay selection, billing audit, and log correlation without breaking its own internal request-id semantics. This adds a dedicated Muse request-context middleware, carries Muse tags into RelayInfo, and appends them into consume/error log payloads. Muse request ids are stored as separate business metadata instead of overwriting the internal request id used for pre-consume idempotency. Constraint: The relay layer must keep working for both /v1 and /v1beta request paths Constraint: Client-supplied Muse request ids cannot replace the internal request id used by new-api billing/idempotency logic Rejected: Overwriting common.RequestIdKey with X-Request-Id | mixes external business ids with internal idempotency ids Rejected: Adding new log table columns in phase 1 | this task only needs relay/log propagation, so other JSON is the minimal compatible path Confidence: medium Scope-risk: moderate Reversibility: clean Directive: Keep Muse business identifiers in dedicated context keys and log other fields; do not reuse internal request-id slots for external correlation ids Tested: go test ./middleware ./relay/common ./controller -run MuseRequestContext -count=1 Tested: go test ./middleware ./relay/common ./controller -count=1 Not-tested: Live relay call from Muse into a running new-api instance
174 lines
4.9 KiB
Go
174 lines
4.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func GetAllLogs(c *gin.Context) {
|
|
pageInfo := common.GetPageQuery(c)
|
|
logType, _ := strconv.Atoi(c.Query("type"))
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
username := c.Query("username")
|
|
tokenName := c.Query("token_name")
|
|
modelName := c.Query("model_name")
|
|
channel, _ := strconv.Atoi(c.Query("channel"))
|
|
group := c.Query("group")
|
|
// request_id filters the internal relay request id; muse_request_id is carried in log.other for drill-down.
|
|
requestId := c.Query("request_id")
|
|
logs, total, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), channel, group, requestId)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
pageInfo.SetTotal(int(total))
|
|
pageInfo.SetItems(logs)
|
|
common.ApiSuccess(c, pageInfo)
|
|
return
|
|
}
|
|
|
|
func GetUserLogs(c *gin.Context) {
|
|
pageInfo := common.GetPageQuery(c)
|
|
userId := c.GetInt("id")
|
|
logType, _ := strconv.Atoi(c.Query("type"))
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
tokenName := c.Query("token_name")
|
|
modelName := c.Query("model_name")
|
|
group := c.Query("group")
|
|
// request_id filters the internal relay request id; muse_request_id is carried in log.other for drill-down.
|
|
requestId := c.Query("request_id")
|
|
logs, total, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), group, requestId)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
pageInfo.SetTotal(int(total))
|
|
pageInfo.SetItems(logs)
|
|
common.ApiSuccess(c, pageInfo)
|
|
return
|
|
}
|
|
|
|
// Deprecated: SearchAllLogs 已废弃,前端未使用该接口。
|
|
func SearchAllLogs(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "该接口已废弃",
|
|
})
|
|
}
|
|
|
|
// Deprecated: SearchUserLogs 已废弃,前端未使用该接口。
|
|
func SearchUserLogs(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "该接口已废弃",
|
|
})
|
|
}
|
|
|
|
func GetLogByKey(c *gin.Context) {
|
|
tokenId := c.GetInt("token_id")
|
|
if tokenId == 0 {
|
|
c.JSON(200, gin.H{
|
|
"success": false,
|
|
"message": "无效的令牌",
|
|
})
|
|
return
|
|
}
|
|
logs, err := model.GetLogByTokenId(tokenId)
|
|
if err != nil {
|
|
c.JSON(200, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(200, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": logs,
|
|
})
|
|
}
|
|
|
|
func GetLogsStat(c *gin.Context) {
|
|
logType, _ := strconv.Atoi(c.Query("type"))
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
tokenName := c.Query("token_name")
|
|
username := c.Query("username")
|
|
modelName := c.Query("model_name")
|
|
channel, _ := strconv.Atoi(c.Query("channel"))
|
|
group := c.Query("group")
|
|
stat, err := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": gin.H{
|
|
"quota": stat.Quota,
|
|
"rpm": stat.Rpm,
|
|
"tpm": stat.Tpm,
|
|
},
|
|
})
|
|
return
|
|
}
|
|
|
|
func GetLogsSelfStat(c *gin.Context) {
|
|
username := c.GetString("username")
|
|
logType, _ := strconv.Atoi(c.Query("type"))
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
tokenName := c.Query("token_name")
|
|
modelName := c.Query("model_name")
|
|
channel, _ := strconv.Atoi(c.Query("channel"))
|
|
group := c.Query("group")
|
|
quotaNum, err := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
|
|
c.JSON(200, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": gin.H{
|
|
"quota": quotaNum.Quota,
|
|
"rpm": quotaNum.Rpm,
|
|
"tpm": quotaNum.Tpm,
|
|
//"token": tokenNum,
|
|
},
|
|
})
|
|
return
|
|
}
|
|
|
|
func DeleteHistoryLogs(c *gin.Context) {
|
|
targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
|
|
if targetTimestamp == 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "target timestamp is required",
|
|
})
|
|
return
|
|
}
|
|
count, err := model.DeleteOldLog(c.Request.Context(), targetTimestamp, 100)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": count,
|
|
})
|
|
return
|
|
}
|