Skip to content

Instantly share code, notes, and snippets.

@itamarhaber
Created June 23, 2019 12:03
Show Gist options
  • Save itamarhaber/0107020b91c71cb52e57e9a9c890c24e to your computer and use it in GitHub Desktop.
Save itamarhaber/0107020b91c71cb52e57e9a9c890c24e to your computer and use it in GitHub Desktop.
xagg and zagg - Lua implementation for timeseries max
--[[
Aggregate a timeseries in a stream
KEYS[1] - the stream
ARGV[1] - start timestamp
ARGV[2] - end timestamp
ARGV[3] - field name
return: int (max of values)
]]--
local remove = table.remove
local agg = 0
local data = redis.call('XRANGE', KEYS[1], ARGV[1], ARGV[2])
while #data > 0 do
local msg = remove(data)
local fv = msg[2]
local datum = tonumber(fv[2]) -- user_usage
if datum > agg then
agg = datum
end
end
return agg
--[[
Aggregate a single timeseries in a zset of a device/metric
KEYS[1] - the zset
ARGV[1] - start timestamp
ARGV[2] - end timestamp
return: int (max of values)
]]--
local sub = string.sub
local remove = table.remove
local agg = 0
local data = redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1], ARGV[2])
while #data > 0 do
local point, fields = remove(data), {}
point:gsub("([^:]+)", function(c) fields[#fields+1] = c end)
local datum = tonumber(fields[2]) -- user_usage, 1st field
if datum > agg then
agg = datum
end
end
return agg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment