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
8a51ae30
authored
Aug 11, 2024
by
OswinWu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: log分页问题
parent
fda2fd2e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
10 deletions
+23
-10
controller/log.go
+11
-6
model/log.go
+12
-4
No files found.
controller/log.go
View file @
8a51ae30
package
controller
package
controller
import
(
import
(
"github.com/gin-gonic/gin"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strconv"
"strconv"
"github.com/gin-gonic/gin"
)
)
func
GetAllLogs
(
c
*
gin
.
Context
)
{
func
GetAllLogs
(
c
*
gin
.
Context
)
{
p
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"p"
))
p
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"p"
))
pageSize
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"page_size"
))
pageSize
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"page_size"
))
if
p
<
0
{
if
p
<
1
{
p
=
0
p
=
1
}
}
if
pageSize
<
0
{
if
pageSize
<
0
{
pageSize
=
common
.
ItemsPerPage
pageSize
=
common
.
ItemsPerPage
...
@@ -24,7 +25,7 @@ func GetAllLogs(c *gin.Context) {
...
@@ -24,7 +25,7 @@ func GetAllLogs(c *gin.Context) {
tokenName
:=
c
.
Query
(
"token_name"
)
tokenName
:=
c
.
Query
(
"token_name"
)
modelName
:=
c
.
Query
(
"model_name"
)
modelName
:=
c
.
Query
(
"model_name"
)
channel
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"channel"
))
channel
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"channel"
))
logs
,
err
:=
model
.
GetAllLogs
(
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
username
,
tokenName
,
p
*
pageSize
,
pageSize
,
channel
)
logs
,
total
,
err
:=
model
.
GetAllLogs
(
logType
,
startTimestamp
,
endTimestamp
,
modelName
,
username
,
tokenName
,
(
p
-
1
)
*
pageSize
,
pageSize
,
channel
)
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -35,9 +36,13 @@ func GetAllLogs(c *gin.Context) {
...
@@ -35,9 +36,13 @@ func GetAllLogs(c *gin.Context) {
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"success"
:
true
,
"message"
:
""
,
"message"
:
""
,
"data"
:
logs
,
"data"
:
map
[
string
]
any
{
"items"
:
logs
,
"total"
:
total
,
"page"
:
p
,
"page_size"
:
pageSize
,
},
})
})
return
}
}
func
GetUserLogs
(
c
*
gin
.
Context
)
{
func
GetUserLogs
(
c
*
gin
.
Context
)
{
...
...
model/log.go
View file @
8a51ae30
...
@@ -3,11 +3,12 @@ package model
...
@@ -3,11 +3,12 @@ package model
import
(
import
(
"context"
"context"
"fmt"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"gorm.io/gorm"
"one-api/common"
"one-api/common"
"strings"
"strings"
"time"
"time"
"github.com/bytedance/gopkg/util/gopool"
"gorm.io/gorm"
)
)
type
Log
struct
{
type
Log
struct
{
...
@@ -95,7 +96,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
...
@@ -95,7 +96,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
}
}
}
}
func
GetAllLogs
(
logType
int
,
startTimestamp
int64
,
endTimestamp
int64
,
modelName
string
,
username
string
,
tokenName
string
,
startIdx
int
,
num
int
,
channel
int
)
(
logs
[]
*
Log
,
err
error
)
{
func
GetAllLogs
(
logType
int
,
startTimestamp
int64
,
endTimestamp
int64
,
modelName
string
,
username
string
,
tokenName
string
,
startIdx
int
,
num
int
,
channel
int
)
(
logs
[]
*
Log
,
total
int64
,
err
error
)
{
var
tx
*
gorm
.
DB
var
tx
*
gorm
.
DB
if
logType
==
LogTypeUnknown
{
if
logType
==
LogTypeUnknown
{
tx
=
DB
tx
=
DB
...
@@ -120,8 +121,15 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
...
@@ -120,8 +121,15 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
if
channel
!=
0
{
if
channel
!=
0
{
tx
=
tx
.
Where
(
"channel_id = ?"
,
channel
)
tx
=
tx
.
Where
(
"channel_id = ?"
,
channel
)
}
}
err
=
tx
.
Model
(
&
Log
{})
.
Count
(
&
total
)
.
Error
if
err
!=
nil
{
return
nil
,
0
,
err
}
err
=
tx
.
Order
(
"id desc"
)
.
Limit
(
num
)
.
Offset
(
startIdx
)
.
Find
(
&
logs
)
.
Error
err
=
tx
.
Order
(
"id desc"
)
.
Limit
(
num
)
.
Offset
(
startIdx
)
.
Find
(
&
logs
)
.
Error
return
logs
,
err
if
err
!=
nil
{
return
nil
,
0
,
err
}
return
logs
,
total
,
err
}
}
func
GetUserLogs
(
userId
int
,
logType
int
,
startTimestamp
int64
,
endTimestamp
int64
,
modelName
string
,
tokenName
string
,
startIdx
int
,
num
int
)
(
logs
[]
*
Log
,
err
error
)
{
func
GetUserLogs
(
userId
int
,
logType
int
,
startTimestamp
int64
,
endTimestamp
int64
,
modelName
string
,
tokenName
string
,
startIdx
int
,
num
int
)
(
logs
[]
*
Log
,
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