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
8979961a
authored
Jun 16, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: record channel's used quota (close #137)
parent
0b3efe57
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
0 deletions
+11
-0
controller/relay.go
+2
-0
model/channel.go
+9
-0
No files found.
controller/relay.go
View file @
8979961a
...
@@ -262,6 +262,8 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -262,6 +262,8 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
userId
:=
c
.
GetInt
(
"id"
)
userId
:=
c
.
GetInt
(
"id"
)
model
.
RecordLog
(
userId
,
model
.
LogTypeConsume
,
fmt
.
Sprintf
(
"使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f)"
,
textRequest
.
Model
,
quota
,
modelRatio
,
groupRatio
))
model
.
RecordLog
(
userId
,
model
.
LogTypeConsume
,
fmt
.
Sprintf
(
"使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f)"
,
textRequest
.
Model
,
quota
,
modelRatio
,
groupRatio
))
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
}
}
}()
}()
...
...
model/channel.go
View file @
8979961a
package
model
package
model
import
(
import
(
"gorm.io/gorm"
"one-api/common"
"one-api/common"
)
)
...
@@ -20,6 +21,7 @@ type Channel struct {
...
@@ -20,6 +21,7 @@ type Channel struct {
BalanceUpdatedTime
int64
`json:"balance_updated_time" gorm:"bigint"`
BalanceUpdatedTime
int64
`json:"balance_updated_time" gorm:"bigint"`
Models
string
`json:"models"`
Models
string
`json:"models"`
Group
string
`json:"group" gorm:"type:varchar(32);default:'default'"`
Group
string
`json:"group" gorm:"type:varchar(32);default:'default'"`
UsedQuota
int64
`json:"used_quota" gorm:"bigint;default:0"`
}
}
func
GetAllChannels
(
startIdx
int
,
num
int
,
selectAll
bool
)
([]
*
Channel
,
error
)
{
func
GetAllChannels
(
startIdx
int
,
num
int
,
selectAll
bool
)
([]
*
Channel
,
error
)
{
...
@@ -136,3 +138,10 @@ func UpdateChannelStatusById(id int, status int) {
...
@@ -136,3 +138,10 @@ func UpdateChannelStatusById(id int, status int) {
common
.
SysError
(
"failed to update channel status: "
+
err
.
Error
())
common
.
SysError
(
"failed to update channel status: "
+
err
.
Error
())
}
}
}
}
func
UpdateChannelUsedQuota
(
id
int
,
quota
int
)
{
err
:=
DB
.
Model
(
&
Channel
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"used_quota"
,
gorm
.
Expr
(
"used_quota + ?"
,
quota
))
.
Error
if
err
!=
nil
{
common
.
SysError
(
"failed to update channel used quota: "
+
err
.
Error
())
}
}
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