47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
|
// Package diagram provide diagram data structure and operation
|
||
|
|
package diagram
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
)
|
||
|
|
|
||
|
|
// RedisClient define struct to create redis client
|
||
|
|
type RedisClient struct {
|
||
|
|
Client *redis.Client
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewRedisClient define func of new redis client instance
|
||
|
|
func NewRedisClient() *RedisClient {
|
||
|
|
return &RedisClient{
|
||
|
|
Client: GetRedisClientInstance(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// QueryByZRangeByLex define func to query real time data from redis zset
|
||
|
|
func (rc *RedisClient) QueryByZRangeByLex(ctx context.Context, key string, size int64, startTimestamp, stopTimeStamp string) ([]float64, error) {
|
||
|
|
client := rc.Client
|
||
|
|
|
||
|
|
datas := make([]float64, 0, size)
|
||
|
|
startStr := "[" + startTimestamp
|
||
|
|
stopStr := stopTimeStamp + "]"
|
||
|
|
args := redis.ZRangeArgs{
|
||
|
|
Key: key,
|
||
|
|
Start: startStr,
|
||
|
|
Stop: stopStr,
|
||
|
|
ByLex: true,
|
||
|
|
Rev: false,
|
||
|
|
Count: size,
|
||
|
|
}
|
||
|
|
|
||
|
|
members, err := client.ZRangeArgsWithScores(ctx, args).Result()
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
for data := range members {
|
||
|
|
datas = append(datas, float64(data))
|
||
|
|
}
|
||
|
|
return datas, nil
|
||
|
|
}
|