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

add in IncrByAutoTs and DecrByAutoTs functions with tests #88

Merged
merged 1 commit into from
Mar 17, 2021
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
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