Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RED-21500: Support automatic timestamps using ts.add key * value #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ int TSDB_add(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
double timestamp, value;
if ((RedisModule_StringToDouble(argv[3], &value) != REDISMODULE_OK))
return RedisModule_ReplyWithError(ctx,"TSDB: invalid value");

if ((RedisModule_StringToDouble(argv[2], &timestamp) != REDISMODULE_OK))
return RedisModule_ReplyWithError(ctx,"TSDB: invalid timestamp");

if ((RedisModule_StringToDouble(argv[2], &timestamp) != REDISMODULE_OK)) {
// if timestamp is "*", take current time (automatic timestamp)
if(RMUtil_StringEqualsC(argv[2], "*"))
timestamp = time(NULL);
else
return RedisModule_ReplyWithError(ctx, "TSDB: invalid timestamp");
}

Series *series = NULL;

Expand Down
9 changes: 9 additions & 0 deletions src/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,12 @@ def test_downsampling_rules(self):
# last time stamp should be the beginning of the last bucket
assert self._get_ts_info(r, 'tester_{}_{}'.format(rule, resolution))['lastTimestamp'] == \
(samples_count - 1) - (samples_count - 1) % resolution

def test_automatic_timestamp(self):
with self.redis() as r:
assert r.execute_command('TS.CREATE', 'tester')
curr_time = int(time.time())
r.execute_command('TS.ADD', 'tester', '*', 1)
result = r.execute_command('TS.RANGE', 'tester', 0, int(time.time()))
# test time difference is not more than 1 second
assert result[0][0] - curr_time <= 1