Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions controller/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func GetAllLogs(c *gin.Context) {
pageInfo := common.GetPageQuery(c)
logType, _ := strconv.Atoi(c.Query("type"))
logFilter := c.Query("log_filter")
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
username := c.Query("username")
Expand All @@ -24,7 +25,7 @@ func GetAllLogs(c *gin.Context) {
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)
logs, total, err := model.GetAllLogs(logType, logFilter, startTimestamp, endTimestamp, modelName, username, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), channel, group, requestId, upstreamRequestId)
if err != nil {
common.ApiError(c, err)
return
Expand Down Expand Up @@ -68,14 +69,15 @@ func GetUserLogs(c *gin.Context) {
pageInfo := common.GetPageQuery(c)
userId := c.GetInt("id")
logType, _ := strconv.Atoi(c.Query("type"))
logFilter := c.Query("log_filter")
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)
logs, total, err := model.GetUserLogs(userId, logType, logFilter, startTimestamp, endTimestamp, modelName, tokenName, pageInfo.GetStartIdx(), pageInfo.GetPageSize(), group, requestId, upstreamRequestId)
if err != nil {
common.ApiError(c, err)
return
Expand Down Expand Up @@ -155,14 +157,15 @@ func GetLogByKey(c *gin.Context) {

func GetLogsStat(c *gin.Context) {
logType, _ := strconv.Atoi(c.Query("type"))
logFilter := c.Query("log_filter")
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)
stat, err := model.SumUsedQuota(logType, logFilter, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
if err != nil {
common.ApiError(c, err)
return
Expand All @@ -183,13 +186,14 @@ func GetLogsStat(c *gin.Context) {
func GetLogsSelfStat(c *gin.Context) {
username := c.GetString("username")
logType, _ := strconv.Atoi(c.Query("type"))
logFilter := c.Query("log_filter")
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)
quotaNum, err := model.SumUsedQuota(logType, logFilter, startTimestamp, endTimestamp, modelName, username, tokenName, channel, group)
if err != nil {
common.ApiError(c, err)
return
Expand Down
2 changes: 1 addition & 1 deletion controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func processChannelError(c *gin.Context, channelError types.ChannelError, err *t
other["channel_name"] = c.GetString("channel_name")
other["channel_type"] = c.GetInt("channel_type")
adminInfo := make(map[string]interface{})
adminInfo["use_channel"] = c.GetStringSlice("use_channel")
service.AppendRetryLogInfo(c, adminInfo, other)
isMultiKey := common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey)
if isMultiKey {
adminInfo["is_multi_key"] = true
Expand Down
49 changes: 34 additions & 15 deletions model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ const (
LogTypeLogin = 7
)

const LogFilterRetry = "retry"

func applyLogTypeFilter(tx *gorm.DB, logType int) *gorm.DB {
if logType == LogTypeUnknown {
return tx
}
return tx.Where("logs.type = ?", logType)
}

func applyRetryLogFilter(tx *gorm.DB) *gorm.DB {
retryPatterns := []string{
`%"retry_log":true%`,
`%"empty_retry":true%`,
}
return tx.Where("(logs.other LIKE ? OR logs.other LIKE ?)", retryPatterns[0], retryPatterns[1])
Comment on lines +80 to +85

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🔵 Trivial

Plan for a searchable retry flag.

logs.other is an unindexed text field here, and the new retry filter uses leading-wildcard LIKE predicates on it in both list and stat queries. If this filter is expected to see real usage at scale, consider materializing the retry marker into a dedicated column or indexed/generated expression instead of scanning JSON blobs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@model/log.go` around lines 80 - 85, The retry filter in applyRetryLogFilter
is scanning the unindexed logs.other JSON blob with leading-wildcard LIKE
predicates, which will not scale for list and stat queries. Update the retry
marker handling in applyRetryLogFilter and the related query paths to use a
dedicated indexed column or a generated/indexed expression for the retry flag,
and keep the filter logic pointed at that searchable field instead of
logs.other.

}

func applyLogFilter(tx *gorm.DB, filter string) *gorm.DB {
switch filter {
case LogFilterRetry:
return applyRetryLogFilter(tx)
default:
return tx
}
}

func userVisibleAuditInfo(adminInfoValue interface{}) map[string]interface{} {
if !common.LogRequestContentEnabled && !common.LogResponseContentEnabled {
return nil
Expand Down Expand Up @@ -464,13 +490,8 @@ func RecordTaskBillingLog(params RecordTaskBillingLogParams) {
}
}

func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int, channel int, group string, requestId string, upstreamRequestId string) (logs []*Log, total int64, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = LOG_DB
} else {
tx = LOG_DB.Where("logs.type = ?", logType)
}
func GetAllLogs(logType int, logFilter string, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int, channel int, group string, requestId string, upstreamRequestId string) (logs []*Log, total int64, err error) {
tx := applyLogFilter(applyLogTypeFilter(LOG_DB, logType), logFilter)

if tx, err = applyExplicitLogTextFilter(tx, "logs.model_name", modelName); err != nil {
return nil, 0, err
Expand Down Expand Up @@ -554,13 +575,8 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName

const logSearchCountLimit = 10000

func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int, group string, requestId string, upstreamRequestId string) (logs []*Log, total int64, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = LOG_DB.Where("logs.user_id = ?", userId)
} else {
tx = LOG_DB.Where("logs.user_id = ? and logs.type = ?", userId, logType)
}
func GetUserLogs(userId int, logType int, logFilter string, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int, group string, requestId string, upstreamRequestId string) (logs []*Log, total int64, err error) {
tx := applyLogFilter(applyLogTypeFilter(LOG_DB.Where("logs.user_id = ?", userId), logType), logFilter)

if tx, err = applyExplicitLogTextFilter(tx, "logs.model_name", modelName); err != nil {
return nil, 0, err
Expand Down Expand Up @@ -635,12 +651,15 @@ type Stat struct {
Tpm int `json:"tpm"`
}

func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int, group string) (stat Stat, err error) {
func SumUsedQuota(logType int, logFilter string, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int, group string) (stat Stat, err error) {
tx := LOG_DB.Table("logs").Select("sum(quota) quota")

// 为rpm和tpm创建单独的查询
rpmTpmQuery := LOG_DB.Table("logs").Select("count(*) rpm, sum(prompt_tokens) + sum(completion_tokens) tpm")

tx = applyLogFilter(tx, logFilter)
rpmTpmQuery = applyLogFilter(rpmTpmQuery, logFilter)

if tx, err = applyExplicitLogTextFilter(tx, "username", username); err != nil {
return stat, err
}
Expand Down
111 changes: 111 additions & 0 deletions model/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"testing"
"time"

"github.com/MAX-API-Next/MAX-API/common"
"github.com/stretchr/testify/require"
Expand All @@ -19,6 +20,116 @@ func withLogAuditSettings(t *testing.T, requestEnabled bool, responseEnabled boo
})
}

func TestGetAllLogsRetryFilter(t *testing.T) {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
t.Cleanup(func() {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
})

logs := createRetryFilterLogs(t)

got, total, err := GetAllLogs(LogTypeUnknown, LogFilterRetry, 0, 0, "", "", "", 0, 10, 0, "", "", "")
require.NoError(t, err)
require.EqualValues(t, 2, total)
require.Len(t, got, 2)
require.ElementsMatch(t, []int{logs[0].Id, logs[1].Id}, []int{got[0].Id, got[1].Id})
}

func TestGetUserLogsRetryFilter(t *testing.T) {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
t.Cleanup(func() {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
})

logs := createRetryFilterLogs(t)

got, total, err := GetUserLogs(1, LogTypeUnknown, LogFilterRetry, 0, 0, "", "", 0, 10, "", "", "")
require.NoError(t, err)
require.EqualValues(t, 2, total)
require.Len(t, got, 2)
require.ElementsMatch(t, []int{logs[0].Id, logs[1].Id}, []int{got[0].LogId, got[1].LogId})
}

func TestSumUsedQuotaRetryFilter(t *testing.T) {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
t.Cleanup(func() {
require.NoError(t, LOG_DB.Where("1 = 1").Delete(&Log{}).Error)
})

createRetryFilterLogs(t)

stat, err := SumUsedQuota(LogTypeUnknown, LogFilterRetry, 0, 0, "", "", "", 0, "")
require.NoError(t, err)
require.Equal(t, 300, stat.Quota)
require.Equal(t, 2, stat.Rpm)
require.Equal(t, 30, stat.Tpm)
}

func createRetryFilterLogs(t *testing.T) []Log {
t.Helper()

logs := []Log{
{
UserId: 1,
CreatedAt: time.Now().Unix() - 10,
Type: LogTypeConsume,
Quota: 100,
PromptTokens: 5,
CompletionTokens: 10,
Other: common.MapToJsonStr(map[string]interface{}{
"retry_log": true,
"admin_info": map[string]interface{}{
"use_channel": []string{"1", "2"},
},
}),
},
{
UserId: 1,
CreatedAt: time.Now().Unix() - 20,
Type: LogTypeConsume,
Quota: 200,
PromptTokens: 7,
CompletionTokens: 8,
Other: common.MapToJsonStr(map[string]interface{}{
"empty_retry": true,
"admin_info": map[string]interface{}{
"use_channel": []string{"3"},
},
}),
},
{
UserId: 1,
CreatedAt: time.Now().Unix() - 30,
Type: LogTypeConsume,
Quota: 400,
PromptTokens: 11,
CompletionTokens: 13,
Other: common.MapToJsonStr(map[string]interface{}{
"admin_info": map[string]interface{}{
"use_channel": []string{"4"},
},
"empty_output_types": []string{"message", "function_call"},
}),
},
{
UserId: 2,
CreatedAt: time.Now().Unix() - 40,
Type: LogTypeConsume,
Quota: 800,
PromptTokens: 17,
CompletionTokens: 19,
Other: common.MapToJsonStr(map[string]interface{}{
"admin_info": map[string]interface{}{
"use_channel": []string{"5"},
},
}),
},
}
require.NoError(t, LOG_DB.Create(&logs).Error)

return logs
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func TestFormatUserLogDetailExposesOnlyUserAuditContent(t *testing.T) {
withLogAuditSettings(t, true, true)

Expand Down
13 changes: 12 additions & 1 deletion service/log_info_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func appendRequestPath(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, other
}
}

func AppendRetryLogInfo(ctx *gin.Context, adminInfo map[string]interface{}, other map[string]interface{}) {
if ctx == nil || adminInfo == nil || other == nil {
return
}
useChannel := ctx.GetStringSlice("use_channel")
adminInfo["use_channel"] = useChannel
if len(useChannel) > 1 {
other["retry_log"] = true
}
}

func GenerateTextOtherInfo(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, modelRatio, groupRatio, completionRatio float64,
cacheTokens int, cacheRatio float64, modelPrice float64, userGroupRatio float64) map[string]interface{} {
other := make(map[string]interface{})
Expand All @@ -58,7 +69,7 @@ func GenerateTextOtherInfo(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, m
}

adminInfo := make(map[string]interface{})
adminInfo["use_channel"] = ctx.GetStringSlice("use_channel")
AppendRetryLogInfo(ctx, adminInfo, other)
isMultiKey := common.GetContextKeyBool(ctx, constant.ContextKeyChannelIsMultiKey)
if isMultiKey {
adminInfo["is_multi_key"] = true
Expand Down
50 changes: 50 additions & 0 deletions service/log_info_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package service

import (
"net/http/httptest"
"testing"
"time"

relaycommon "github.com/MAX-API-Next/MAX-API/relay/common"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)

func TestGenerateTextOtherInfoMarksRetryLog(t *testing.T) {
gin.SetMode(gin.TestMode)
rec := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(rec)
ctx.Set("use_channel", []string{"1", "2"})

now := time.Now()
info := &relaycommon.RelayInfo{
StartTime: now,
FirstResponseTime: now,
ChannelMeta: &relaycommon.ChannelMeta{},
}

other := GenerateTextOtherInfo(ctx, info, 1, 1, 1, 0, 0, 0, 1)

require.Equal(t, true, other["retry_log"])
adminInfo, ok := other["admin_info"].(map[string]interface{})
require.True(t, ok)
require.Equal(t, []string{"1", "2"}, adminInfo["use_channel"])
}

func TestGenerateTextOtherInfoDoesNotMarkSingleChannelAsRetry(t *testing.T) {
gin.SetMode(gin.TestMode)
rec := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(rec)
ctx.Set("use_channel", []string{"1"})

now := time.Now()
info := &relaycommon.RelayInfo{
StartTime: now,
FirstResponseTime: now,
ChannelMeta: &relaycommon.ChannelMeta{},
}

other := GenerateTextOtherInfo(ctx, info, 1, 1, 1, 0, 0, 0, 1)

require.NotContains(t, other, "retry_log")
}
Loading
Loading