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
Commit
2a99fae1
authored
Oct 14, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix request count not updated correctly when using batch update
parent
1a493289
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
7 deletions
+28
-7
model/user.go
+20
-1
model/utils.go
+8
-6
No files found.
model/user.go
View file @
2a99fae1
...
...
@@ -309,7 +309,8 @@ func GetRootUserEmail() (email string) {
func
UpdateUserUsedQuotaAndRequestCount
(
id
int
,
quota
int
)
{
if
common
.
BatchUpdateEnabled
{
addNewRecord
(
BatchUpdateTypeUsedQuotaAndRequestCount
,
id
,
quota
)
addNewRecord
(
BatchUpdateTypeUsedQuota
,
id
,
quota
)
addNewRecord
(
BatchUpdateTypeRequestCount
,
id
,
1
)
return
}
updateUserUsedQuotaAndRequestCount
(
id
,
quota
,
1
)
...
...
@@ -327,6 +328,24 @@ func updateUserUsedQuotaAndRequestCount(id int, quota int, count int) {
}
}
func
updateUserUsedQuota
(
id
int
,
quota
int
)
{
err
:=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
map
[
string
]
interface
{}{
"used_quota"
:
gorm
.
Expr
(
"used_quota + ?"
,
quota
),
},
)
.
Error
if
err
!=
nil
{
common
.
SysError
(
"failed to update user used quota: "
+
err
.
Error
())
}
}
func
updateUserRequestCount
(
id
int
,
count
int
)
{
err
:=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"request_count"
,
gorm
.
Expr
(
"request_count + ?"
,
count
))
.
Error
if
err
!=
nil
{
common
.
SysError
(
"failed to update user request count: "
+
err
.
Error
())
}
}
func
GetUsernameById
(
id
int
)
(
username
string
)
{
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
"username"
)
.
Find
(
&
username
)
return
username
...
...
model/utils.go
View file @
2a99fae1
...
...
@@ -6,13 +6,13 @@ import (
"time"
)
const
BatchUpdateTypeCount
=
4
// if you add a new type, you need to add a new map and a new lock
const
(
BatchUpdateTypeUserQuota
=
iota
BatchUpdateTypeTokenQuota
BatchUpdateTypeUsedQuota
AndRequestCount
BatchUpdateTypeUsedQuota
BatchUpdateTypeChannelUsedQuota
BatchUpdateTypeRequestCount
BatchUpdateTypeCount
// if you add a new type, you need to add a new map and a new lock
)
var
batchUpdateStores
[]
map
[
int
]
int
...
...
@@ -51,7 +51,7 @@ func batchUpdate() {
store
:=
batchUpdateStores
[
i
]
batchUpdateStores
[
i
]
=
make
(
map
[
int
]
int
)
batchUpdateLocks
[
i
]
.
Unlock
()
// TODO: maybe we can combine updates with same key?
for
key
,
value
:=
range
store
{
switch
i
{
case
BatchUpdateTypeUserQuota
:
...
...
@@ -64,8 +64,10 @@ func batchUpdate() {
if
err
!=
nil
{
common
.
SysError
(
"failed to batch update token quota: "
+
err
.
Error
())
}
case
BatchUpdateTypeUsedQuotaAndRequestCount
:
updateUserUsedQuotaAndRequestCount
(
key
,
value
,
1
)
// TODO: count is incorrect
case
BatchUpdateTypeUsedQuota
:
updateUserUsedQuota
(
key
,
value
)
case
BatchUpdateTypeRequestCount
:
updateUserRequestCount
(
key
,
value
)
case
BatchUpdateTypeChannelUsedQuota
:
updateChannelUsedQuota
(
key
,
value
)
}
...
...
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