Kaynağa Gözat

feat: 日志显示重试信息

CaIon 1 yıl önce
ebeveyn
işleme
71dcf43c71
5 değiştirilmiş dosya ile 50 ekleme ve 2 silme
  1. 9 0
      common/utils.go
  2. 5 2
      controller/relay.go
  3. 9 0
      model/log.go
  4. 3 0
      relay/relay-text.go
  5. 24 0
      web/src/components/LogsTable.js

+ 9 - 0
common/utils.go

@@ -258,3 +258,12 @@ func MapToJsonStrFloat(m map[string]float64) string {
 	}
 	return string(bytes)
 }
+
+func StrToMap(str string) map[string]interface{} {
+	m := make(map[string]interface{})
+	err := json.Unmarshal([]byte(str), &m)
+	if err != nil {
+		return nil
+	}
+	return m
+}

+ 5 - 2
controller/relay.go

@@ -43,7 +43,7 @@ func Relay(c *gin.Context) {
 	group := c.GetString("group")
 	originalModel := c.GetString("original_model")
 	openaiErr := relayHandler(c, relayMode)
-	useChannel := []int{channelId}
+	c.Set("use_channel", []string{fmt.Sprintf("%d", channelId)})
 	if openaiErr != nil {
 		go processChannelError(c, channelId, openaiErr)
 	} else {
@@ -56,7 +56,9 @@ func Relay(c *gin.Context) {
 			break
 		}
 		channelId = channel.Id
-		useChannel = append(useChannel, channelId)
+		useChannel := c.GetStringSlice("use_channel")
+		useChannel = append(useChannel, fmt.Sprintf("%d", channelId))
+		c.Set("use_channel", useChannel)
 		common.LogInfo(c.Request.Context(), fmt.Sprintf("using channel #%d to retry (remain times %d)", channel.Id, i))
 		middleware.SetupContextForSelectedChannel(c, channel, originalModel)
 
@@ -67,6 +69,7 @@ func Relay(c *gin.Context) {
 			go processChannelError(c, channelId, openaiErr)
 		}
 	}
+	useChannel := c.GetStringSlice("use_channel")
 	if len(useChannel) > 1 {
 		retryLogStr := fmt.Sprintf("重试:%s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
 		common.LogInfo(c.Request.Context(), retryLogStr)

+ 9 - 0
model/log.go

@@ -142,6 +142,15 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
 		tx = tx.Where("created_at <= ?", endTimestamp)
 	}
 	err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
+	for i := range logs {
+		var otherMap map[string]interface{}
+		otherMap = common.StrToMap(logs[i].Other)
+		if otherMap != nil {
+			// delete admin
+			delete(otherMap, "admin_info")
+		}
+		logs[i].Other = common.MapToJsonStr(otherMap)
+	}
 	return logs, err
 }
 

+ 3 - 0
relay/relay-text.go

@@ -320,6 +320,9 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, textRe
 	other["group_ratio"] = groupRatio
 	other["completion_ratio"] = completionRatio
 	other["model_price"] = modelPrice
+	adminInfo := make(map[string]interface{})
+	adminInfo["use_channel"] = ctx.GetStringSlice("use_channel")
+	other["admin_info"] = adminInfo
 	model.RecordConsumeLog(ctx, relayInfo.UserId, relayInfo.ChannelId, promptTokens, completionTokens, logModel, tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, other)
 
 	//if quota != 0 {

+ 24 - 0
web/src/components/LogsTable.js

@@ -294,6 +294,30 @@ const LogsTable = () => {
         );
       },
     },
+    {
+      title: '重试',
+      dataIndex: 'retry',
+      className: isAdmin() ? 'tableShow' : 'tableHiddle',
+      render: (text, record, index) => {
+        let content = '渠道:' + record.channel;
+        if (record.other !== '') {
+          let other = JSON.parse(record.other);
+          if (other.admin_info !== undefined) {
+            if (
+              other.admin_info.use_channel !== null &&
+              other.admin_info.use_channel !== undefined &&
+              other.admin_info.use_channel !== ''
+            ) {
+              // channel id array
+              let useChannel = other.admin_info.use_channel;
+              let useChannelStr = useChannel.join('->');
+              content = `渠道:${useChannelStr}`;
+            }
+          }
+        }
+        return isAdminUser ? <div>{content}</div> : <></>;
+      },
+    },
     {
       title: '详情',
       dataIndex: 'content',