feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
+43
-40
@@ -2,8 +2,6 @@ package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -152,21 +150,23 @@ func GetLogByTokenId(tokenId int) (logs []*Log, err error) {
|
||||
return logs, err
|
||||
}
|
||||
|
||||
func RecordLog(userId int, logType int, content string) {
|
||||
func RecordLog(userId int, logType int, content *common.Message) {
|
||||
if logType == LogTypeConsume && !common.LogConsumeEnabled {
|
||||
return
|
||||
}
|
||||
username, _ := GetUsernameById(userId, false)
|
||||
other := NewLogOther()
|
||||
log := &Log{
|
||||
UserId: userId,
|
||||
Username: username,
|
||||
CreatedAt: common.GetTimestamp(),
|
||||
Type: logType,
|
||||
Content: content,
|
||||
Content: other.SetContent([]*common.Message{content}),
|
||||
Other: other.JSONString(),
|
||||
}
|
||||
err := createLog(log)
|
||||
if err != nil {
|
||||
common.SysLog("failed to record log: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to record log: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,13 +202,13 @@ func RecordLogWithAdminInfo(userId int, logType int, content string, adminInfo *
|
||||
if adminInfo != nil || operation != nil {
|
||||
data, err := common.Marshal(AuditOther{AdminInfo: adminInfo, Op: operation})
|
||||
if err != nil {
|
||||
common.SysError("failed to encode log admin info: " + err.Error())
|
||||
common.SysError(common.LogText("failed to encode log admin info: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
log.Other = string(data)
|
||||
}
|
||||
if err := createLog(log); err != nil {
|
||||
common.SysLog("failed to record log: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to record log: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ func RecordOperationAuditLog(logUserId, actorRole int, content string, ip string
|
||||
RecordAuditLog(c, AuditLog{UserId: logUserId, Username: username, ActorRole: actorRole, Category: category, Action: action, Content: content, Ip: ip, Status: status, Success: success, Other: other})
|
||||
}
|
||||
|
||||
func RecordTopupLog(userId int, content string, callerIp string, paymentMethod string, callbackPaymentMethod string) {
|
||||
func RecordTopupLog(userId int, content *common.Message, callerIp string, paymentMethod string, callbackPaymentMethod string) {
|
||||
username, _ := GetUsernameById(userId, false)
|
||||
other := NewLogOther()
|
||||
other.MergeAdmin(map[string]any{
|
||||
@@ -270,19 +270,19 @@ func RecordTopupLog(userId int, content string, callerIp string, paymentMethod s
|
||||
Username: username,
|
||||
CreatedAt: common.GetTimestamp(),
|
||||
Type: LogTypeTopup,
|
||||
Content: content,
|
||||
Content: other.SetContent([]*common.Message{content}),
|
||||
Ip: callerIp,
|
||||
Other: other.JSONString(),
|
||||
}
|
||||
err := createLog(log)
|
||||
if err != nil {
|
||||
common.SysLog("failed to record topup log: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to record topup log: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func RecordErrorLog(c *gin.Context, userId int, channelId int, modelName string, tokenName string, content string, tokenId int, useTimeSeconds int,
|
||||
isStream bool, group string, other *LogOther) {
|
||||
logger.LogInfo(c, fmt.Sprintf("record error log: userId=%d, channelId=%d, modelName=%s, tokenName=%s, content=%s", userId, channelId, modelName, tokenName, common.LocalLogPreview(content)))
|
||||
logger.LogInfo(c, common.LogText("record error log: userId=%d, channelId=%d, modelName=%s, tokenName=%s, content=%s", userId, channelId, modelName, tokenName, common.LocalLogPreview(content)))
|
||||
username := c.GetString("username")
|
||||
requestId := c.GetString(common.RequestIdKey)
|
||||
upstreamRequestId := c.GetString(common.UpstreamRequestIdKey)
|
||||
@@ -324,35 +324,37 @@ func RecordErrorLog(c *gin.Context, userId int, channelId int, modelName string,
|
||||
normalizeErrorLogGroup(log)
|
||||
err := createLog(log)
|
||||
if err != nil {
|
||||
logger.LogError(c, "failed to record log: "+err.Error())
|
||||
logger.LogError(c, common.LogText("failed to record log: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
type RecordConsumeLogParams struct {
|
||||
ChannelId int `json:"channel_id"`
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
ModelName string `json:"model_name"`
|
||||
TokenName string `json:"token_name"`
|
||||
Quota int `json:"quota"`
|
||||
Content string `json:"content"`
|
||||
TokenId int `json:"token_id"`
|
||||
UseTimeSeconds int `json:"use_time_seconds"`
|
||||
IsStream bool `json:"is_stream"`
|
||||
Group string `json:"group"`
|
||||
Other *LogOther `json:"other"`
|
||||
ChannelId int `json:"channel_id"`
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
ModelName string `json:"model_name"`
|
||||
TokenName string `json:"token_name"`
|
||||
Quota int `json:"quota"`
|
||||
Content []*common.Message `json:"content"`
|
||||
TokenId int `json:"token_id"`
|
||||
UseTimeSeconds int `json:"use_time_seconds"`
|
||||
IsStream bool `json:"is_stream"`
|
||||
Group string `json:"group"`
|
||||
Other *LogOther `json:"other"`
|
||||
}
|
||||
|
||||
func RecordConsumeLog(c *gin.Context, userId int, params RecordConsumeLogParams) {
|
||||
if !common.LogConsumeEnabled {
|
||||
return
|
||||
}
|
||||
logger.LogInfo(c, fmt.Sprintf("record consume log: userId=%d, params=%s", userId, common.GetJsonString(params)))
|
||||
logger.LogInfo(c, common.LogText("record consume log: userId=%d, params=%s", userId, common.GetJsonString(params)))
|
||||
username := c.GetString("username")
|
||||
requestId := c.GetString(common.RequestIdKey)
|
||||
upstreamRequestId := c.GetString(common.UpstreamRequestIdKey)
|
||||
createdAt := common.GetTimestamp()
|
||||
otherStr := params.Other.JSONString()
|
||||
other := params.Other
|
||||
content := other.SetContent(params.Content)
|
||||
otherStr := other.JSONString()
|
||||
// 判断是否需要记录 IP
|
||||
needRecordIp := false
|
||||
if settingMap, err := GetUserSetting(userId, false); err == nil {
|
||||
@@ -365,7 +367,7 @@ func RecordConsumeLog(c *gin.Context, userId int, params RecordConsumeLogParams)
|
||||
Username: username,
|
||||
CreatedAt: createdAt,
|
||||
Type: LogTypeConsume,
|
||||
Content: params.Content,
|
||||
Content: content,
|
||||
PromptTokens: params.PromptTokens,
|
||||
CompletionTokens: params.CompletionTokens,
|
||||
TokenName: params.TokenName,
|
||||
@@ -389,7 +391,7 @@ func RecordConsumeLog(c *gin.Context, userId int, params RecordConsumeLogParams)
|
||||
}
|
||||
err := createLog(log)
|
||||
if err != nil {
|
||||
logger.LogError(c, "failed to record log: "+err.Error())
|
||||
logger.LogError(c, common.LogText("failed to record log: %s", err.Error()))
|
||||
}
|
||||
if common.DataExportEnabled {
|
||||
LogQuotaData(QuotaDataLogParams{
|
||||
@@ -410,7 +412,7 @@ func RecordConsumeLog(c *gin.Context, userId int, params RecordConsumeLogParams)
|
||||
type RecordTaskBillingLogParams struct {
|
||||
UserId int
|
||||
LogType int
|
||||
Content string
|
||||
Content []*common.Message
|
||||
ChannelId int
|
||||
ModelName string
|
||||
Quota int
|
||||
@@ -432,23 +434,24 @@ func RecordTaskBillingLog(params RecordTaskBillingLogParams) {
|
||||
}
|
||||
}
|
||||
createdAt := common.GetTimestamp()
|
||||
other := params.Other
|
||||
log := &Log{
|
||||
UserId: params.UserId,
|
||||
Username: username,
|
||||
CreatedAt: createdAt,
|
||||
Type: params.LogType,
|
||||
Content: params.Content,
|
||||
Content: other.SetContent(params.Content),
|
||||
TokenName: tokenName,
|
||||
ModelName: params.ModelName,
|
||||
Quota: params.Quota,
|
||||
ChannelId: params.ChannelId,
|
||||
TokenId: params.TokenId,
|
||||
Group: params.Group,
|
||||
Other: params.Other.JSONString(),
|
||||
Other: other.JSONString(),
|
||||
}
|
||||
err := createLog(log)
|
||||
if err != nil {
|
||||
common.SysLog("failed to record task billing log: " + err.Error())
|
||||
common.SysLog(common.LogText("failed to record task billing log: %s", err.Error()))
|
||||
}
|
||||
if params.LogType == LogTypeConsume && common.DataExportEnabled {
|
||||
nodeName := params.NodeName
|
||||
@@ -606,8 +609,8 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
|
||||
}
|
||||
err = tx.Model(&Log{}).Limit(logSearchCountLimit).Count(&total).Error
|
||||
if err != nil {
|
||||
common.SysError("failed to count user logs: " + err.Error())
|
||||
return nil, 0, errors.New("查询日志失败")
|
||||
common.SysError(common.LogText("failed to count user logs: %s", err.Error()))
|
||||
return nil, 0, common.NewMessage("Failed to query logs")
|
||||
}
|
||||
order := "logs.id desc"
|
||||
if common.UsingLogDatabase(common.DatabaseTypeClickHouse) {
|
||||
@@ -615,8 +618,8 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
|
||||
}
|
||||
err = tx.Order(order).Limit(num).Offset(startIdx).Find(&logs).Error
|
||||
if err != nil {
|
||||
common.SysError("failed to search user logs: " + err.Error())
|
||||
return nil, 0, errors.New("查询日志失败")
|
||||
common.SysError(common.LogText("failed to search user logs: %s", err.Error()))
|
||||
return nil, 0, common.NewMessage("Failed to query logs")
|
||||
}
|
||||
|
||||
formatUserLogs(logs, startIdx)
|
||||
@@ -683,16 +686,16 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa
|
||||
|
||||
// 执行查询
|
||||
if err := tx.Scan(&stat).Error; err != nil {
|
||||
common.SysError("failed to query log stat: " + err.Error())
|
||||
return stat, errors.New("查询统计数据失败")
|
||||
common.SysError(common.LogText("failed to query log stat: %s", err.Error()))
|
||||
return stat, common.NewMessage("Failed to query statistics")
|
||||
}
|
||||
var rateStat struct {
|
||||
Rpm int
|
||||
Tpm int
|
||||
}
|
||||
if err := rpmTpmQuery.Scan(&rateStat).Error; err != nil {
|
||||
common.SysError("failed to query rpm/tpm stat: " + err.Error())
|
||||
return stat, errors.New("查询统计数据失败")
|
||||
common.SysError(common.LogText("failed to query rpm/tpm stat: %s", err.Error()))
|
||||
return stat, common.NewMessage("Failed to query statistics")
|
||||
}
|
||||
stat.Rpm = rateStat.Rpm
|
||||
stat.Tpm = rateStat.Tpm
|
||||
|
||||
Reference in new issue
Block a user