Skip to content

Instantly share code, notes, and snippets.

@itamarhaber itamarhaber/xagg.lua Secret
Created Jun 23, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.