Skip to content

Commit 1eec676

Browse files
add TDigestInfo struct
1 parent 39e6f42 commit 1eec676

File tree

1 file changed

+85
-4
lines changed

1 file changed

+85
-4
lines changed

client.go

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,52 @@ type Client struct {
1818
Name string
1919
}
2020

21+
// TDigestInfo is a struct that represents T-Digest properties
22+
type TDigestInfo struct {
23+
compression int64
24+
capacity int64
25+
mergedNodes int64
26+
unmergedNodes int64
27+
mergedWeight float64
28+
unmergedWeight float64
29+
totalCompressions int64
30+
}
31+
32+
// Compression
33+
func (info *TDigestInfo) Compression() int64 {
34+
return info.compression
35+
}
36+
37+
// Capacity
38+
func (info *TDigestInfo) Capacity() int64 {
39+
return info.capacity
40+
}
41+
42+
// MergedNodes
43+
func (info *TDigestInfo) MergedNodes() int64 {
44+
return info.mergedNodes
45+
}
46+
47+
// UnmergedNodes
48+
func (info *TDigestInfo) UnmergedNodes() int64 {
49+
return info.unmergedNodes
50+
}
51+
52+
// MergedWeight
53+
func (info *TDigestInfo) MergedWeight() float64 {
54+
return info.mergedWeight
55+
}
56+
57+
// UnmergedWeight
58+
func (info *TDigestInfo) UnmergedWeight() float64 {
59+
return info.unmergedWeight
60+
}
61+
62+
// TotalCompressions
63+
func (info *TDigestInfo) TotalCompressions() int64 {
64+
return info.totalCompressions
65+
}
66+
2167
// NewClient creates a new client connecting to the redis host, and using the given name as key prefix.
2268
// Addr can be a single host:port pair, or a comma separated list of host:port,host:port...
2369
// In the case of multiple hosts we create a multi-pool and select connections at random
@@ -458,15 +504,15 @@ func (client *Client) TdReset(key string) (string, error) {
458504
}
459505

460506
// TdAdd - Adds one or more samples to a sketch
461-
func (client *Client) TdAdd(key string, samples map[string]float64) ([]string, error) {
507+
func (client *Client) TdAdd(key string, samples map[string]float64) (string, error) {
462508
conn := client.Pool.Get()
463509
defer conn.Close()
464510
args := redis.Args{key}
465511
for k, v := range samples {
466512
args = args.Add(k, v)
467513
}
468514
reply, err := conn.Do("TDIGEST.ADD", args...)
469-
return redis.Strings(reply, err)
515+
return redis.String(reply, err)
470516
}
471517

472518
// TdMerge - Merges all of the values from 'from' to 'this' sketch
@@ -507,10 +553,10 @@ func (client *Client) TdCdf(key string, value float64) (float64, error) {
507553

508554
// TdInfo - Returns compression, capacity, total merged and unmerged nodes, the total
509555
// compressions made up to date on that key, and merged and unmerged weight.
510-
func (client *Client) TdInfo(key string) (map[string]int64, error) {
556+
func (client *Client) TdInfo(key string) (TDigestInfo, error) {
511557
conn := client.Pool.Get()
512558
defer conn.Close()
513-
return ParseInfoReply(redis.Values(conn.Do("TDIGEST.INFO", key)))
559+
return ParseTDigestInfo(redis.Values(conn.Do("TDIGEST.INFO", key)))
514560
}
515561

516562
func ParseInfoReply(values []interface{}, err error) (map[string]int64, error) {
@@ -526,3 +572,38 @@ func ParseInfoReply(values []interface{}, err error) (map[string]int64, error) {
526572
}
527573
return m, err
528574
}
575+
576+
func ParseTDigestInfo(result interface{}, err error) (info TDigestInfo, outErr error) {
577+
values, outErr := redis.Values(result, err)
578+
if outErr != nil {
579+
return TDigestInfo{}, err
580+
}
581+
if len(values)%2 != 0 {
582+
return TDigestInfo{}, errors.New("ParseInfo expects even number of values result")
583+
}
584+
var key string
585+
for i := 0; i < len(values); i += 2 {
586+
key, outErr = redis.String(values[i], nil)
587+
switch key {
588+
case "Compression":
589+
info.compression, outErr = redis.Int64(values[i+1], nil)
590+
case "Capacity":
591+
info.capacity, outErr = redis.Int64(values[i+1], nil)
592+
case "Merged nodes":
593+
info.mergedNodes, outErr = redis.Int64(values[i+1], nil)
594+
case "Unmerged nodes":
595+
info.unmergedNodes, outErr = redis.Int64(values[i+1], nil)
596+
case "Merged weight":
597+
info.mergedWeight, outErr = redis.Float64(values[i+1], nil)
598+
case "Unmerged weight":
599+
info.unmergedWeight, outErr = redis.Float64(values[i+1], nil)
600+
case "Total compressions":
601+
info.totalCompressions, outErr = redis.Int64(values[i+1], nil)
602+
}
603+
if outErr != nil {
604+
return TDigestInfo{}, outErr
605+
}
606+
}
607+
608+
return info, nil
609+
}

0 commit comments

Comments
 (0)