From 1b32e63411444de104fd5a5752b485e7562bcfd1 Mon Sep 17 00:00:00 2001 From: Pratish Bodhale Date: Mon, 1 Jul 2024 17:10:03 +0530 Subject: [PATCH] lua script fixes --- pkg/scalers/redis_hms_scaler.go | 47 ++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/pkg/scalers/redis_hms_scaler.go b/pkg/scalers/redis_hms_scaler.go index e7249a801ad..aa895a42bd4 100644 --- a/pkg/scalers/redis_hms_scaler.go +++ b/pkg/scalers/redis_hms_scaler.go @@ -33,27 +33,32 @@ type redisHmsMetadata struct { // NewRedisHMSScaler creates a new redisHMSScaler func NewRedisHMSScaler(ctx context.Context, config *ScalerConfig) (Scaler, error) { luaScript := ` - local mapName = KEYS[1] - - -- Retrieve the map from Redis - local map = redis.call(hgetall, mapName) - - -- Convert the flat list returned by hgetall to a proper table - local mapTable = {} - for i = 1, #map, 2 do - mapTable[map[i]] = tonumber(map[i+1]) - end - - -- Ensure the map has the necessary keys - if mapTable.current == nil or mapTable.desired == nil then - return nil, "Error: The map must contain 'current' and 'desired' keys." - end - - -- Calculate the difference - local difference = mapTable.desired - mapTable.current - - -- Return the difference - return difference +local mapName = KEYS[1] + +-- Retrieve the map from Redis +local map = redis.call('HGETALL', mapName) + +-- Convert the flat list returned by hgetall to a proper table +local mapTable = {} +for i = 1, #map, 2 do + mapTable[map[i]] = tonumber(map[i+1]) +end + +-- Ensure the map has the necessary keys +if mapTable.current == nil or mapTable.desired == nil then + return nil, "Error: The map must contain 'current' and 'desired' keys." +end + +-- Calculate the difference +local difference = mapTable.desired - mapTable.current + +-- If the difference is positive, return it +if difference > 0 then + return difference +end + +-- Return the difference +return 0 ` metricType, err := GetMetricTargetType(config)