@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "fmt"
6
7
"log"
7
8
"strings"
8
9
"time"
@@ -27,8 +28,8 @@ func init() {
27
28
}
28
29
29
30
type NSQConfig struct {
30
- Addr string `json:"addr"`
31
- TopicPrefix string `json:"topic_prefix"`
31
+ Addr string `json:"addr" yaml:"addr" `
32
+ TopicPrefix string `json:"topic_prefix" yaml:"topic_prefix" `
32
33
}
33
34
34
35
// 定义 NSQ 消息结构
@@ -78,6 +79,11 @@ func convertInt(value interface{}) int {
78
79
}
79
80
}
80
81
82
+ func genAIKey (key string , provider string ) string {
83
+ keys := strings .Split (key , "@" )
84
+ return strings .TrimSuffix (keys [0 ], fmt .Sprintf ("-%s" , provider ))
85
+ }
86
+
81
87
// HandleMessage 处理从 NSQ 读取的消息
82
88
func (h * NSQHandler ) HandleMessage (message * nsq.Message ) error {
83
89
log .Printf ("Received message: %s" , string (message .Body ))
@@ -104,8 +110,7 @@ func (h *NSQHandler) HandleMessage(message *nsq.Message) error {
104
110
finalStatus := & AIProviderStatus {}
105
111
for _ , s := range data .AI .ProviderStats {
106
112
status := ToKeyStatus (s .Status ).Int ()
107
- keys := strings .Split (s .Key , "@" )
108
- key := keys [0 ]
113
+ key := genAIKey (s .Key , s .Provider )
109
114
err = h .aiKeyService .Save (ctx , key , & ai_key.Edit {
110
115
Status : & status ,
111
116
})
@@ -128,8 +133,9 @@ func (h *NSQHandler) HandleMessage(message *nsq.Message) error {
128
133
finalStatus = & s
129
134
}
130
135
if finalStatus != nil {
131
- keys := strings .Split (finalStatus .Key , "@" )
132
- err = h .aiKeyService .IncrUseToken (ctx , keys [0 ], convertInt (data .AI .TotalToken ))
136
+ //keys := strings.Split(finalStatus.Key, "@")
137
+ key := genAIKey (finalStatus .Key , finalStatus .Provider )
138
+ err = h .aiKeyService .IncrUseToken (ctx , key , convertInt (data .AI .TotalToken ))
133
139
if err != nil {
134
140
log .Printf ("Failed to increment AI key token: %v" , err )
135
141
return err
0 commit comments