Skip to content

Instantly share code, notes, and snippets.

@mitallast
Last active April 19, 2017 20:54
Show Gist options
  • Save mitallast/b401c5a95db3716226b1 to your computer and use it in GitHub Desktop.
Save mitallast/b401c5a95db3716226b1 to your computer and use it in GitHub Desktop.
Average metrics using redis operations
# retrieve_metric_avg.lua $operation_type
local operation_type = ARGV[1];
local count = redis.call("HGET", "operations_count", operation_type)
local total = redis.call("HGET", "operations_total", operation_type)
if not count or not total then
return 0
else
local avg = tonumber(total)/tonumber(count)
return tostring(avg)
end
# update_metric_avg.lua $operation_type $uuid
local operation_type = ARGV[1]
local uuid = ARGV[2]
redis.call("HINCRBY", "operations_count", operation_type, 1)
local not_exists = redis.call("HSETNX", "operations_" .. operation_type .. "_users", uuid, 1)
if not_exists == 1 then
redis.call("HINCRBY", "operations_total", operation_type, 1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment