🔖 chore: logs list displays channel name.

This commit is contained in:
MartialBE 2024-05-15 15:16:43 +08:00
parent 5ae57435ba
commit be874c330f
No known key found for this signature in database
GPG Key ID: 27C0267EC84B0A5C
4 changed files with 19 additions and 15 deletions

View File

@ -20,8 +20,10 @@ type Log struct {
Quota int `json:"quota" gorm:"default:0"`
PromptTokens int `json:"prompt_tokens" gorm:"default:0"`
CompletionTokens int `json:"completion_tokens" gorm:"default:0"`
ChannelId int `json:"channel" gorm:"index"`
ChannelId int `json:"channel_id" gorm:"index"`
RequestTime int `json:"request_time" gorm:"default:0"`
Channel *Channel `json:"channel" gorm:"foreignKey:Id;references:ChannelId"`
}
const (
@ -82,7 +84,7 @@ type LogsListParams struct {
ModelName string `form:"model_name"`
Username string `form:"username"`
TokenName string `form:"token_name"`
Channel int `form:"channel"`
ChannelId int `form:"channel_id"`
}
var allowedLogsOrderFields = map[string]bool{
@ -98,10 +100,12 @@ func GetLogsList(params *LogsListParams) (*DataResult[Log], error) {
var tx *gorm.DB
var logs []*Log
if params.LogType == LogTypeUnknown {
tx = DB
} else {
tx = DB.Where("type = ?", params.LogType)
tx = DB.Preload("Channel", func(db *gorm.DB) *gorm.DB {
return db.Select("id, name")
})
if params.LogType != LogTypeUnknown {
tx = tx.Where("type = ?", params.LogType)
}
if params.ModelName != "" {
tx = tx.Where("model_name = ?", params.ModelName)
@ -118,8 +122,8 @@ func GetLogsList(params *LogsListParams) (*DataResult[Log], error) {
if params.EndTimestamp != 0 {
tx = tx.Where("created_at <= ?", params.EndTimestamp)
}
if params.Channel != 0 {
tx = tx.Where("channel_id = ?", params.Channel)
if params.ChannelId != 0 {
tx = tx.Where("channel_id = ?", params.ChannelId)
}
return PaginateAndOrder[Log](tx, &params.PaginationParams, &logs, allowedLogsOrderFields)

View File

@ -70,7 +70,7 @@ export default function LogTableRow({ item, userIsAdmin }) {
<TableRow tabIndex={item.id}>
<TableCell>{timestamp2string(item.created_at)}</TableCell>
{userIsAdmin && <TableCell>{item.channel || ''}</TableCell>}
{userIsAdmin && <TableCell>{(item.channel_id || '') + '(' + (item.channel?.name || '未知') + ')'}</TableCell>}
{userIsAdmin && (
<TableCell>
<Label color="default" variant="outlined">

View File

@ -133,15 +133,15 @@ export default function TableToolBar({ filterName, handleFilterName, userIsAdmin
{userIsAdmin && (
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={{ xs: 3, sm: 2, md: 4 }} padding={'24px'}>
<FormControl>
<InputLabel htmlFor="channel-channel-label">渠道ID</InputLabel>
<InputLabel htmlFor="channel-channel_id-label">渠道ID</InputLabel>
<OutlinedInput
id="channel"
name="channel"
id="channel_id"
name="channel_id"
sx={{
minWidth: '100%'
}}
label="渠道ID"
value={filterName.channel}
value={filterName.channel_id}
onChange={handleFilterName}
placeholder="渠道ID"
startAdornment={

View File

@ -29,7 +29,7 @@ export default function Log() {
start_timestamp: 0,
end_timestamp: dayjs().unix() + 3600,
log_type: 0,
channel: ''
channel_id: ''
};
const [page, setPage] = useState(0);
@ -82,7 +82,7 @@ export default function Log() {
const url = userIsAdmin ? '/api/log/' : '/api/log/self/';
if (!userIsAdmin) {
delete keyword.username;
delete keyword.channel;
delete keyword.channel_id;
}
const res = await API.get(url, {