From 1017c1575859f92548808059c9f4235efc810ff7 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 17 Sep 2023 19:12:58 +0800 Subject: [PATCH] chore: update implementation --- controller/log.go | 3 +- controller/relay-audio.go | 4 +- controller/relay-image.go | 4 +- controller/relay-text.go | 5 +-- model/ability.go | 2 +- model/cache.go | 4 +- model/channel.go | 9 +--- model/log.go | 9 ++-- web/src/components/ChannelsTable.js | 6 +-- web/src/components/LogsTable.js | 69 ++++++++++++++++------------- 10 files changed, 56 insertions(+), 59 deletions(-) diff --git a/controller/log.go b/controller/log.go index c0c3f69f..df808d8d 100644 --- a/controller/log.go +++ b/controller/log.go @@ -45,8 +45,7 @@ func GetUserLogs(c *gin.Context) { endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64) tokenName := c.Query("token_name") modelName := c.Query("model_name") - channel, _ := strconv.Atoi(c.Query("channel")) - logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage, channel) + logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage) if err != nil { c.JSON(200, gin.H{ "success": false, diff --git a/controller/relay-audio.go b/controller/relay-audio.go index fe8fac34..1ebb751a 100644 --- a/controller/relay-audio.go +++ b/controller/relay-audio.go @@ -17,7 +17,7 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode tokenId := c.GetInt("token_id") channelType := c.GetInt("channel") - channelID := c.GetInt("channel_id") + channelId := c.GetInt("channel_id") userId := c.GetInt("id") group := c.GetString("group") @@ -107,7 +107,7 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode if quota != 0 { tokenName := c.GetString("token_name") logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) - model.RecordConsumeLog(userId, channelID, 0, 0, audioModel, tokenName, quota, logContent) + model.RecordConsumeLog(userId, channelId, 0, 0, audioModel, tokenName, quota, logContent) model.UpdateUserUsedQuotaAndRequestCount(userId, quota) channelId := c.GetInt("channel_id") model.UpdateChannelUsedQuota(channelId, quota) diff --git a/controller/relay-image.go b/controller/relay-image.go index 90092160..ea1e1897 100644 --- a/controller/relay-image.go +++ b/controller/relay-image.go @@ -18,7 +18,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode tokenId := c.GetInt("token_id") channelType := c.GetInt("channel") - channelID := c.GetInt("channel_id") + channelId := c.GetInt("channel_id") userId := c.GetInt("id") consumeQuota := c.GetBool("consume_quota") group := c.GetString("group") @@ -138,7 +138,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode if quota != 0 { tokenName := c.GetString("token_name") logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) - model.RecordConsumeLog(userId, channelID, 0, 0, imageModel, tokenName, quota, logContent) + model.RecordConsumeLog(userId, channelId, 0, 0, imageModel, tokenName, quota, logContent) model.UpdateUserUsedQuotaAndRequestCount(userId, quota) channelId := c.GetInt("channel_id") model.UpdateChannelUsedQuota(channelId, quota) diff --git a/controller/relay-text.go b/controller/relay-text.go index 0b92d427..43366da4 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -36,7 +36,7 @@ func init() { func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { channelType := c.GetInt("channel") - channelID := c.GetInt("channel_id") + channelId := c.GetInt("channel_id") tokenId := c.GetInt("token_id") userId := c.GetInt("id") consumeQuota := c.GetBool("consume_quota") @@ -328,7 +328,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { var textResponse TextResponse tokenName := c.GetString("token_name") - channelId := c.GetInt("channel_id") defer func() { // c.Writer.Flush() @@ -361,7 +360,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { } if quota != 0 { logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) - model.RecordConsumeLog(userId, channelID, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent) + model.RecordConsumeLog(userId, channelId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent) model.UpdateUserUsedQuotaAndRequestCount(userId, quota) model.UpdateChannelUsedQuota(channelId, quota) diff --git a/model/ability.go b/model/ability.go index 8724e69f..eb68fa0d 100644 --- a/model/ability.go +++ b/model/ability.go @@ -10,7 +10,7 @@ type Ability struct { Model string `json:"model" gorm:"primaryKey;autoIncrement:false"` ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"` Enabled bool `json:"enabled"` - Priority *int64 `json:"priority" gorm:"bigint;default:0"` + Priority int64 `json:"priority" gorm:"bigint;default:0"` } func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) { diff --git a/model/cache.go b/model/cache.go index 92576165..631ef49a 100644 --- a/model/cache.go +++ b/model/cache.go @@ -160,7 +160,7 @@ func InitChannelCache() { for group, model2channels := range newGroup2model2channels { for model, channels := range model2channels { sort.Slice(channels, func(i, j int) bool { - return channels[i].GetPriority() > channels[j].GetPriority() + return channels[i].Priority > channels[j].Priority }) newGroup2model2channels[group][model] = channels } @@ -192,7 +192,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error } // choose by priority firstChannel := channels[0] - if firstChannel.GetPriority() > 0 { + if firstChannel.Priority > 0 { return firstChannel, nil } idx := rand.Intn(len(channels)) diff --git a/model/channel.go b/model/channel.go index 1f5d9571..2da210fa 100644 --- a/model/channel.go +++ b/model/channel.go @@ -23,14 +23,7 @@ type Channel struct { Group string `json:"group" gorm:"type:varchar(32);default:'default'"` UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"` ModelMapping string `json:"model_mapping" gorm:"type:varchar(1024);default:''"` - Priority *int64 `json:"priority" gorm:"bigint;default:0"` -} - -func (c *Channel) GetPriority() int64 { - if c.Priority == nil { - return 0 - } - return *c.Priority + Priority int64 `json:"priority" gorm:"bigint;default:0"` } func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) { diff --git a/model/log.go b/model/log.go index aa659d39..53e21d22 100644 --- a/model/log.go +++ b/model/log.go @@ -45,7 +45,7 @@ func RecordLog(userId int, logType int, content string) { } } -func RecordConsumeLog(userId int, channelID, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) { +func RecordConsumeLog(userId int, channelId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) { if !common.LogConsumeEnabled { return } @@ -60,7 +60,7 @@ func RecordConsumeLog(userId int, channelID, promptTokens int, completionTokens TokenName: tokenName, ModelName: modelName, Quota: quota, - Channel: channelID, + Channel: channelId, } err := DB.Create(log).Error if err != nil { @@ -97,7 +97,7 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName return logs, err } -func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int, channel 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) { var tx *gorm.DB if logType == LogTypeUnknown { tx = DB.Where("user_id = ?", userId) @@ -116,9 +116,6 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int if endTimestamp != 0 { tx = tx.Where("created_at <= ?", endTimestamp) } - if channel != 0 { - tx = tx.Where("channel = ?", channel) - } err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error return logs, err } diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 708d9daf..43262e1a 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -24,7 +24,7 @@ function renderType(type) { } type2label[0] = { value: 0, text: '未知类型', color: 'grey' }; } - return ; + return ; } function renderBalance(type, balance) { @@ -399,7 +399,7 @@ const ChannelsTable = () => { }}> } - content='输入优先级,越高越优先' + content='渠道选择优先级,越高越优先' basic /> @@ -471,7 +471,7 @@ const ChannelsTable = () => { - + diff --git a/web/src/components/LogsTable.js b/web/src/components/LogsTable.js index 0e42371d..f3741354 100644 --- a/web/src/components/LogsTable.js +++ b/web/src/components/LogsTable.js @@ -57,9 +57,9 @@ const LogsTable = () => { model_name: '', start_timestamp: timestamp2string(0), end_timestamp: timestamp2string(now.getTime() / 1000 + 3600), - channel: 0, + channel: '' }); - const { username, token_name, model_name, start_timestamp, end_timestamp, channel } = inputs; + const { username, token_name, model_name, start_timestamp, end_timestamp, channel } = inputs; const [stat, setStat] = useState({ quota: 0, @@ -73,7 +73,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=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}&channel=${channel}`); + let res = await API.get(`/api/log/self/stat?type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`); const { success, message, data } = res.data; if (success) { setStat(data); @@ -112,7 +112,7 @@ const LogsTable = () => { if (isAdminUser) { url = `/api/log/?p=${startIdx}&type=${logType}&username=${username}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}&channel=${channel}`; } else { - url = `/api/log/self/?p=${startIdx}&type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}&channel=${channel}`; + url = `/api/log/self/?p=${startIdx}&type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; } const res = await API.get(url); const { success, message, data } = res.data; @@ -206,21 +206,11 @@ const LogsTable = () => {
- { - isAdminUser && ( - - ) - } - - - @@ -229,6 +219,19 @@ const LogsTable = () => { onChange={handleInputChange} /> 查询 + { + isAdminUser && <> + + + + + + + }
@@ -242,6 +245,17 @@ const LogsTable = () => { > 时间 + { + isAdminUser && { + sortLog('channel'); + }} + width={1} + > + 渠道 + + } { isAdminUser && { > 模型 - { - sortLog('channel'); - }} - width={2} - > - 渠道 - { @@ -312,16 +317,16 @@ const LogsTable = () => { onClick={() => { sortLog('quota'); }} - width={2} + width={1} > - 消耗额度 + 额度 { sortLog('content'); }} - width={isAdminUser ? 4 : 5} + width={isAdminUser ? 4 : 6} > 详情 @@ -339,6 +344,11 @@ const LogsTable = () => { return ( {renderTimestamp(log.created_at)} + { + isAdminUser && ( + {log.channel ? : ''} + ) + } { isAdminUser && ( {log.username ? : ''} @@ -347,7 +357,6 @@ const LogsTable = () => { {log.token_name ? : ''} {renderType(log.type)} {log.model_name ? : ''} - {log.channel ? : ''} {log.prompt_tokens ? log.prompt_tokens : ''} {log.completion_tokens ? log.completion_tokens : ''} {log.quota ? renderQuota(log.quota, 6) : ''} @@ -359,7 +368,7 @@ const LogsTable = () => { - +