Skip to content

Commit eaa56b7

Browse files
authored
redis timeseries support (#1652)
1 parent 1f11f8c commit eaa56b7

File tree

9 files changed

+1590
-5
lines changed

9 files changed

+1590
-5
lines changed

redis/commands/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ def nativestr(x):
2323
def delist(x):
2424
"""Given a list of binaries, return the stringified version."""
2525
return [nativestr(obj) for obj in x]
26+
27+
28+
def parse_to_list(response):
29+
"""Optimistally parse the response to a list.
30+
"""
31+
res = []
32+
for item in response:
33+
try:
34+
res.append(int(item))
35+
except ValueError:
36+
res.append(nativestr(item))
37+
except TypeError:
38+
res.append(None)
39+
return res

redis/commands/json/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
# from typing import Optional
21
from json import JSONDecoder, JSONEncoder
32

4-
# # from redis.client import Redis
5-
63
from .helpers import bulk_of_jsons
74
from ..helpers import nativestr, delist
85
from .commands import JSONCommands
9-
# from ..feature import AbstractFeature
106

117

128
class JSON(JSONCommands):

redis/commands/redismodules.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,22 @@ def ft(self, index_name="idx"):
2727
try:
2828
modversion = self.loaded_modules['search']
2929
except IndexError:
30-
raise ModuleError("rejson is not a loaded in the redis instance.")
30+
raise ModuleError("search is not a loaded in the redis instance.")
3131

3232
from .search import Search
3333
s = Search(client=self, version=modversion, index_name=index_name)
3434
return s
35+
36+
def ts(self, index_name="idx"):
37+
"""Access the timeseries namespace, providing support for
38+
redis timeseries data.
39+
"""
40+
try:
41+
modversion = self.loaded_modules['timeseries']
42+
except IndexError:
43+
raise ModuleError("timeseries is not a loaded in "
44+
"the redis instance.")
45+
46+
from .timeseries import TimeSeries
47+
s = TimeSeries(client=self, version=modversion, index_name=index_name)
48+
return s

redis/commands/timeseries/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from redis.client import bool_ok
2+
3+
from .utils import (
4+
parse_range,
5+
parse_get,
6+
parse_m_range,
7+
parse_m_get,
8+
)
9+
from .info import TSInfo
10+
from ..helpers import parse_to_list
11+
from .commands import (
12+
ALTER_CMD,
13+
CREATE_CMD,
14+
CREATERULE_CMD,
15+
DELETERULE_CMD,
16+
DEL_CMD,
17+
GET_CMD,
18+
INFO_CMD,
19+
MGET_CMD,
20+
MRANGE_CMD,
21+
MREVRANGE_CMD,
22+
QUERYINDEX_CMD,
23+
RANGE_CMD,
24+
REVRANGE_CMD,
25+
TimeSeriesCommands,
26+
)
27+
28+
29+
class TimeSeries(TimeSeriesCommands):
30+
"""
31+
This class subclasses redis-py's `Redis` and implements RedisTimeSeries's
32+
commands (prefixed with "ts").
33+
The client allows to interact with RedisTimeSeries and use all of it's
34+
functionality.
35+
"""
36+
37+
def __init__(self, client=None, version=None, **kwargs):
38+
"""Create a new RedisTimeSeries client."""
39+
# Set the module commands' callbacks
40+
MODULE_CALLBACKS = {
41+
CREATE_CMD: bool_ok,
42+
ALTER_CMD: bool_ok,
43+
CREATERULE_CMD: bool_ok,
44+
DEL_CMD: int,
45+
DELETERULE_CMD: bool_ok,
46+
RANGE_CMD: parse_range,
47+
REVRANGE_CMD: parse_range,
48+
MRANGE_CMD: parse_m_range,
49+
MREVRANGE_CMD: parse_m_range,
50+
GET_CMD: parse_get,
51+
MGET_CMD: parse_m_get,
52+
INFO_CMD: TSInfo,
53+
QUERYINDEX_CMD: parse_to_list,
54+
}
55+
56+
self.client = client
57+
self.execute_command = client.execute_command
58+
self.MODULE_VERSION = version
59+
60+
for k in MODULE_CALLBACKS:
61+
self.client.set_response_callback(k, MODULE_CALLBACKS[k])

0 commit comments

Comments
 (0)