Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
b9bc6f0e
authored
May 22, 2026
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "fix: correct usage logs filtering (#4883)"
This reverts commit
554defe4
.
parent
f2c7647e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
31 deletions
+35
-31
model/log.go
+35
-30
web/default/src/features/usage-logs/components/usage-logs-table.tsx
+0
-1
No files found.
model/log.go
View file @
b9bc6f0e
...
...
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"
"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
=
applyLogContainsFilter
(
tx
,
"logs.model_name"
,
modelName
)
tx
=
applyLogContainsFilter
(
tx
,
"logs.username"
,
username
)
tx
=
applyLogContainsFilter
(
tx
,
"logs.token_name"
,
tokenName
)
if
modelName
!=
""
{
tx
=
tx
.
Where
(
"logs.model_name like ?"
,
modelName
)
}
if
username
!=
""
{
tx
=
tx
.
Where
(
"logs.username = ?"
,
username
)
}
if
tokenName
!=
""
{
tx
=
tx
.
Where
(
"logs.token_name = ?"
,
tokenName
)
}
if
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
=
applyLogContainsFilter
(
tx
,
"logs.model_name"
,
modelName
)
tx
=
applyLogContainsFilter
(
tx
,
"logs.token_name"
,
tokenName
)
if
modelName
!=
""
{
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
!=
""
{
tx
=
tx
.
Where
(
"logs.request_id = ?"
,
requestId
)
}
...
...
@@ -430,42 +443,34 @@ type Stat struct {
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
)
{
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
=
applyLogContainsFilter
(
tx
,
"username"
,
username
)
rpmTpmQuery
=
applyLogContainsFilter
(
rpmTpmQuery
,
"username"
,
username
)
tx
=
applyLogContainsFilter
(
tx
,
"token_name"
,
tokenName
)
rpmTpmQuery
=
applyLogContainsFilter
(
rpmTpmQuery
,
"token_name"
,
tokenName
)
if
username
!=
""
{
tx
=
tx
.
Where
(
"username = ?"
,
username
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
"username = ?"
,
username
)
}
if
tokenName
!=
""
{
tx
=
tx
.
Where
(
"token_name = ?"
,
tokenName
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
"token_name = ?"
,
tokenName
)
}
if
startTimestamp
!=
0
{
tx
=
tx
.
Where
(
"created_at >= ?"
,
startTimestamp
)
}
if
endTimestamp
!=
0
{
tx
=
tx
.
Where
(
"created_at <= ?"
,
endTimestamp
)
}
tx
=
applyLogContainsFilter
(
tx
,
"model_name"
,
modelName
)
rpmTpmQuery
=
applyLogContainsFilter
(
rpmTpmQuery
,
"model_name"
,
modelName
)
if
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
{
tx
=
tx
.
Where
(
"channel_id = ?"
,
channel
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
"channel_id = ?"
,
channel
)
...
...
web/default/src/features/usage-logs/components/usage-logs-table.tsx
View file @
b9bc6f0e
...
...
@@ -150,7 +150,6 @@ export function UsageLogsTable({ logCategory }: UsageLogsTableProps) {
getFacetedRowModel
:
getFacetedRowModel
(),
getFacetedUniqueValues
:
getFacetedUniqueValues
(),
manualPagination
:
true
,
manualFiltering
:
true
,
pageCount
:
Math
.
ceil
((
data
?.
total
||
0
)
/
pagination
.
pageSize
),
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment