Skip to content

Commit

Permalink
[SDCISA-13736] Fix bad habits in analyze-pathsize.lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenalpha committed Jan 3, 2024
1 parent 1b13c2f commit 50232a4
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions redis/analyze-pathsize.lua
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
local sep = ":";
local toAnalyze = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]

local resourceCount = 0
local resourcesSize = 0

local function analyzeChildrenAndItself(path)
if redis.call('exists',resourcesPrefix..path) == 1 then
local res_size = redis.call('hget', resourcesPrefix..path, 'resource')
resourceCount = resourceCount + 1
resourcesSize = resourcesSize + string.len(res_size)
elseif redis.call('exists',collectionsPrefix..path) == 1 then
local members = redis.call('zrangebyscore',collectionsPrefix..path,'-inf','+inf')
for key,value in pairs(members) do
local pathToAnalyze = path..":"..value
analyzeChildrenAndItself(pathToAnalyze)
end
else
redis.log(redis.LOG_WARNING, "can't analyze resource from type: "..path)
end
end

local function round(num, dp)
return string.format("%."..(dp or 0).."f", num)
end

local function toHumanReadable(size)
if size >= 1048576 then
local mbs = round(size / 1048576, 2)
return mbs.." MB"
elseif size >= 1024 then
local kbs = round(size / 1024, 2)
return kbs.." KB"
else
return size.." Bytes"
end
end

if "/" == toAnalyze then
toAnalyze = ""
end

analyzeChildrenAndItself(toAnalyze)

local sep = ":";
local toAnalyze = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]

local resourceCount = 0
local resourcesSize = 0

local function analyzeChildrenAndItself(path)
local pathAbs = resourcesPrefix .. path
if redis.call('exists',pathAbs) == 1 then
local res_size = redis.call('hget', pathAbs, 'resource')
resourceCount = resourceCount + 1
resourcesSize = resourcesSize + string.len(res_size)
elseif redis.call('exists',pathAbs) == 1 then
local members = redis.call('zrangebyscore',pathAbs,'-inf','+inf')
for key,value in pairs(members) do
local pathToAnalyze = path..":"..value
analyzeChildrenAndItself(pathToAnalyze)
end
else
redis.log(redis.LOG_WARNING, "can't analyze resource from type: "..path)
end
end

local function round(num, dp)
return string.format("%."..(dp or 0).."f", num)
end

local function toHumanReadable(size)
if size >= 1048576 then
local mbs = round(size / 1048576, 2)
return mbs.." MB"
elseif size >= 1024 then
local kbs = round(size / 1024, 2)
return kbs.." KB"
else
return size.." Bytes"
end
end

if "/" == toAnalyze then
toAnalyze = ""
end

analyzeChildrenAndItself(toAnalyze)

return "Found "..resourceCount.." resources with total size of "..toHumanReadable(resourcesSize)

0 comments on commit 50232a4

Please sign in to comment.