Init customer

This commit is contained in:
‘Liu
2024-01-10 14:19:33 +08:00
parent d062bc60e4
commit 3d9b81c706
11 changed files with 92 additions and 12 deletions

View File

@@ -71,7 +71,26 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
common.LogError(ctx, "failed to record log: "+err.Error())
}
}
func GetLogsByKey(logType int, startTimestamp int64, endTimestamp int64, key string, startIdx int, num int) (logs []*Log, err error) {
var tx *gorm.DB
token, err := GetNameByToken(key)
if logType == LogTypeUnknown {
tx = DB.Debug()
} else {
tx = DB.Debug().Where("type = ?", logType)
}
if token != nil {
tx = tx.Where("token_name = ?", token.Name)
}
if startTimestamp != 0 {
tx = tx.Where("created_at >= ?", startTimestamp)
}
if endTimestamp != 0 {
tx = tx.Where("created_at <= ?", endTimestamp)
}
err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
return logs, err
}
func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int, channel int) (logs []*Log, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {

View File

@@ -3,8 +3,9 @@ package model
import (
"errors"
"fmt"
"gorm.io/gorm"
"one-api/common"
"gorm.io/gorm"
)
type Token struct {
@@ -32,7 +33,15 @@ func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
err = DB.Where("user_id = ?", userId).Where("name LIKE ?", keyword+"%").Find(&tokens).Error
return tokens, err
}
func GetNameByToken(token string) (*Token, error) {
if token == "" {
return nil, errors.New("token为空")
}
token_name := Token{Key: token}
var err error = nil
err = DB.First(&token_name, "`key` = ?", token).Error
return &token_name, err
}
func ValidateUserToken(key string) (token *Token, err error) {
if key == "" {
return nil, errors.New("未提供令牌")