@@ -226,5 +226,125 @@ const claimTimedOutMeasurements = defineScript({
226226 } ,
227227} ) ;
228228
229- export const scripts = { recordProgress, recordProgressAppend, recordResult, markFinishedByTimeout, claimTimedOutMeasurements } ;
229+ const registerProbeLogScopes = defineScript ( {
230+ SCRIPT : `
231+ local reporterKey = KEYS[1]
232+ local knownScopesKey = KEYS[2]
233+ local activeWindow = tonumber(ARGV[1])
234+ local maxScopes = tonumber(ARGV[2])
235+ local minReporters = tonumber(ARGV[3])
236+ local reporterIdentity = ARGV[4]
237+ local now = tonumber(redis.call('TIME')[1])
238+ local cutoff = now - activeWindow
239+
240+ redis.call('ZREMRANGEBYSCORE', reporterKey, '-inf', cutoff)
241+
242+ local scores = redis.call('ZMSCORE', reporterKey, unpack(ARGV, 5))
243+ local newScopes = 0
244+
245+ for index = 1, #scores do
246+ if not scores[index] then
247+ newScopes = newScopes + 1
248+ end
249+ end
250+
251+ if redis.call('ZCARD', reporterKey) + newScopes > maxScopes then
252+ return 0
253+ end
254+
255+ local zaddArguments = {}
256+
257+ for index = 5, #ARGV do
258+ table.insert(zaddArguments, now)
259+ table.insert(zaddArguments, ARGV[index])
260+ end
261+
262+ redis.call('ZADD', reporterKey, 'GT', unpack(zaddArguments))
263+ redis.call('EXPIRE', reporterKey, activeWindow)
264+
265+ local knownScopes = redis.call('SMISMEMBER', knownScopesKey, unpack(ARGV, 5))
266+ local newKnownScopes = {}
267+
268+ for index = 3, #KEYS do
269+ local scopeKey = KEYS[index]
270+ local scopeIndex = index - 2
271+
272+ redis.call('ZADD', scopeKey, 'GT', now, reporterIdentity)
273+ redis.call('ZREMRANGEBYSCORE', scopeKey, '-inf', cutoff)
274+ redis.call('EXPIRE', scopeKey, activeWindow)
275+
276+ if knownScopes[scopeIndex] == 0 and redis.call('ZCARD', scopeKey) >= minReporters then
277+ table.insert(newKnownScopes, ARGV[index + 2])
278+ end
279+ end
280+
281+ if #newKnownScopes > 0 then
282+ redis.call('SADD', knownScopesKey, unpack(newKnownScopes))
283+ end
284+
285+ return 1
286+ ` ,
287+ parseCommand (
288+ parser : CommandParser ,
289+ reporterKey : string ,
290+ knownScopesKey : string ,
291+ scopeKeys : string [ ] ,
292+ reporterIdentity : string ,
293+ activeWindow : number ,
294+ maxScopes : number ,
295+ minReporters : number ,
296+ scopes : string [ ] ,
297+ ) {
298+ parser . push ( ( scopeKeys . length + 2 ) . toString ( ) ) ;
299+ parser . pushKey ( reporterKey ) ;
300+ parser . pushKey ( knownScopesKey ) ;
301+ scopeKeys . forEach ( scopeKey => parser . pushKey ( scopeKey ) ) ;
302+
303+ parser . push (
304+ activeWindow . toString ( ) ,
305+ maxScopes . toString ( ) ,
306+ minReporters . toString ( ) ,
307+ reporterIdentity ,
308+ ...scopes ,
309+ ) ;
310+ } ,
311+ transformReply ( reply : number ) {
312+ return reply === 1 ;
313+ } ,
314+ } ) ;
315+
316+ const countProbeLogScopeReporters = defineScript ( {
317+ NUMBER_OF_KEYS : 2 ,
318+ SCRIPT : `
319+ local now = tonumber(redis.call('TIME')[1])
320+ local cutoff = now - tonumber(ARGV[2])
321+
322+ redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', cutoff)
323+ local count = redis.call('ZCARD', KEYS[2])
324+
325+ if count < tonumber(ARGV[3]) then
326+ redis.call('SREM', KEYS[1], ARGV[1])
327+ end
328+
329+ return count
330+ ` ,
331+ parseCommand ( parser : CommandParser , knownScopesKey : string , scopeKey : string , scope : string , activeWindow : number , minReporters : number ) {
332+ parser . pushKey ( knownScopesKey ) ;
333+ parser . pushKey ( scopeKey ) ;
334+ parser . push ( scope , activeWindow . toString ( ) , minReporters . toString ( ) ) ;
335+ } ,
336+ transformReply ( reply : number ) {
337+ return reply ;
338+ } ,
339+ } ) ;
340+
341+ export const scripts = {
342+ recordProgress,
343+ recordProgressAppend,
344+ recordResult,
345+ markFinishedByTimeout,
346+ claimTimedOutMeasurements,
347+ registerProbeLogScopes,
348+ countProbeLogScopeReporters,
349+ } ;
230350export type RedisScripts = typeof scripts ;
0 commit comments