Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
thomasdarimont / Redis_Stats.md
Last active January 31, 2023 17:27
Example for computing various running statistics with Lua in Redis backed by a hash

Running statistics with Redis and Lua

This is an example for computing running statistics with Lua backed by a hash in Redis. We support counting, average (with and without exponential smoothing), stddev, variance, min, max, sum of observed values. An example for approximating a running median can be found here: https://gist.github.com/thomasdarimont/fff68191d45a001b2d84

Data structure

We use a hash for storing various statistic value under the key "stats_value" in redis. Note: If you need a specific alpha value for smoothing the average, then set the desired alpha -> e.g. alpha 0.7. If alpha is 0.0 then no smoothing is applied.

@kikito
kikito / assert_contains.lua
Created August 29, 2014 11:44
Lua Busted table contains assertion
local s = require("say")
local function contains(container, contained)
if container == contained then return true end
local t1,t2 = type(container), type(contained)
if t1 ~= t2 then return false end
if t1 == 'table' then
for k,v in pairs(contained) do
if not contains(container[k], v) then return false end