Revert "fix: correct usage logs filtering (#4883)"
Some checks failed
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Has been cancelled
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Has been cancelled
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Has been cancelled
Release (Linux, macOS, Windows) / Linux Release (push) Has been cancelled
Release (Linux, macOS, Windows) / macOS Release (push) Has been cancelled
Release (Linux, macOS, Windows) / Windows Release (push) Has been cancelled
Some checks failed
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Has been cancelled
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Has been cancelled
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Has been cancelled
Release (Linux, macOS, Windows) / Linux Release (push) Has been cancelled
Release (Linux, macOS, Windows) / macOS Release (push) Has been cancelled
Release (Linux, macOS, Windows) / Windows Release (push) Has been cancelled
This reverts commit 554defe4f430ff327b8d4a2079240b791f8b2bc6.
This commit is contained in:
parent
f2c7647ecf
commit
b9bc6f0e21
65
model/log.go
65
model/log.go
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/QuantumNous/new-api/common"
|
"github.com/QuantumNous/new-api/common"
|
||||||
@ -309,9 +308,15 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
|
|||||||
tx = LOG_DB.Where("logs.type = ?", logType)
|
tx = LOG_DB.Where("logs.type = ?", logType)
|
||||||
}
|
}
|
||||||
|
|
||||||
tx = applyLogContainsFilter(tx, "logs.model_name", modelName)
|
if modelName != "" {
|
||||||
tx = applyLogContainsFilter(tx, "logs.username", username)
|
tx = tx.Where("logs.model_name like ?", modelName)
|
||||||
tx = applyLogContainsFilter(tx, "logs.token_name", tokenName)
|
}
|
||||||
|
if username != "" {
|
||||||
|
tx = tx.Where("logs.username = ?", username)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("logs.token_name = ?", tokenName)
|
||||||
|
}
|
||||||
if requestId != "" {
|
if requestId != "" {
|
||||||
tx = tx.Where("logs.request_id = ?", requestId)
|
tx = tx.Where("logs.request_id = ?", requestId)
|
||||||
}
|
}
|
||||||
@ -392,8 +397,16 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
|
|||||||
tx = LOG_DB.Where("logs.user_id = ? and logs.type = ?", userId, logType)
|
tx = LOG_DB.Where("logs.user_id = ? and logs.type = ?", userId, logType)
|
||||||
}
|
}
|
||||||
|
|
||||||
tx = applyLogContainsFilter(tx, "logs.model_name", modelName)
|
if modelName != "" {
|
||||||
tx = applyLogContainsFilter(tx, "logs.token_name", tokenName)
|
modelNamePattern, err := sanitizeLikePattern(modelName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
tx = tx.Where("logs.model_name LIKE ? ESCAPE '!'", modelNamePattern)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("logs.token_name = ?", tokenName)
|
||||||
|
}
|
||||||
if requestId != "" {
|
if requestId != "" {
|
||||||
tx = tx.Where("logs.request_id = ?", requestId)
|
tx = tx.Where("logs.request_id = ?", requestId)
|
||||||
}
|
}
|
||||||
@ -430,42 +443,34 @@ type Stat struct {
|
|||||||
Tpm int `json:"tpm"`
|
Tpm int `json:"tpm"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func logContainsPattern(input string) (string, bool) {
|
|
||||||
input = strings.TrimSpace(input)
|
|
||||||
if input == "" {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
replacer := strings.NewReplacer("!", "!!", "%", "!%", "_", "!_")
|
|
||||||
return "%" + replacer.Replace(input) + "%", true
|
|
||||||
}
|
|
||||||
|
|
||||||
func applyLogContainsFilter(tx *gorm.DB, column string, value string) *gorm.DB {
|
|
||||||
pattern, ok := logContainsPattern(value)
|
|
||||||
if !ok {
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
return tx.Where(column+" LIKE ? ESCAPE '!'", pattern)
|
|
||||||
}
|
|
||||||
|
|
||||||
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, 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")
|
tx := LOG_DB.Table("logs").Select("sum(quota) quota")
|
||||||
|
|
||||||
// 为rpm和tpm创建单独的查询
|
// 为rpm和tpm创建单独的查询
|
||||||
rpmTpmQuery := LOG_DB.Table("logs").Select("count(*) rpm, sum(prompt_tokens) + sum(completion_tokens) tpm")
|
rpmTpmQuery := LOG_DB.Table("logs").Select("count(*) rpm, sum(prompt_tokens) + sum(completion_tokens) tpm")
|
||||||
|
|
||||||
tx = applyLogContainsFilter(tx, "username", username)
|
if username != "" {
|
||||||
rpmTpmQuery = applyLogContainsFilter(rpmTpmQuery, "username", username)
|
tx = tx.Where("username = ?", username)
|
||||||
tx = applyLogContainsFilter(tx, "token_name", tokenName)
|
rpmTpmQuery = rpmTpmQuery.Where("username = ?", username)
|
||||||
rpmTpmQuery = applyLogContainsFilter(rpmTpmQuery, "token_name", tokenName)
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("token_name = ?", tokenName)
|
||||||
|
rpmTpmQuery = rpmTpmQuery.Where("token_name = ?", tokenName)
|
||||||
|
}
|
||||||
if startTimestamp != 0 {
|
if startTimestamp != 0 {
|
||||||
tx = tx.Where("created_at >= ?", startTimestamp)
|
tx = tx.Where("created_at >= ?", startTimestamp)
|
||||||
}
|
}
|
||||||
if endTimestamp != 0 {
|
if endTimestamp != 0 {
|
||||||
tx = tx.Where("created_at <= ?", endTimestamp)
|
tx = tx.Where("created_at <= ?", endTimestamp)
|
||||||
}
|
}
|
||||||
tx = applyLogContainsFilter(tx, "model_name", modelName)
|
if modelName != "" {
|
||||||
rpmTpmQuery = applyLogContainsFilter(rpmTpmQuery, "model_name", modelName)
|
modelNamePattern, err := sanitizeLikePattern(modelName)
|
||||||
|
if err != nil {
|
||||||
|
return stat, err
|
||||||
|
}
|
||||||
|
tx = tx.Where("model_name LIKE ? ESCAPE '!'", modelNamePattern)
|
||||||
|
rpmTpmQuery = rpmTpmQuery.Where("model_name LIKE ? ESCAPE '!'", modelNamePattern)
|
||||||
|
}
|
||||||
if channel != 0 {
|
if channel != 0 {
|
||||||
tx = tx.Where("channel_id = ?", channel)
|
tx = tx.Where("channel_id = ?", channel)
|
||||||
rpmTpmQuery = rpmTpmQuery.Where("channel_id = ?", channel)
|
rpmTpmQuery = rpmTpmQuery.Where("channel_id = ?", channel)
|
||||||
|
|||||||
@ -150,7 +150,6 @@ export function UsageLogsTable({ logCategory }: UsageLogsTableProps) {
|
|||||||
getFacetedRowModel: getFacetedRowModel(),
|
getFacetedRowModel: getFacetedRowModel(),
|
||||||
getFacetedUniqueValues: getFacetedUniqueValues(),
|
getFacetedUniqueValues: getFacetedUniqueValues(),
|
||||||
manualPagination: true,
|
manualPagination: true,
|
||||||
manualFiltering: true,
|
|
||||||
pageCount: Math.ceil((data?.total || 0) / pagination.pageSize),
|
pageCount: Math.ceil((data?.total || 0) / pagination.pageSize),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user