Skip to content

Commit e062674

Browse files
authored
add in IncrByAutoTs and DecrByAutoTs functions with tests (#88)
1 parent 8a44943 commit e062674

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Diff for: client.go

+24
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ func (client *Client) IncrBy(key string, timestamp int64, value float64, options
373373
return redis.Int64(conn.Do(INCRBY_CMD, args...))
374374
}
375375

376+
// Creates a new sample that increments the latest sample's value with an auto timestamp
377+
func (client *Client) IncrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
378+
conn := client.Pool.Get()
379+
defer conn.Close()
380+
381+
args, err := AddCounterArgs(key, -1, value, options)
382+
if err != nil {
383+
return -1, err
384+
}
385+
return redis.Int64(conn.Do(INCRBY_CMD, args...))
386+
}
387+
376388
// Creates a new sample that decrements the latest sample's value
377389
func (client *Client) DecrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error) {
378390
conn := client.Pool.Get()
@@ -385,6 +397,18 @@ func (client *Client) DecrBy(key string, timestamp int64, value float64, options
385397
return redis.Int64(conn.Do(DECRBY_CMD, args...))
386398
}
387399

400+
// Creates a new sample that decrements the latest sample's value with auto timestamp
401+
func (client *Client) DecrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
402+
conn := client.Pool.Get()
403+
defer conn.Close()
404+
405+
args, err := AddCounterArgs(key, -1, value, options)
406+
if err != nil {
407+
return -1, err
408+
}
409+
return redis.Int64(conn.Do(DECRBY_CMD, args...))
410+
}
411+
388412
// Add counter args for command TS.INCRBY/TS.DECRBY
389413
func AddCounterArgs(key string, timestamp int64, value float64, options CreateOptions) (redis.Args, error) {
390414
args := redis.Args{key, value}

Diff for: client_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis_timeseries_go
22

33
import (
4+
"log"
45
"os"
56
"reflect"
67
"testing"
@@ -723,6 +724,20 @@ func TestNewClientFromPool(t *testing.T) {
723724
assert.Nil(t, err1)
724725
assert.Nil(t, err2)
725726
}
727+
728+
func TestIncrDecrByAutoTs(t *testing.T) {
729+
tkey := "Test:IncrDecrByAutoTs"
730+
err := client.FlushAll()
731+
assert.Nil(t, err)
732+
storedTimestamp1, _ := client.IncrByAutoTs(tkey, 101, CreateOptions{Uncompressed: false, Labels: map[string]string{}})
733+
time.Sleep(1 * time.Millisecond)
734+
log.Printf("YO: %+v\n", storedTimestamp1)
735+
storedTimestamp2, _ := client.DecrByAutoTs(tkey, 1, CreateOptions{Uncompressed: false, Labels: map[string]string{}})
736+
assert.True(t, storedTimestamp1 < storedTimestamp2)
737+
datapoint, _ := client.Get(tkey)
738+
assert.True(t, datapoint.Value == 100)
739+
}
740+
726741
func TestIncrDecrBy(t *testing.T) {
727742
err := client.FlushAll()
728743
assert.Nil(t, err)

0 commit comments

Comments
 (0)