Skip to content

Commit

Permalink
add in IncrByAutoTs and DecrByAutoTs functions with tests (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
maguec authored Mar 17, 2021
1 parent 8a44943 commit e062674
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ func (client *Client) IncrBy(key string, timestamp int64, value float64, options
return redis.Int64(conn.Do(INCRBY_CMD, args...))
}

// Creates a new sample that increments the latest sample's value with an auto timestamp
func (client *Client) IncrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()

args, err := AddCounterArgs(key, -1, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(INCRBY_CMD, args...))
}

// Creates a new sample that decrements the latest sample's value
func (client *Client) DecrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
Expand All @@ -385,6 +397,18 @@ func (client *Client) DecrBy(key string, timestamp int64, value float64, options
return redis.Int64(conn.Do(DECRBY_CMD, args...))
}

// Creates a new sample that decrements the latest sample's value with auto timestamp
func (client *Client) DecrByAutoTs(key string, value float64, options CreateOptions) (int64, error) {
conn := client.Pool.Get()
defer conn.Close()

args, err := AddCounterArgs(key, -1, value, options)
if err != nil {
return -1, err
}
return redis.Int64(conn.Do(DECRBY_CMD, args...))
}

// Add counter args for command TS.INCRBY/TS.DECRBY
func AddCounterArgs(key string, timestamp int64, value float64, options CreateOptions) (redis.Args, error) {
args := redis.Args{key, value}
Expand Down
15 changes: 15 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis_timeseries_go

import (
"log"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -723,6 +724,20 @@ func TestNewClientFromPool(t *testing.T) {
assert.Nil(t, err1)
assert.Nil(t, err2)
}

func TestIncrDecrByAutoTs(t *testing.T) {
tkey := "Test:IncrDecrByAutoTs"
err := client.FlushAll()
assert.Nil(t, err)
storedTimestamp1, _ := client.IncrByAutoTs(tkey, 101, CreateOptions{Uncompressed: false, Labels: map[string]string{}})
time.Sleep(1 * time.Millisecond)
log.Printf("YO: %+v\n", storedTimestamp1)
storedTimestamp2, _ := client.DecrByAutoTs(tkey, 1, CreateOptions{Uncompressed: false, Labels: map[string]string{}})
assert.True(t, storedTimestamp1 < storedTimestamp2)
datapoint, _ := client.Get(tkey)
assert.True(t, datapoint.Value == 100)
}

func TestIncrDecrBy(t *testing.T) {
err := client.FlushAll()
assert.Nil(t, err)
Expand Down

0 comments on commit e062674

Please sign in to comment.