redis_timeseries_go

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

license CircleCI GitHub issues Codecov GoDoc Go Report Card Total alerts

redistimeseries-go

Forum Discord

Go client for RedisTimeSeries (https://github.com/RedisTimeSeries/redistimeseries), based on redigo.

Client and ConnPool based on the work of dvirsky and mnunberg on https://github.com/RediSearch/redisearch-go

Installing

$ go get github.com/RedisTimeSeries/redistimeseries-go

Running tests

A simple test suite is provided, and can be run with:

$ go test

The tests expect a Redis server with the RedisTimeSeries module loaded to be available at localhost:6379

Example Code

package main 

import (
        "fmt"
        redistimeseries "github.com/RedisTimeSeries/redistimeseries-go"
)

func main() {
		// Connect to localhost with no password
        var client = redistimeseries.NewClient("localhost:6379", "nohelp", nil)
        var keyname = "mytest"
        _, haveit := client.Info(keyname)
        if haveit != nil {
			client.CreateKeyWithOptions(keyname, redistimeseries.DefaultCreateOptions)
			client.CreateKeyWithOptions(keyname+"_avg", redistimeseries.DefaultCreateOptions)
			client.CreateRule(keyname, redistimeseries.AvgAggregation, 60, keyname+"_avg")
        }
		// Add sample with timestamp from server time and value 100
        // TS.ADD mytest * 100 
        _, err := client.AddAutoTs(keyname, 100)
        if err != nil {
                fmt.Println("Error:", err)
        }
}

Supported RedisTimeSeries Commands

Command Recommended API and godoc
TS.CREATE CreateKeyWithOptions
TS.ALTER AlterKeyWithOptions
TS.ADD
TS.MADD MultiAdd
TS.INCRBY/TS.DECRBY IncrBy / DecrBy
TS.CREATERULE CreateRule
TS.DELETERULE DeleteRule
TS.RANGE RangeWithOptions
TS.REVRANGE ReverseRangeWithOptions
TS.MRANGE MultiRangeWithOptions
TS.MREVRANGE MultiReverseRangeWithOptions
TS.GET Get
TS.MGET
TS.INFO Info
TS.QUERYINDEX QueryIndex

License

redistimeseries-go is distributed under the Apache-2 license - see LICENSE

Documentation

Index

Examples

Constants

View Source
const (
	CREATE_CMD     string = "TS.CREATE"
	ALTER_CMD      string = "TS.ALTER"
	ADD_CMD        string = "TS.ADD"
	MADD_CMD       string = "TS.MADD"
	INCRBY_CMD     string = "TS.INCRBY"
	DECRBY_CMD     string = "TS.DECRBY"
	CREATERULE_CMD string = "TS.CREATERULE"
	DELETERULE_CMD string = "TS.DELETERULE"
	RANGE_CMD      string = "TS.RANGE"
	REVRANGE_CMD   string = "TS.REVRANGE"
	MRANGE_CMD     string = "TS.MRANGE"
	MREVRANGE_CMD  string = "TS.MREVRANGE"
	GET_CMD        string = "TS.GET"
	MGET_CMD       string = "TS.MGET"
	INFO_CMD       string = "TS.INFO"
	QUERYINDEX_CMD string = "TS.QUERYINDEX"
	DEL_CMD        string = "DEL"
	TS_DEL_CMD     string = "TS.DEL"
)
View Source
const TimeRangeFull = int64(-1)
View Source
const TimeRangeMaximum = math.MaxInt64
View Source
const TimeRangeMinimum = 0

Variables

View Source
var DefaultCreateOptions = CreateOptions{
	Uncompressed:    false,
	RetentionMSecs:  0,
	Labels:          map[string]string{},
	ChunkSize:       0,
	DuplicatePolicy: "",
}
View Source
var DefaultMultiGetOptions = MultiGetOptions{
	WithLabels: false,
}

MultiGetOptions are the default options for querying across multiple time-series

View Source
var DefaultMultiRangeOptions = MultiRangeOptions{
	AggType:          "",
	TimeBucket:       -1,
	Count:            -1,
	WithLabels:       false,
	SelectedLabels:   []string{},
	Align:            -1,
	FilterByTs:       []int64{},
	FilterByValueMin: nil,
	FilterByValueMax: nil,
	GroupBy:          "",
	Reduce:           "",
}

MultiRangeOptions are the default options for querying across multiple time-series

View Source
var DefaultRangeOptions = *NewRangeOptions()

DefaultRangeOptions are the default options for querying across a time-series range

Functions

func AddCounterArgs

func AddCounterArgs(key string, timestamp int64, value float64, options CreateOptions) (redis.Args, error)

Add counter args for command TS.INCRBY/TS.DECRBY

func MakeStringPtr

func MakeStringPtr(s string) *string

Helper function to create a string pointer from a string literal. Useful for calls to NewClient with an auth pass that is known at compile time.

func ParseLabels

func ParseLabels(res interface{}) (labels map[string]string, err error)

Types

type AggregationType

type AggregationType string
const (
	AvgAggregation   AggregationType = "AVG"
	SumAggregation   AggregationType = "SUM"
	MinAggregation   AggregationType = "MIN"
	MaxAggregation   AggregationType = "MAX"
	CountAggregation AggregationType = "COUNT"
	FirstAggregation AggregationType = "FIRST"
	LastAggregation  AggregationType = "LAST"
	StdPAggregation  AggregationType = "STD.P"
	StdSAggregation  AggregationType = "STD.S"
	VarPAggregation  AggregationType = "VAR.P"
	VarSAggregation  AggregationType = "VAR.S"
)

type Client

type Client struct {
	Pool ConnPool
	Name string
}

Client is an interface to time series redis commands

func NewClient

func NewClient(addr, name string, authPass *string) *Client

NewClient creates a new client connecting to the redis host, and using the given name as key prefix. Addr can be a single host:port pair, or a comma separated list of host:port,host:port... In the case of multiple hosts we create a multi-pool and select connections at random

func NewClientFromPool

func NewClientFromPool(pool *redis.Pool, name string) *Client

NewClientFromPool creates a new Client with the given pool and client name

Example

exemplifies the NewClientFromPool function

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	client.Add("ts", 1, 5)
	datapoints, _ := client.RangeWithOptions("ts", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Println(datapoints[0])
}
Output:

{1 5}

func (*Client) Add

func (client *Client) Add(key string, timestamp int64, value float64) (storedTimestamp int64, err error)

Add - Append (or create and append) a new sample to the series args: key - time series key name timestamp - time of value value - value

Example

Exemplifies the usage of Add function with a time-series created with the default options nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	labels := map[string]string{
		"machine": "machine-1",
		"az":      "us-west-2",
	}
	// get the default options and set the time-serie labels
	options := redistimeseries.DefaultCreateOptions
	options.Labels = labels

	client.CreateKeyWithOptions("time-serie-add", options)

	client.Add("time-serie-add", 1, 2.0)
	client.Add("time-serie-add", 2, 4.0)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-serie-add")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=2 value=4.000000
Example (BackFilling)

Exemplifies the usage of Add function for back filling - Add samples to a time series where the time of the sample is older than the newest sample in the series nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// get the default options and set the time-serie labels
	options := redistimeseries.DefaultCreateOptions

	client.CreateKeyWithOptions("time-serie-add-back-filling", options)

	client.Add("time-serie-add-back-filling", 1, 1)
	client.Add("time-serie-add-back-filling", 2, 1)
	client.Add("time-serie-add-back-filling", 4, 1)
	// Add sample with timestamp ( 3 ) where the time of the sample is older than the newest sample in the series ( 4 )
	client.Add("time-serie-add-back-filling", 3, 1)

	// Retrieve the time-series data points
	datapoints, _ := client.RangeWithOptions("time-serie-add-back-filling", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{1 1} {2 1} {3 1} {4 1}]
Example (DuplicateDatapointsLastDuplicatePolicy)

Exemplifies the usage of Add function with a duplicate policy of LAST (override with latest value) nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// get the default options and set the duplicate policy to LAST (override with latest value)
	options := redistimeseries.DefaultCreateOptions
	options.DuplicatePolicy = redistimeseries.LastDuplicatePolicy

	client.CreateKeyWithOptions("time-series-add-duplicate-last", options)

	client.Add("time-series-add-duplicate-last", 1, 1.0)
	client.Add("time-series-add-duplicate-last", 1, 10.0)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-series-add-duplicate-last")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=1 value=10.000000
Example (MinDuplicatePolicy)

Exemplifies the usage of Add function with a duplicate policy of MIN (override with min value) nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// get the default options and set the duplicate policy to MIN (override with latest value)
	options := redistimeseries.DefaultCreateOptions
	options.DuplicatePolicy = redistimeseries.MinDuplicatePolicy

	client.CreateKeyWithOptions("time-series-add-duplicate-min", options)

	client.Add("time-series-add-duplicate-min", 1, 1.0)
	client.Add("time-series-add-duplicate-min", 1, 10.0)

	// Retrieve the minimal data point
	minDatapoint, _ := client.Get("time-series-add-duplicate-min")

	fmt.Printf("Minimal datapoint: timestamp=%d value=%f\n", minDatapoint.Timestamp, minDatapoint.Value)
}
Output:

Minimal datapoint: timestamp=1 value=1.000000

func (*Client) AddAutoTs

func (client *Client) AddAutoTs(key string, value float64) (storedTimestamp int64, err error)

AddAutoTs - Append (or create and append) a new sample to the series, with DB automatic timestamp (using the system clock) args: key - time series key name value - value

func (*Client) AddAutoTsWithOptions

func (client *Client) AddAutoTsWithOptions(key string, value float64, options CreateOptions) (storedTimestamp int64, err error)

AddAutoTsWithOptions - Append (or create and append) a new sample to the series, with the specified CreateOptions and DB automatic timestamp (using the system clock) args: key - time series key name value - value options - define options for create key on add

func (*Client) AddWithOptions

func (client *Client) AddWithOptions(key string, timestamp int64, value float64, options CreateOptions) (storedTimestamp int64, err error)

AddWithOptions - Append (or create and append) a new sample to the series, with the specified CreateOptions args: key - time series key name timestamp - time of value value - value options - define options for create key on add

Example

Exemplifies the usage of AddWithOptions function with the default options and some additional time-serie labels nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client")

	labels := map[string]string{
		"machine": "machine-1",
		"az":      "us-west-2",
	}
	// get the default options and set the time-serie labels
	options := redistimeseries.DefaultCreateOptions
	options.Labels = labels

	client.AddWithOptions("time-series-example-add", 1, 1, options)
	client.AddWithOptions("time-series-example-add", 2, 2, options)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-series-example-add")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=2 value=2.000000
Example (BackFilling)

Exemplifies the usage of AddWithOptions function for back filling - Add samples to a time series where the time of the sample is older than the newest sample in the series nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client")

	labels := map[string]string{
		"machine": "machine-1",
		"az":      "us-west-2",
	}

	// use the default options
	options := redistimeseries.DefaultCreateOptions
	options.Labels = labels

	client.AddWithOptions("time-series-example-back-filling", 1, 1, options)
	client.AddWithOptions("time-series-example-back-filling", 2, 1, options)
	client.AddWithOptions("time-series-example-back-filling", 4, 1, options)
	// Add sample with timestamp ( 3 ) where the time of the sample is older than the newest sample in the series ( 4 )
	client.AddWithOptions("time-series-example-back-filling", 3, 1, options)

	// Retrieve the time-series data points
	datapoints, _ := client.RangeWithOptions("time-series-example-back-filling", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{1 1} {2 1} {3 1} {4 1}]
Example (DuplicateDatapointsLastDuplicatePolicy)

Exemplifies the usage of AddWithOptions function with a duplicate policy of LAST (override with latest value) and with MIN (override with minimal value) nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client")

	labels := map[string]string{
		"machine": "machine-1",
		"az":      "us-west-2",
	}

	// get the default options and set the duplicate policy to LAST (override with latest value)
	options := redistimeseries.DefaultCreateOptions
	options.DuplicatePolicy = redistimeseries.LastDuplicatePolicy
	options.Labels = labels

	client.AddWithOptions("time-series-example-duplicate", 1, 1, options)
	client.AddWithOptions("time-series-example-duplicate", 1, 10, options)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-series-example-duplicate")

	// change the duplicate policy to MIN
	options.DuplicatePolicy = redistimeseries.MinDuplicatePolicy

	// The current value will not be overridden because the new added value is higher
	client.AddWithOptions("time-series-example-duplicate", 1, 15, options)

	// Retrieve the latest data point
	minDatapoint, _ := client.Get("time-series-example-duplicate")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
	fmt.Printf("Minimal datapoint: timestamp=%d value=%f\n", minDatapoint.Timestamp, minDatapoint.Value)
}
Output:

Latest datapoint: timestamp=1 value=10.000000
Minimal datapoint: timestamp=1 value=10.000000
Example (DuplicateDatapointsMaxDuplicatePolicy)

Exemplifies the usage of AddWithOptions function with a duplicate policy of MAX (only override if the value is higher than the existing value) nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client")

	labels := map[string]string{
		"machine": "machine-1",
		"az":      "us-west-2",
	}

	// get the default options and set the duplicate policy to MAX (only override if the value is higher than the existing value)
	options := redistimeseries.DefaultCreateOptions
	options.DuplicatePolicy = redistimeseries.MaxDuplicatePolicy
	options.Labels = labels

	client.AddWithOptions("time-series-example-duplicate-max", 1, 10.0, options)

	// this should not override the value given that the previous one ( 10.0 ) is greater than the new one we're trying to add
	client.AddWithOptions("time-series-example-duplicate-max", 1, 5.0, options)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-series-example-duplicate-max")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=1 value=10.000000

func (*Client) AddWithRetention

func (client *Client) AddWithRetention(key string, timestamp int64, value float64, duration int64) (storedTimestamp int64, err error)

AddWithRetention - append a new value to the series with a duration args: key - time series key name timestamp - time of value value - value duration - value Deprecated: This function has been deprecated, use AddWithOptions instead

func (*Client) AggMultiRange

func (client *Client) AggMultiRange(fromTimestamp int64, toTimestamp int64, aggType AggregationType,
	bucketSizeSec int, filters ...string) (ranges []Range, err error)

AggMultiRange - Query a timestamp range across multiple time-series by filters. args: fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. aggType - aggregation type bucketSizeSec - time bucket for aggregation filters - list of filters e.g. "a=bb", "b!=aa" Deprecated: This function has been deprecated, use MultiRangeWithOptions instead

func (*Client) AggRange

func (client *Client) AggRange(key string, fromTimestamp int64, toTimestamp int64, aggType AggregationType,
	bucketSizeSec int) (dataPoints []DataPoint, err error)

AggRange - aggregation over a ranged query args: key - time series key name fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. aggType - aggregation type bucketSizeSec - time bucket for aggregation Deprecated: This function has been deprecated, use RangeWithOptions instead

func (*Client) AlterKeyWithOptions

func (client *Client) AlterKeyWithOptions(key string, options CreateOptions) (err error)

Update the retention, labels of an existing key. The parameters are the same as TS.CREATE.

func (*Client) CreateKey

func (client *Client) CreateKey(key string, retentionTime time.Duration) (err error)

CreateKey create a new time-series Deprecated: This function has been deprecated, use CreateKeyWithOptions instead

func (*Client) CreateKeyWithOptions

func (client *Client) CreateKeyWithOptions(key string, options CreateOptions) (err error)
Example

Exemplifies the usage of CreateKeyWithOptions function with a duplicate policy of LAST (override with latest value) nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	client.CreateKeyWithOptions("time-serie-last-policy", redistimeseries.CreateOptions{DuplicatePolicy: redistimeseries.LastDuplicatePolicy})

	// Add duplicate timestamp just to ensure it obeys the duplicate policy
	client.Add("time-serie-last-policy", 4, 2.0)
	client.Add("time-serie-last-policy", 4, 10.0)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-serie-last-policy")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=4 value=10.000000
Example (RetentionTime)

Exemplifies the usage of CreateKeyWithOptions function with a retention time of 1 hour nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
	"time"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client")

	// get the default options and set the retention time
	options := redistimeseries.DefaultCreateOptions
	options.RetentionMSecs = time.Hour

	client.CreateKeyWithOptions("time-series-example-retention-time", options)

	client.Add("time-series-example-retention-time", 1, 1)
	client.Add("time-series-example-retention-time", 2, 2)

	// Retrieve the latest data point
	latestDatapoint, _ := client.Get("time-series-example-retention-time")

	fmt.Printf("Latest datapoint: timestamp=%d value=%f\n", latestDatapoint.Timestamp, latestDatapoint.Value)
}
Output:

Latest datapoint: timestamp=2 value=2.000000

func (*Client) CreateRule

func (client *Client) CreateRule(sourceKey string, aggType AggregationType, bucketSizeMSec uint, destinationKey string) (err error)

CreateRule - create a compaction rule args: sourceKey - key name for source time series aggType - AggregationType bucketSizeMSec - Time bucket for aggregation in milliseconds destinationKey - key name for destination time series

func (*Client) DecrBy

func (client *Client) DecrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error)

Creates a new sample that decrements the latest sample's value

func (*Client) DecrByAutoTs

func (client *Client) DecrByAutoTs(key string, value float64, options CreateOptions) (int64, error)

Creates a new sample that decrements the latest sample's value with auto timestamp

func (*Client) DeleteRange

func (client *Client) DeleteRange(key string, fromTimestamp int64, toTimestamp int64) (totalDeletedSamples int64, err error)

DeleteRange - Delete data points for a given timeseries and interval range in the form of start and end delete timestamps. Returns the total deleted datapoints. args:

key - time series key name
fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp.
toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp.
Example

exemplifies the usage of DeleteRange function

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// Create serie and add datapoint
	client.Add("ts", 1, 5)
	client.Add("ts", 10, 15.5)
	client.Add("ts", 20, 25)

	// Query the serie
	datapoints, _ := client.RangeWithOptions("ts", redistimeseries.TimeRangeMinimum, redistimeseries.TimeRangeMaximum, redistimeseries.DefaultRangeOptions)
	fmt.Println("Before deleting datapoints: ", datapoints)

	// Delete datapoints from timestamp 1 until 10 ( inclusive )
	totalDeletedSamples, _ := client.DeleteRange("ts", 1, 10)
	fmt.Printf("Deleted %d datapoints\n", totalDeletedSamples)

	// Query the serie after deleting from timestamp 1 until 10 ( inclusive )
	datapoints, _ = client.RangeWithOptions("ts", redistimeseries.TimeRangeMinimum, redistimeseries.TimeRangeMaximum, redistimeseries.DefaultRangeOptions)
	fmt.Println("After deleting datapoints: ", datapoints)

}
Output:

Before deleting datapoints:  [{1 5} {10 15.5} {20 25}]
Deleted 2 datapoints
After deleting datapoints:  [{20 25}]

func (*Client) DeleteRule

func (client *Client) DeleteRule(sourceKey string, destinationKey string) (err error)

DeleteRule - delete a compaction rule args: sourceKey - key name for source time series destinationKey - key name for destination time series

func (*Client) DeleteSerie

func (client *Client) DeleteSerie(key string) (err error)

DeleteSerie - deletes series given the time series key name. This API is sugar coating on top of redis DEL command args: key - time series key name

Example

exemplifies the usage of DeleteSerie function

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// Create serie and add datapoint
	client.Add("ts", 1, 5)

	// Query the serie
	datapoints, _ := client.RangeWithOptions("ts", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Println(datapoints[0])
	
Output:

func (*Client) Get

func (client *Client) Get(key string) (dataPoint *DataPoint,
	err error)

Get - Get the last sample of a time-series. args: key - time-series key name

func (*Client) IncrBy

func (client *Client) IncrBy(key string, timestamp int64, value float64, options CreateOptions) (int64, error)

Creates a new sample that increments the latest sample's value

func (*Client) IncrByAutoTs

func (client *Client) IncrByAutoTs(key string, value float64, options CreateOptions) (int64, error)

Creates a new sample that increments the latest sample's value with an auto timestamp

func (*Client) Info

func (client *Client) Info(key string) (res KeyInfo, err error)

Returns information and statistics on the time-series. args: key - time-series key name

func (*Client) MultiAdd

func (client *Client) MultiAdd(samples ...Sample) (timestamps []interface{}, err error)

Append new samples to a list of series.

Example

Exemplifies the usage of MultiAdd.

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
	"log"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}

	err := client.CreateKeyWithOptions("timeserie-1", redistimeseries.CreateOptions{Labels: labels1})
	if err != nil {
		log.Fatal(err)
	}
	err = client.CreateKeyWithOptions("timeserie-2", redistimeseries.CreateOptions{Labels: labels2})
	if err != nil {
		log.Fatal(err)
	}

	// Adding multiple datapoints to multiple series
	datapoints := []redistimeseries.Sample{
		{"timeserie-1", redistimeseries.DataPoint{1, 10.5}},
		{"timeserie-1", redistimeseries.DataPoint{2, 40.5}},
		{"timeserie-2", redistimeseries.DataPoint{1, 60.5}},
	}
	timestamps, _ := client.MultiAdd(datapoints...)

	fmt.Printf("Example adding multiple datapoints to multiple series. Added timestamps: %v\n", timestamps)

	// Adding multiple datapoints to the same serie
	datapointsSameSerie := []redistimeseries.Sample{
		{"timeserie-1", redistimeseries.DataPoint{3, 10.5}},
		{"timeserie-1", redistimeseries.DataPoint{4, 40.5}},
		{"timeserie-1", redistimeseries.DataPoint{5, 60.5}},
	}
	timestampsSameSerie, _ := client.MultiAdd(datapointsSameSerie...)

	fmt.Printf("Example of adding multiple datapoints to the same serie. Added timestamps: %v\n", timestampsSameSerie)

}
Output:

Example adding multiple datapoints to multiple series. Added timestamps: [1 2 1]
Example of adding multiple datapoints to the same serie. Added timestamps: [3 4 5]

func (*Client) MultiGet

func (client *Client) MultiGet(filters ...string) (ranges []Range, err error)

MultiGet - Get the last sample across multiple time-series, matching the specific filters. args: filters - list of filters e.g. "a=bb", "b!=aa"

func (*Client) MultiGetWithOptions

func (client *Client) MultiGetWithOptions(multiGetOptions MultiGetOptions, filters ...string) (ranges []Range, err error)

MultiGetWithOptions - Get the last samples matching the specific filters. args: multiGetOptions - MultiGetOptions options. You can use the default DefaultMultiGetOptions filters - list of filters e.g. "a=bb", "b!=aa"

Example

Exemplifies the usage of MultiGetWithOptions function while using the default MultiGetOptions and while using user defined MultiGetOptions.

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiGetWithOptions(redistimeseries.DefaultMultiGetOptions, "az=us-east-1")

	rangesWithLabels, _ := client.MultiGetWithOptions(*redistimeseries.NewMultiGetOptions().SetWithLabels(true), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
	fmt.Printf("Ranges with labels: %v\n", rangesWithLabels)

}
Output:

Ranges: [{time-serie-1 map[] [{4 2}]} {time-serie-2 map[] [{4 10}]}]
Ranges with labels: [{time-serie-1 map[az:us-east-1 machine:machine-1] [{4 2}]} {time-serie-2 map[az:us-east-1 machine:machine-2] [{4 10}]}]

func (*Client) MultiRangeWithOptions

func (client *Client) MultiRangeWithOptions(fromTimestamp int64, toTimestamp int64, mrangeOptions MultiRangeOptions, filters ...string) (ranges []Range, err error)

MultiRangeWithOptions - Query a timestamp range across multiple time-series by filters. args: fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. mrangeOptions - MultiRangeOptions options. You can use the default DefaultMultiRangeOptions filters - list of filters e.g. "a=bb", "b!=aa"

Example

nolint Exemplifies the usage of MultiRangeWithOptions function.

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure clean DB
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiRangeWithOptions(1, 10, redistimeseries.DefaultMultiRangeOptions, "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{2 1} {4 2}]} {time-serie-2 map[] [{1 5} {4 10}]}]
Example (FilterByTs)

Exemplifies the usage of MultiRangeWithOptions function, filtering the result by specific timestamps nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure clean DB
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetFilterByTs([]int64{1, 2}), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{2 1}]} {time-serie-2 map[] [{1 5}]}]
Example (FilterByValue)

Exemplifies the usage of MultiRangeWithOptions function, filtering the result by value using minimum and maximum. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 2.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetFilterByValue(1, 5), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{2 1} {4 2}]} {time-serie-2 map[] [{1 2}]}]
Example (GroupByReduce)

nolint Exemplifies the usage of MultiRangeWithOptions function. grouping multiple time-series

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure clean DB
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
		"team":    "team-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
		"team":    "team-2",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	labels3 := map[string]string{
		"machine": "machine-3",
		"az":      "us-east-1",
		"team":    "team-2",
	}
	client.AddWithOptions("time-serie-3", 1, 55.0, redistimeseries.CreateOptions{Labels: labels3})
	client.Add("time-serie-3", 4, 99.0)

	// Find out the total resources usage by team
	ranges, _ := client.MultiRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetWithLabels(true).SetGroupByReduce("team", redistimeseries.SumReducer), "az=us-east-1")

	fmt.Printf("Sum of usage by team: %v\n", ranges)
}
Output:

Sum of usage by team: [{team=team-1 map[__reducer__:sum __source__:time-serie-1 team:team-1] [{2 1} {4 2}]} {team=team-2 map[__reducer__:sum __source__:time-serie-2,time-serie-3 team:team-2] [{1 60} {4 109}]}]
Example (SelectedLabels)

Exemplifies the usage of MultiRangeWithOptions function, filtering the returned labels. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine":  "machine-1",
		"team":     "SF-1",
		"location": "SF",
		"az":       "us-east-1",
	}
	client.AddWithOptions("selected-labels-ex-time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("selected-labels-ex-time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine":  "machine-2",
		"team":     "NY-1",
		"location": "NY",
		"az":       "us-east-1",
	}
	client.AddWithOptions("selected-labels-ex-time-serie-2", 1, 10.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("selected-labels-ex-time-serie-2", 4, 15.0)

	ranges, _ := client.MultiRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetSelectedLabels([]string{"az", "location"}), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{selected-labels-ex-time-serie-1 map[az:us-east-1 location:SF] [{2 1} {4 2}]} {selected-labels-ex-time-serie-2 map[az:us-east-1 location:NY] [{1 10} {4 15}]}]

func (*Client) MultiReverseRangeWithOptions

func (client *Client) MultiReverseRangeWithOptions(fromTimestamp int64, toTimestamp int64, mrangeOptions MultiRangeOptions, filters ...string) (ranges []Range, err error)

MultiReverseRangeWithOptions - Query a timestamp range across multiple time-series by filters, in reverse direction. args: fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. mrangeOptions - MultiRangeOptions options. You can use the default DefaultMultiRangeOptions filters - list of filters e.g. "a=bb", "b!=aa"

Example

Exemplifies the usage of MultiReverseRangeWithOptions function. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiReverseRangeWithOptions(1, 10, redistimeseries.DefaultMultiRangeOptions, "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{4 2} {2 1}]} {time-serie-2 map[] [{4 10} {1 5}]}]
Example (FilterByTs)

Exemplifies the usage of MultiReverseRangeWithOptions function, filtering the result by specific timestamps nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 5.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiReverseRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetFilterByTs([]int64{1, 2}), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{2 1}]} {time-serie-2 map[] [{1 5}]}]
Example (FilterByValue)

Exemplifies the usage of MultiReverseRangeWithOptions function, filtering the result by value using minimum and maximum. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine": "machine-1",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine": "machine-2",
		"az":      "us-east-1",
	}
	client.AddWithOptions("time-serie-2", 1, 2.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("time-serie-2", 4, 10.0)

	ranges, _ := client.MultiReverseRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetFilterByValue(1, 5), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{time-serie-1 map[] [{4 2} {2 1}]} {time-serie-2 map[] [{1 2}]}]
Example (SelectedLabels)

Exemplifies the usage of MultiReverseRangeWithOptions function, filtering the returned labels. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")

	// ensure the DB is empty
	client.FlushAll()

	labels1 := map[string]string{
		"machine":  "machine-1",
		"team":     "SF-1",
		"location": "SF",
		"az":       "us-east-1",
	}
	client.AddWithOptions("selected-labels-ex-time-serie-1", 2, 1.0, redistimeseries.CreateOptions{Labels: labels1})
	client.Add("selected-labels-ex-time-serie-1", 4, 2.0)

	labels2 := map[string]string{
		"machine":  "machine-2",
		"team":     "NY-1",
		"location": "NY",
		"az":       "us-east-1",
	}
	client.AddWithOptions("selected-labels-ex-time-serie-2", 1, 10.0, redistimeseries.CreateOptions{Labels: labels2})
	client.Add("selected-labels-ex-time-serie-2", 4, 15.0)

	ranges, _ := client.MultiReverseRangeWithOptions(1, 10, *redistimeseries.NewMultiRangeOptions().SetSelectedLabels([]string{"az", "location"}), "az=us-east-1")

	fmt.Printf("Ranges: %v\n", ranges)
}
Output:

Ranges: [{selected-labels-ex-time-serie-1 map[az:us-east-1 location:SF] [{4 2} {2 1}]} {selected-labels-ex-time-serie-2 map[az:us-east-1 location:NY] [{4 15} {1 10}]}]

func (*Client) QueryIndex

func (client *Client) QueryIndex(filters ...string) (keys []string, err error)

Get all the keys matching the filter list.

func (*Client) Range

func (client *Client) Range(key string, fromTimestamp int64, toTimestamp int64) (dataPoints []DataPoint, err error)

Range - ranged query args: key - time series key name fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. Deprecated: This function has been deprecated, use RangeWithOptions instead

func (*Client) RangeWithOptions

func (client *Client) RangeWithOptions(key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) (dataPoints []DataPoint, err error)

RangeWithOptions - Query a timestamp range on a specific time-series args: key - time-series key name fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. rangeOptions - RangeOptions options. You can use the default DefaultRangeOptions

Example

Exemplifies the usage of RangeWithOptions function nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-1", int64(ts), float64(ts))
	}

	datapoints, _ := client.RangeWithOptions("ts-1", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{1 1} {2 2} {3 3} {4 4} {5 5} {6 6} {7 7} {8 8} {9 9}]
Example (AggregationAlign)

Exemplifies the usage of RangeWithOptions function, while changing the reference timestamp on which a bucket is defined. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-1", int64(ts), float64(ts))
	}

	datapoints, _ := client.RangeWithOptions("ts-1", 0, 1000, *redistimeseries.NewRangeOptions().SetAggregation(redistimeseries.CountAggregation, 2).SetAlign(1))
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{1 2} {3 2} {5 2} {7 2} {9 1}]
Example (AggregationMax)

Exemplifies the usage of RangeWithOptions function, while changing the reference timestamp on which a bucket is defined. nolint:errcheck

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-1", int64(ts), float64(ts))
	}

	datapoints, _ := client.RangeWithOptions("ts-1", 0, 1000, *redistimeseries.NewRangeOptions().SetAggregation(redistimeseries.MaxAggregation, 5))
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{0 4} {5 9}]

func (*Client) ReverseRangeWithOptions

func (client *Client) ReverseRangeWithOptions(key string, fromTimestamp int64, toTimestamp int64, rangeOptions RangeOptions) (dataPoints []DataPoint, err error)

ReverseRangeWithOptions - Query a timestamp range on a specific time-series in reverse order args: key - time-series key name fromTimestamp - start of range. You can use TimeRangeMinimum to express the minimum possible timestamp. toTimestamp - end of range. You can use TimeRangeFull or TimeRangeMaximum to express the maximum possible timestamp. rangeOptions - RangeOptions options. You can use the default DefaultRangeOptions

Example

nolint Exemplifies the usage of ReverseRangeWithOptions function

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-2", int64(ts), float64(ts))
	}

	datapoints, _ := client.ReverseRangeWithOptions("ts-2", 0, 1000, redistimeseries.DefaultRangeOptions)
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{9 9} {8 8} {7 7} {6 6} {5 5} {4 4} {3 3} {2 2} {1 1}]
Example (FilterByTs)

nolint Exemplifies the usage of ReverseRangeWithOptions function while filtering by timestamp

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-2", int64(ts), float64(ts))
	}

	datapoints, _ := client.ReverseRangeWithOptions("ts-2", 0, 1000, *redistimeseries.NewRangeOptions().SetFilterByTs([]int64{1, 2, 3, 4, 5}))
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{5 5} {4 4} {3 3} {2 2} {1 1}]
Example (FilterByValue)

nolint Exemplifies the usage of ReverseRangeWithOptions function while filtering value

package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"

	redistimeseries "github.com/temphiza/redistimeseries-go"
)

func main() {
	host := "localhost:6379"
	password := ""
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
	for ts := 1; ts < 10; ts++ {
		client.Add("ts-2", int64(ts), float64(ts))
	}

	datapoints, _ := client.ReverseRangeWithOptions("ts-2", 0, 1000, *redistimeseries.NewRangeOptions().SetFilterByValue(5, 50))
	fmt.Printf("Datapoints: %v\n", datapoints)
}
Output:

Datapoints: [{9 9} {8 8} {7 7} {6 6} {5 5}]

type ConnPool

type ConnPool interface {
	Get() redis.Conn
	Close() error
}

type CreateOptions

type CreateOptions struct {
	Uncompressed    bool
	RetentionMSecs  time.Duration
	Labels          map[string]string
	ChunkSize       int64
	DuplicatePolicy DuplicatePolicyType
}

CreateOptions are a direct mapping to the options provided when creating a new time-series Check https://oss.redislabs.com/redistimeseries/1.4/commands/#tscreate for a detailed description

func (*CreateOptions) Serialize

func (options *CreateOptions) Serialize(args []interface{}) (result []interface{}, err error)

Serialize options to args Deprecated: This function has been deprecated given that DUPLICATE_POLICY and ON_DUPLICATE depend upon the issuing command, use SerializeSeriesOptions instead

func (*CreateOptions) SerializeSeriesOptions

func (options *CreateOptions) SerializeSeriesOptions(cmd string, args []interface{}) (result []interface{}, err error)

Serialize options to args. Given that DUPLICATE_POLICY and ON_DUPLICATE depend upon the issuing command we need to specify the command for which we are generating the args for

type DataPoint

type DataPoint struct {
	Timestamp int64
	Value     float64
}

func NewDataPoint

func NewDataPoint(timestamp int64, value float64) *DataPoint

func ParseDataPoint

func ParseDataPoint(rawDataPoint interface{}) (dataPoint *DataPoint, err error)

func ParseDataPoints

func ParseDataPoints(info interface{}) (dataPoints []DataPoint, err error)

type DuplicatePolicyType

type DuplicatePolicyType string
const (
	BlockDuplicatePolicy DuplicatePolicyType = "block" // an error will occur for any out of order sample
	FirstDuplicatePolicy DuplicatePolicyType = "first" // ignore the new value
	LastDuplicatePolicy  DuplicatePolicyType = "last"  // override with latest value
	MinDuplicatePolicy   DuplicatePolicyType = "min"   // only override if the value is lower than the existing value
	MaxDuplicatePolicy   DuplicatePolicyType = "max"   // only override if the value is higher than the existing value
)

Check https://oss.redislabs.com/redistimeseries/configuration/#duplicate_policy for more inforamtion about duplicate policies

type KeyInfo

type KeyInfo struct {
	ChunkCount         int64
	MaxSamplesPerChunk int64 // As of RedisTimeseries >= v1.4 MaxSamplesPerChunk is deprecated in favor of ChunkSize
	ChunkSize          int64
	FirstTimestamp     int64
	LastTimestamp      int64
	RetentionTime      int64
	Rules              []Rule
	Labels             map[string]string
	DuplicatePolicy    DuplicatePolicyType // Duplicate sample policy
}

func ParseInfo

func ParseInfo(result interface{}, err error) (info KeyInfo, outErr error)

type MultiGetOptions

type MultiGetOptions struct {
	WithLabels bool
}

MultiGetOptions represent the options for querying across multiple time-series

func NewMultiGetOptions

func NewMultiGetOptions() *MultiGetOptions

func (*MultiGetOptions) SetWithLabels

func (mgetopts *MultiGetOptions) SetWithLabels(value bool) *MultiGetOptions

type MultiHostPool

type MultiHostPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewMultiHostPool

func NewMultiHostPool(hosts []string, authPass *string) *MultiHostPool

func (*MultiHostPool) Close

func (p *MultiHostPool) Close() (err error)

func (*MultiHostPool) Get

func (p *MultiHostPool) Get() redis.Conn

type MultiRangeOptions

type MultiRangeOptions struct {
	AggType          AggregationType
	TimeBucket       int
	Count            int64
	WithLabels       bool
	SelectedLabels   []string
	Align            int64
	FilterByTs       []int64
	FilterByValueMin *float64
	FilterByValueMax *float64
	GroupBy          string
	Reduce           ReducerType
}

MultiRangeOptions represent the options for querying across multiple time-series

func NewMultiRangeOptions

func NewMultiRangeOptions() *MultiRangeOptions

func (*MultiRangeOptions) SetAggregation

func (mrangeopts *MultiRangeOptions) SetAggregation(aggType AggregationType, timeBucket int) *MultiRangeOptions

func (*MultiRangeOptions) SetAlign

func (mrangeopts *MultiRangeOptions) SetAlign(byTimeStamp int64) *MultiRangeOptions

SetAlign sets the time bucket alignment control for AGGREGATION. This will control the time bucket timestamps by changing the reference timestamp on which a bucket is defined.

func (*MultiRangeOptions) SetCount

func (mrangeopts *MultiRangeOptions) SetCount(count int64) *MultiRangeOptions

func (*MultiRangeOptions) SetFilterByTs

func (mrangeopts *MultiRangeOptions) SetFilterByTs(filterByTS []int64) *MultiRangeOptions

SetFilterByTs sets the list of timestamps to filter the result by specific timestamps

func (*MultiRangeOptions) SetFilterByValue

func (mrangeopts *MultiRangeOptions) SetFilterByValue(min, max float64) *MultiRangeOptions

SetFilterByValue filters the result by value using minimum and maximum ( inclusive )

func (*MultiRangeOptions) SetGroupByReduce

func (mrangeopts *MultiRangeOptions) SetGroupByReduce(byLabel string, reducer ReducerType) *MultiRangeOptions

SetGroupByReduce Aggregates results across different time series, grouped by the provided label name. When combined with AGGREGATION the groupby/reduce is applied post aggregation stage.

func (*MultiRangeOptions) SetSelectedLabels

func (mrangeopts *MultiRangeOptions) SetSelectedLabels(labels []string) *MultiRangeOptions

SetSelectedLabels limits the series reply labels to provided label names

func (*MultiRangeOptions) SetWithLabels

func (mrangeopts *MultiRangeOptions) SetWithLabels(value bool) *MultiRangeOptions

type Range

type Range struct {
	Name       string
	Labels     map[string]string
	DataPoints []DataPoint
}

func ParseRanges

func ParseRanges(info interface{}) (ranges []Range, err error)

func ParseRangesSingleDataPoint

func ParseRangesSingleDataPoint(info interface{}) (ranges []Range, err error)

type RangeOptions

type RangeOptions struct {
	AggType          AggregationType
	TimeBucket       int
	Count            int64
	Align            int64
	FilterByTs       []int64
	FilterByValueMin *float64
	FilterByValueMax *float64
}

MultiRangeOptions represent the options for querying across multiple time-series

func NewRangeOptions

func NewRangeOptions() *RangeOptions

func (*RangeOptions) SetAggregation

func (rangeopts *RangeOptions) SetAggregation(aggType AggregationType, timeBucket int) *RangeOptions

func (*RangeOptions) SetAlign

func (rangeopts *RangeOptions) SetAlign(byTimeStamp int64) *RangeOptions

SetAlign sets the time bucket alignment control for AGGREGATION. This will control the time bucket timestamps by changing the reference timestamp on which a bucket is defined.

func (*RangeOptions) SetCount

func (rangeopts *RangeOptions) SetCount(count int64) *RangeOptions

func (*RangeOptions) SetFilterByTs

func (rangeopts *RangeOptions) SetFilterByTs(filterByTS []int64) *RangeOptions

SetFilterByTs sets a list of timestamps to filter the result by specific timestamps

func (*RangeOptions) SetFilterByValue

func (rangeopts *RangeOptions) SetFilterByValue(min, max float64) *RangeOptions

SetFilterByValue filters result by value using minimum and maximum ( inclusive )

type ReducerType

type ReducerType string
const (
	SumReducer ReducerType = "SUM"
	MinReducer ReducerType = "MIN"
	MaxReducer ReducerType = "MAX"
)

type Rule

type Rule struct {
	DestKey       string
	BucketSizeSec int
	AggType       AggregationType
}

func ParseRules

func ParseRules(ruleInterface interface{}, err error) (rules []Rule, retErr error)

type Sample

type Sample struct {
	Key       string
	DataPoint DataPoint
}

type SingleHostPool

type SingleHostPool struct {
	*redis.Pool
}

func NewSingleHostPool

func NewSingleHostPool(host string, authPass *string) *SingleHostPool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL