<commit_before>-- UNIVARIATE TIMESERIES IN REDIS
--
-- Stand alone Lua script for managing an univariate timeseries in Redis
--
-- A timeseries is an
-- 	1) ordered (with respect timestamps)
--  2) unique (each timestamp is unique within the timeseries)
--  3) associative (it associate a timestamp with a value)
-- container.
-- This script can be used by any clients, not just python-stdnet.
-- Commands are implemented in the *timeseries_commands* table. To
-- execute a command use the *timeseries* functions *call* and *call_multi*
--
-- timeseries.call('size', id)
-- timeseries.call('add', id, 100000, 'foo')
local function single_id(ids, cname)
    if # ids == 1 then
        return ids[1]
    else
        error('Only one timeseries permitted for command ' .. cname)
    end
end

-- COMMANDS TABLE
local timeseries_commands = {
    size = function (self, ids)
        local id = single_id(ids, 'len')
        return redis.call('zcard', id)
    end,
    -- Add timestamp-value pairs to the timeseries
    add = function (self, ids, ...)
        local id = single_id(ids, 'add')
        local N = # arg
        local h = math.floor(N/2)
        assert(2*h == N, 'Cannot add to timeseries. Number of arguments must be even')
        local i = 0
        local timestamp, value, stimestamp
        while i < N do
            timestamp = arg[i+1] + 0
            -- This is the trick. The value is a JSON encoded representation of
            -- a two-elements table given by the timestamp and the value itself
            value = cjson.encode({timestamp, arg[i+2]})
            redis.call('zremrangebyscore', id, timestamp, timestamp)
            redis.call('zadd', id, timestamp, value)
            i = i + 2
        end
    end,
    -- Check if *timestamp* exists in the timeseries
    exists = function (self, ids, timestamp)
        local value = self._single_value(ids, timestamp, 'exists')
        if value then
            return true
        else
            return false
        end
    end,
    -- Rank of *timestamp* in timeseries
    rank = function (self, ids, timestamp)
        local value = self._single_value(ids, timestamp, 'rank')
        if value then
            local id = single_id(ids, 'rank')
            return redis.call('zrank', id, value)
        end<commit_msg>more tests for timeseries. Added pop and pop_range variants<commit_after>-- UNIVARIATE TIMESERIES IN REDIS
--
-- Stand-alone Lua script for managing an univariate timeseries in Redis
--
-- A timeseries is an
--  1) ordered (with respect timestamps)
--  2) unique (each timestamp is unique within the timeseries)
--  3) associative (it associate a timestamp with a value)
-- container.
-- This script can be used by any clients, not just python-stdnet.
-- Commands are implemented in the *timeseries_commands* table. To
-- execute a command use the *timeseries* functions *call* and *call_multi*
--
-- timeseries.call('size', id)
-- timeseries.call('add', id, 100000, 'foo')
local function single_id(ids, cname)
    if # ids == 1 then
        return ids[1]
    else
        error('Only one timeseries permitted for command ' .. cname)
    end
end

-- COMMANDS TABLE
local timeseries_commands = {
    size = function (self, ids)
        local id = single_id(ids, 'len')
        return redis.call('zcard', id)
    end,
    -- Add timestamp-value pairs to the timeseries
    add = function (self, ids, ...)
        local id = single_id(ids, 'add')
        local N = # arg
        local h = math.floor(N/2)
        assert(2*h == N, 'Cannot add to timeseries. Number of arguments must be even')
        local i = 0
        local timestamp, value, stimestamp
        while i < N do
            timestamp = arg[i+1] + 0
            -- This is the trick. The value is a JSON encoded representation of
            -- a two-elements table given by the timestamp and the value itself
            value = cjson.encode({timestamp, arg[i+2]})
            -- This make sure of timestamp to be unique
            redis.call('zremrangebyscore', id, timestamp, timestamp)
            redis.call('zadd', id, timestamp, value)
            i = i + 2
        end
    end,
    -- Check if *timestamp* exists in the timeseries
    exists = function (self, ids, timestamp)
        local value = self._single_value(ids, timestamp, 'exists')
        if value then
            return true
        else
            return false
        end
    end,
    -- Rank of *timestamp* in timeseries
    rank = function (self, ids, timestamp)
        local value = self._single_value(ids, timestamp, 'rank')
        if value then
            local id = single_id(ids, 'rank')
            return redis.call('zrank', id, value)
        end
{'fraction_non_alphanumeric': 0.09931798806479114, 'fraction_numerical': 0.0076726342710997444, 'mean_word_length': 2.247750865051903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3817450', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1315, 'n_words': 524}