From e7534878f308281551e42efb8046d7ba4871b09e Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Sat, 24 Jun 2023 19:34:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=B8=A0=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/log.go | 12 ++++++++---- controller/relay-text.go | 4 +--- i18n/en.json | 1 + model/log.go | 24 +++++++++++++++++++----- web/src/components/LogsTable.js | 23 ++++++++++++++++++----- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/controller/log.go b/controller/log.go index 09cb1bb6..9de96fa2 100644 --- a/controller/log.go +++ b/controller/log.go @@ -17,7 +17,8 @@ func GetAllLogs(c *gin.Context) { endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) username := c.Query("username") modelName := c.Query("model_name") - logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, p*common.ItemsPerPage, common.ItemsPerPage) + channelName := c.Query("channel_name") + logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, p*common.ItemsPerPage, common.ItemsPerPage, channelName) if err != nil { c.JSON(200, gin.H{ "success": false, @@ -43,7 +44,8 @@ func GetUserLogs(c *gin.Context) { endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) tokenName := c.Query("token_name") modelName := c.Query("model_name") - logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage) + channelName := c.Query("channel_name") + logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage, channelName) if err != nil { c.JSON(200, gin.H{ "success": false, @@ -99,7 +101,8 @@ func GetLogsStat(c *gin.Context) { endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) username := c.Query("username") modelName := c.Query("model_name") - quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, "") + channelName := c.Query("channel_name") + quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, "", channelName) //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "") c.JSON(200, gin.H{ "success": true, @@ -118,7 +121,8 @@ func GetLogsSelfStat(c *gin.Context) { endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) tokenName := c.Query("token_name") modelName := c.Query("model_name") - quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName) + channelName := c.Query("channel_name") + quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName, channelName) //tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName) c.JSON(200, gin.H{ "success": true, diff --git a/controller/relay-text.go b/controller/relay-text.go index d0a27f84..a6fc2299 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -140,8 +140,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { if ratio != 0 && quota <= 0 { quota = 1 } - tokenName := c.GetString("token_name") - model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过渠道「%s」通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)%d prompt + %d completion = %d tokens", channelName, tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio, prompt, completion, tokens)) if strings.Contains(channelName, "免费") == false { quotaDelta := quota - preConsumedQuota err := model.PostConsumeTokenQuota(tokenId, quotaDelta) @@ -150,7 +148,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { } tokenName := c.GetString("token_name") logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) - model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent) + model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent, channelName) model.UpdateUserUsedQuotaAndRequestCount(userId, quota) channelId := c.GetInt("channel_id") model.UpdateChannelUsedQuota(channelId, quota) diff --git a/i18n/en.json b/i18n/en.json index 9072be13..3ccdec15 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -449,6 +449,7 @@ "留空则查询全部用户": "Leave blank to query all users", "留空则查询全部令牌": "Leave blank to query all tokens", "模型名称": "Model Name", + "渠道名称": "Channel Name", "留空则查询全部模型": "Leave blank to query all models", "起始时间": "Start Time", "结束时间": "End Time", diff --git a/model/log.go b/model/log.go index ce56bdbb..313478ae 100644 --- a/model/log.go +++ b/model/log.go @@ -17,6 +17,7 @@ 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"` + ChannelName string `json:"channel_name" gorm:"index;default:''"` } const ( @@ -44,7 +45,7 @@ func RecordLog(userId int, logType int, content string) { } } -func RecordConsumeLog(userId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) { +func RecordConsumeLog(userId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string, channelName string) { if !common.LogConsumeEnabled { return } @@ -59,6 +60,7 @@ func RecordConsumeLog(userId int, promptTokens int, completionTokens int, modelN TokenName: tokenName, ModelName: modelName, Quota: quota, + ChannelName: channelName, } err := DB.Create(log).Error if err != nil { @@ -66,7 +68,7 @@ func RecordConsumeLog(userId int, promptTokens int, completionTokens int, modelN } } -func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, startIdx int, num int) (logs []*Log, err error) { +func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, startIdx int, num int, channelName string) (logs []*Log, err error) { var tx *gorm.DB if logType == LogTypeUnknown { tx = DB @@ -85,11 +87,14 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName if endTimestamp != 0 { tx = tx.Where("created_at <= ?", endTimestamp) } + if channelName != "" { + tx = tx.Where("channel_name = ?", channelName) + } err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error return logs, 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, channelName string) (logs []*Log, err error) { var tx *gorm.DB if logType == LogTypeUnknown { tx = DB.Where("user_id = ?", userId) @@ -108,6 +113,9 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int if endTimestamp != 0 { tx = tx.Where("created_at <= ?", endTimestamp) } + if channelName != "" { + tx = tx.Where("channel_name = ?", channelName) + } err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error return logs, err } @@ -122,7 +130,7 @@ func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) { return logs, err } -func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (quota int) { +func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channelName string) (quota int) { tx := DB.Table("logs").Select("sum(quota)") if username != "" { tx = tx.Where("username = ?", username) @@ -139,11 +147,14 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa if modelName != "" { tx = tx.Where("model_name = ?", modelName) } + if channelName != "" { + tx = tx.Where("channel_name = ?", channelName) + } tx.Where("type = ?", LogTypeConsume).Scan("a) return quota } -func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) { +func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channelName string) (token int) { tx := DB.Table("logs").Select("sum(prompt_tokens) + sum(completion_tokens)") if username != "" { tx = tx.Where("username = ?", username) @@ -160,6 +171,9 @@ func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelNa if modelName != "" { tx = tx.Where("model_name = ?", modelName) } + if channelName != "" { + tx = tx.Where("channel_name = ?", channelName) + } tx.Where("type = ?", LogTypeConsume).Scan(&token) return token } diff --git a/web/src/components/LogsTable.js b/web/src/components/LogsTable.js index 6cd102d1..4cfb9d43 100644 --- a/web/src/components/LogsTable.js +++ b/web/src/components/LogsTable.js @@ -53,10 +53,11 @@ const LogsTable = () => { const [inputs, setInputs] = useState({ name: '', model_name: '', + channel_name: '', start_timestamp: timestamp2string(0), end_timestamp: timestamp2string(now.getTime() / 1000 + 3600) }); - const { name, model_name, start_timestamp, end_timestamp } = inputs; + const { name, model_name,channel_name, start_timestamp, end_timestamp } = inputs; const [stat, setStat] = useState({ quota: 0, @@ -70,7 +71,7 @@ const LogsTable = () => { const getLogSelfStat = async () => { let localStartTimestamp = Date.parse(start_timestamp) / 1000; let localEndTimestamp = Date.parse(end_timestamp) / 1000; - let res = await API.get(`/api/log/self/stat?type=${logType}&token_name=${name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`); + let res = await API.get(`/api/log/self/stat?type=${logType}&token_name=${name}&model_name=${model_name}&channel_name=${channel_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`); const { success, message, data } = res.data; if (success) { setStat(data); @@ -82,7 +83,7 @@ const LogsTable = () => { const getLogStat = async () => { let localStartTimestamp = Date.parse(start_timestamp) / 1000; let localEndTimestamp = Date.parse(end_timestamp) / 1000; - let res = await API.get(`/api/log/stat?type=${logType}&username=${name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`); + let res = await API.get(`/api/log/stat?type=${logType}&username=${name}&model_name=${model_name}&channel_name=${channel_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`); const { success, message, data } = res.data; if (success) { setStat(data); @@ -96,9 +97,9 @@ const LogsTable = () => { let localStartTimestamp = Date.parse(start_timestamp) / 1000; let localEndTimestamp = Date.parse(end_timestamp) / 1000; if (isAdminUser) { - url = `/api/log/?p=${startIdx}&type=${logType}&username=${name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; + url = `/api/log/?p=${startIdx}&type=${logType}&username=${name}&model_name=${model_name}&channel_name=${channel_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; } else { - url = `/api/log/self/?p=${startIdx}&type=${logType}&token_name=${name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; + url = `/api/log/self/?p=${startIdx}&type=${logType}&token_name=${name}&model_name=${model_name}&channel_name=${channel_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; } const res = await API.get(url); const { success, message, data } = res.data; @@ -188,6 +189,8 @@ const LogsTable = () => { onChange={handleInputChange} /> + @@ -233,6 +236,15 @@ const LogsTable = () => { > 模型 + { + sortLog('channel_name'); + }} + width={2} + > + 渠道 + { @@ -295,6 +307,7 @@ const LogsTable = () => { } {renderType(log.type)} {log.model_name ? : ''} + {log.channel_name ? : ''} {log.prompt_tokens ? log.prompt_tokens: ''} {log.completion_tokens ? log.completion_tokens: ''} {log.quota ? renderQuota(log.quota, 6) : ''}