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
39328d65
authored
Sep 17, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: able to delete logs now (close #486)
parent
3f9e723c
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
18 deletions
+85
-18
controller/log.go
+42
-10
model/log.go
+5
-0
router/api-router.go
+1
-0
web/src/components/OperationSetting.js
+37
-8
No files found.
controller/log.go
View file @
39328d65
...
...
@@ -2,6 +2,7 @@ package controller
import
(
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/model"
"strconv"
...
...
@@ -20,17 +21,18 @@ func GetAllLogs(c *gin.Context) {
modelName
:=
c
.
Query
(
"model_name"
)
logs
,
err
:=
model
.
GetAllLogs
(
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
username
,
tokenName
,
p
*
common
.
ItemsPerPage
,
common
.
ItemsPerPage
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
return
}
func
GetUserLogs
(
c
*
gin
.
Context
)
{
...
...
@@ -46,34 +48,36 @@ func GetUserLogs(c *gin.Context) {
modelName
:=
c
.
Query
(
"model_name"
)
logs
,
err
:=
model
.
GetUserLogs
(
userId
,
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
tokenName
,
p
*
common
.
ItemsPerPage
,
common
.
ItemsPerPage
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
return
}
func
SearchAllLogs
(
c
*
gin
.
Context
)
{
keyword
:=
c
.
Query
(
"keyword"
)
logs
,
err
:=
model
.
SearchAllLogs
(
keyword
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
return
}
func
SearchUserLogs
(
c
*
gin
.
Context
)
{
...
...
@@ -81,17 +85,18 @@ func SearchUserLogs(c *gin.Context) {
userId
:=
c
.
GetInt
(
"id"
)
logs
,
err
:=
model
.
SearchUserLogs
(
userId
,
keyword
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
return
}
func
GetLogsStat
(
c
*
gin
.
Context
)
{
...
...
@@ -103,7 +108,7 @@ func GetLogsStat(c *gin.Context) {
modelName
:=
c
.
Query
(
"model_name"
)
quotaNum
:=
model
.
SumUsedQuota
(
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
username
,
tokenName
)
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
gin
.
H
{
...
...
@@ -111,6 +116,7 @@ func GetLogsStat(c *gin.Context) {
//"token": tokenNum,
},
})
return
}
func
GetLogsSelfStat
(
c
*
gin
.
Context
)
{
...
...
@@ -122,7 +128,7 @@ func GetLogsSelfStat(c *gin.Context) {
modelName
:=
c
.
Query
(
"model_name"
)
quotaNum
:=
model
.
SumUsedQuota
(
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
username
,
tokenName
)
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
c
.
JSON
(
200
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
gin
.
H
{
...
...
@@ -130,4 +136,30 @@ func GetLogsSelfStat(c *gin.Context) {
//"token": tokenNum,
},
})
return
}
func
DeleteHistoryLogs
(
c
*
gin
.
Context
)
{
targetTimestamp
,
_
:=
strconv
.
ParseInt
(
c
.
Query
(
"target_timestamp"
),
10
,
64
)
if
targetTimestamp
==
0
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
"target timestamp is required"
,
})
return
}
count
,
err
:=
model
.
DeleteOldLog
(
targetTimestamp
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
count
,
})
return
}
model/log.go
View file @
39328d65
...
...
@@ -169,3 +169,8 @@ func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelNa
tx
.
Where
(
"type = ?"
,
LogTypeConsume
)
.
Scan
(
&
token
)
return
token
}
func
DeleteOldLog
(
targetTimestamp
int64
)
(
int64
,
error
)
{
result
:=
DB
.
Where
(
"created_at < ?"
,
targetTimestamp
)
.
Delete
(
&
Log
{})
return
result
.
RowsAffected
,
result
.
Error
}
router/api-router.go
View file @
39328d65
...
...
@@ -98,6 +98,7 @@ func SetApiRouter(router *gin.Engine) {
}
logRoute
:=
apiRouter
.
Group
(
"/log"
)
logRoute
.
GET
(
"/"
,
middleware
.
AdminAuth
(),
controller
.
GetAllLogs
)
logRoute
.
DELETE
(
"/"
,
middleware
.
AdminAuth
(),
controller
.
DeleteHistoryLogs
)
logRoute
.
GET
(
"/stat"
,
middleware
.
AdminAuth
(),
controller
.
GetLogsStat
)
logRoute
.
GET
(
"/self/stat"
,
middleware
.
UserAuth
(),
controller
.
GetLogsSelfStat
)
logRoute
.
GET
(
"/search"
,
middleware
.
AdminAuth
(),
controller
.
SearchAllLogs
)
...
...
web/src/components/OperationSetting.js
View file @
39328d65
import
React
,
{
useEffect
,
useState
}
from
'react'
;
import
{
Divider
,
Form
,
Grid
,
Header
}
from
'semantic-ui-react'
;
import
{
API
,
showError
,
verifyJSON
}
from
'../helpers'
;
import
{
API
,
showError
,
showSuccess
,
timestamp2string
,
verifyJSON
}
from
'../helpers'
;
const
OperationSetting
=
()
=>
{
let
now
=
new
Date
();
let
[
inputs
,
setInputs
]
=
useState
({
QuotaForNewUser
:
0
,
QuotaForInviter
:
0
,
...
...
@@ -20,10 +21,11 @@ const OperationSetting = () => {
DisplayInCurrencyEnabled
:
''
,
DisplayTokenStatEnabled
:
''
,
ApproximateTokenEnabled
:
''
,
RetryTimes
:
0
,
RetryTimes
:
0
});
const
[
originInputs
,
setOriginInputs
]
=
useState
({});
let
[
loading
,
setLoading
]
=
useState
(
false
);
let
[
historyTimestamp
,
setHistoryTimestamp
]
=
useState
(
timestamp2string
(
now
.
getTime
()
/
1000
-
30
*
24
*
3600
));
// a month ago
const
getOptions
=
async
()
=>
{
const
res
=
await
API
.
get
(
'/api/option/'
);
...
...
@@ -130,6 +132,17 @@ const OperationSetting = () => {
}
};
const
deleteHistoryLogs
=
async
()
=>
{
console
.
log
(
inputs
);
const
res
=
await
API
.
delete
(
`/api/log/?target_timestamp=
${
Date
.
parse
(
historyTimestamp
)
/
1000
}
`
);
const
{
success
,
message
,
data
}
=
res
.
data
;
if
(
success
)
{
showSuccess
(
`
${
data
}
条日志已清理!`
);
return
;
}
showError
(
'日志清理失败:'
+
message
);
};
return
(
<
Grid
columns
=
{
1
}
>
<
Grid
.
Column
>
...
...
@@ -180,12 +193,6 @@ const OperationSetting = () => {
<
/Form.Group
>
<
Form
.
Group
inline
>
<
Form
.
Checkbox
checked
=
{
inputs
.
LogConsumeEnabled
===
'true'
}
label
=
'启用额度消费日志记录'
name
=
'LogConsumeEnabled'
onChange
=
{
handleInputChange
}
/
>
<
Form
.
Checkbox
checked
=
{
inputs
.
DisplayInCurrencyEnabled
===
'true'
}
label
=
'以货币形式显示额度'
name
=
'DisplayInCurrencyEnabled'
...
...
@@ -209,6 +216,28 @@ const OperationSetting = () => {
}}
>
保存通用设置
<
/Form.Button
>
<
Divider
/>
<
Header
as
=
'h3'
>
日志设置
<
/Header
>
<
Form
.
Group
inline
>
<
Form
.
Checkbox
checked
=
{
inputs
.
LogConsumeEnabled
===
'true'
}
label
=
'启用额度消费日志记录'
name
=
'LogConsumeEnabled'
onChange
=
{
handleInputChange
}
/
>
<
/Form.Group
>
<
Form
.
Group
widths
=
{
4
}
>
<
Form
.
Input
label
=
'目标时间'
value
=
{
historyTimestamp
}
type
=
'datetime-local'
name
=
'history_timestamp'
onChange
=
{(
e
,
{
name
,
value
})
=>
{
setHistoryTimestamp
(
value
);
}}
/
>
<
/Form.Group
>
<
Form
.
Button
onClick
=
{()
=>
{
deleteHistoryLogs
().
then
();
}}
>
清理历史日志
<
/Form.Button
>
<
Divider
/>
<
Header
as
=
'h3'
>
监控设置
<
/Header
>
<
Form
.
Group
widths
=
{
3
}
>
...
...
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