redis / redis-py

Redis Python client
MIT License
12.4k stars 2.48k forks source link

labels not working in timeseries #3272

Open Simoliv opened 3 weeks ago

Simoliv commented 3 weeks ago

Version: Latest I guess, installed from pypi, redis is docker container, also latest

Platform: Ubuntu 22.04

Description:

I do not get any labels, no matter what I try. I tried from the documentation:

import redis

r = redis.Redis(host='localhost', port=6379, db=0)
ts = r.ts()

print(r.ping())

try:
    r.execute_command('TS.CREATE', 'ts_key', labels={"label1": 1, "label2": 2}, duplicate_policy="LAST")
    print("created")
except redis.exceptions.ResponseError as e:
    # Ignore error if the timeseries already exists
    if "already exists" not in str(e):
        raise

#print(ts.add("ts_key", 1657265437756, 1, duplicate_policy="LAST"))
#print(ts.add("ts_key", "1657265437757", 2, duplicate_policy="LAST"))

#print(ts.add("ts_key", 1657265437754, 5, labels={"label1": 1, "label2": 2}, duplicate_policy="LAST"))
#print(ts.add("ts_key", 1657265437759, 17, labels={"label1": 3, "label2": 4}, duplicate_policy="LAST"))

print(r.execute_command('TS.ADD', 'ts_key', 1657265437756, 1, "LABELS", "label1", 1, "label2", 2, "DUPLICATE_POLICY", "LAST"))
print(r.execute_command('TS.ADD', 'ts_key', 1657265437757, 2, "LABELS", "label1", 1, "label2", 2, "DUPLICATE_POLICY", "LAST"))
print(r.execute_command('TS.ADD', 'ts_key', 1657265437754, 5, "LABELS", "label1", 1, "label2", 2, "DUPLICATE_POLICY", "LAST"))
print(r.execute_command('TS.ADD', 'ts_key', 1657265437759, 17, "LABELS", "label1", 3, "label2", 4, "DUPLICATE_POLICY", "LAST"))

print(ts.range("ts_key", "-", "+"))
print(ts.mget(["label1=1"], with_labels=True))
print(ts.mget(["label1=1"])) 

results in

True
created
1657265437756
1657265437757
1657265437754
1657265437759
[(1657265437754, 5.0), (1657265437756, 1.0), (1657265437757, 2.0), (1657265437759, 17.0)]
[]
[]

No labels. Is it a problem or am I doing anything wrong ?

Simoliv commented 3 weeks ago

Great somebody took a look.

Broken in 5.0.4, tried latest version also

>>> redis.VERSION
(5, 1, '0b6')
>>> 
gerzse commented 2 weeks ago

Hi @Simoliv ,

When you create the timeseries, you can't use kwargs. You must write it as:

r.execute_command('TS.CREATE', 'ts_key', 'labels', "label1", 1, "label2", 2, 'duplicate_policy', "LAST")

and then things start to work. Just like you did when adding entries to the time series.

Though the simpler code to create the series would be:

ts.create("ts_key", labels={"label1": "1", "label2": "2"})

Not sure if you are aware of this detail from the TS.ADD command documentation:

LABELS {label value}... is set of label-value pairs that represent metadata labels of the time series. Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See LABELS in TS.CREATE.

So, given that you pre-create your timeseries with labels, by the time you do TS.ADD you don't need to specify the labels. They would be useful if the timeseries would be missing and have to be created before adding.

Hope this helps.

Regards, Gabriel