32
32
clearAllModelErrorsScript = redis .NewScript (clearAllModelErrorsLuaScript )
33
33
)
34
34
35
- func buildStatsKey (model string , channelID interface {}) string {
36
- return fmt .Sprintf ("%s%s%s%v%s" , modelKeyPrefix , model , channelKeyPart , channelID , statsKeySuffix )
37
- }
38
-
39
35
// GetModelErrorRate gets error rate for a specific model across all channels
40
36
func GetModelsErrorRate (ctx context.Context ) (map [string ]float64 , error ) {
41
37
if ! common .RedisEnabled {
@@ -48,11 +44,8 @@ func GetModelsErrorRate(ctx context.Context) (map[string]float64, error) {
48
44
iter := common .RDB .Scan (ctx , 0 , pattern , 0 ).Iterator ()
49
45
for iter .Next (ctx ) {
50
46
key := iter .Val ()
51
- parts := strings .Split (key , ":" )
52
- if len (parts ) != 3 || parts [2 ] != "total_stats" {
53
- continue
54
- }
55
- model := parts [1 ]
47
+ model := strings .TrimPrefix (key , modelKeyPrefix )
48
+ model = strings .TrimSuffix (model , modelTotalStatsSuffix )
56
49
57
50
rate , err := getModelErrorRateScript .Run (
58
51
ctx ,
@@ -101,7 +94,6 @@ func AddRequest(ctx context.Context, model string, channelID int64, isError bool
101
94
errorFlag ,
102
95
now ,
103
96
config .GetModelErrorAutoBanRate (),
104
- time .Second .Milliseconds ()* 15 ,
105
97
canAutoBan (),
106
98
).Int64 ()
107
99
if err != nil {
@@ -110,24 +102,42 @@ func AddRequest(ctx context.Context, model string, channelID int64, isError bool
110
102
return val == 3 , val == 1 , nil
111
103
}
112
104
105
+ func buildStatsKey (model string , channelID string ) string {
106
+ return fmt .Sprintf ("%s%s%s%v%s" , modelKeyPrefix , model , channelKeyPart , channelID , statsKeySuffix )
107
+ }
108
+
109
+ func getModelChannelID (key string ) (string , int64 , bool ) {
110
+ content := strings .TrimPrefix (key , modelKeyPrefix )
111
+ content = strings .TrimSuffix (content , statsKeySuffix )
112
+ model , channelIDStr , ok := strings .Cut (content , channelKeyPart )
113
+ if ! ok {
114
+ return "" , 0 , false
115
+ }
116
+ channelID , err := strconv .ParseInt (channelIDStr , 10 , 64 )
117
+ if err != nil {
118
+ return "" , 0 , false
119
+ }
120
+ return model , channelID , true
121
+ }
122
+
113
123
// GetChannelModelErrorRates gets error rates for a specific channel
114
124
func GetChannelModelErrorRates (ctx context.Context , channelID int64 ) (map [string ]float64 , error ) {
115
125
if ! common .RedisEnabled {
116
126
return map [string ]float64 {}, nil
117
127
}
118
128
119
129
result := make (map [string ]float64 )
120
- pattern := buildStatsKey ("*" , channelID )
130
+ pattern := buildStatsKey ("*" , strconv . FormatInt ( channelID , 10 ) )
121
131
now := time .Now ().UnixMilli ()
122
132
123
133
iter := common .RDB .Scan (ctx , 0 , pattern , 0 ).Iterator ()
124
134
for iter .Next (ctx ) {
125
135
key := iter .Val ()
126
- parts := strings .Split (key , ":" )
127
- if len (parts ) != 5 || parts [4 ] != "stats" {
136
+
137
+ model , _ , ok := getModelChannelID (key )
138
+ if ! ok {
128
139
continue
129
140
}
130
- model := parts [1 ]
131
141
132
142
rate , err := getChannelModelErrorRateScript .Run (
133
143
ctx ,
@@ -206,7 +216,8 @@ func GetAllBannedModelChannels(ctx context.Context) (map[string][]int64, error)
206
216
207
217
for iter .Next (ctx ) {
208
218
key := iter .Val ()
209
- model := strings .Split (key , ":" )[1 ]
219
+ model := strings .TrimPrefix (key , modelKeyPrefix )
220
+ model = strings .TrimSuffix (model , bannedKeySuffix )
210
221
211
222
channels , err := getBannedChannelsScript .Run (
212
223
ctx ,
@@ -233,20 +244,15 @@ func GetAllChannelModelErrorRates(ctx context.Context) (map[int64]map[string]flo
233
244
}
234
245
235
246
result := make (map [int64 ]map [string ]float64 )
236
- pattern := modelKeyPrefix + "*" + channelKeyPart + "*" + statsKeySuffix
247
+ pattern := buildStatsKey ( "*" , "*" )
237
248
now := time .Now ().UnixMilli ()
238
249
239
250
iter := common .RDB .Scan (ctx , 0 , pattern , 0 ).Iterator ()
240
251
for iter .Next (ctx ) {
241
252
key := iter .Val ()
242
- parts := strings .Split (key , ":" )
243
- if len (parts ) != 5 || parts [4 ] != "stats" {
244
- continue
245
- }
246
253
247
- model := parts [1 ]
248
- channelID , err := strconv .ParseInt (parts [3 ], 10 , 64 )
249
- if err != nil {
254
+ model , channelID , ok := getModelChannelID (key )
255
+ if ! ok {
250
256
continue
251
257
}
252
258
@@ -281,14 +287,14 @@ local channel_id = ARGV[1]
281
287
local is_error = tonumber(ARGV[2])
282
288
local now_ts = tonumber(ARGV[3])
283
289
local max_error_rate = tonumber(ARGV[4])
284
- local statsExpiry = tonumber(ARGV[5])
285
- local can_auto_ban = tonumber(ARGV[6])
290
+ local can_auto_ban = tonumber(ARGV[5])
286
291
287
292
local banned_key = "model:" .. model .. ":banned"
288
293
local stats_key = "model:" .. model .. ":channel:" .. channel_id .. ":stats"
289
294
local model_stats_key = "model:" .. model .. ":total_stats"
290
295
local maxSliceCount = 12
291
- local current_slice = math.floor(now_ts / 10000)
296
+ local statsExpiry = maxSliceCount * 10 * 1000
297
+ local current_slice = math.floor(now_ts / 10 / 1000)
292
298
293
299
local function parse_req_err(value)
294
300
if not value then return 0, 0 end
@@ -367,7 +373,7 @@ return check_channel_error()
367
373
local model_stats_key = KEYS[1]
368
374
local now_ts = tonumber(ARGV[1])
369
375
local maxSliceCount = 12
370
- local current_slice = math.floor(now_ts / 10000 )
376
+ local current_slice = math.floor(now_ts / 10 / 1000 )
371
377
local min_valid_slice = current_slice - maxSliceCount
372
378
373
379
local function parse_req_err(value)
@@ -389,14 +395,14 @@ for i = 1, #all_slices, 2 do
389
395
end
390
396
391
397
if total_req == 0 then return 0 end
392
- return total_err / total_req
398
+ return string.format("%.2f", total_err / total_req)
393
399
`
394
400
395
401
getChannelModelErrorRateLuaScript = `
396
402
local stats_key = KEYS[1]
397
403
local now_ts = tonumber(ARGV[1])
398
404
local maxSliceCount = 12
399
- local current_slice = math.floor(now_ts / 10000 )
405
+ local current_slice = math.floor(now_ts / 10 / 1000 )
400
406
local min_valid_slice = current_slice - maxSliceCount
401
407
402
408
local function parse_req_err(value)
0 commit comments