Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 1m40s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 42m16s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
157 lines
4.6 KiB
Go
157 lines
4.6 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")
|
|
requestId := c.Query("request_id")
|
|
upstreamRequestId := c.Query("upstream_request_id")
|
|
logs, total, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), channel, group, requestId, upstreamRequestId, c.Query("correlation_key"), c.Query("attempt_status"))
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
if c.GetInt("role") < common.RoleRootUser {
|
|
model.FormatAdminLogs(logs)
|
|
} else {
|
|
model.FormatRootLogs(logs)
|
|
}
|
|
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")
|
|
requestId := c.Query("request_id")
|
|
upstreamRequestId := c.Query("upstream_request_id")
|
|
logs, total, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), group, requestId, upstreamRequestId,c.Query("attempt_status"))
|
|
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, c.Query("correlation_key"), c.Query("attempt_status"))
|
|
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,"",c.Query("attempt_status"))
|
|
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
|
|
}
|