MCPcopy
hub / github.com/redis/go-redis / ExampleClient_timeseries_range

Function ExampleClient_timeseries_range

doctests/timeseries_tut_test.go:165–311  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

163}
164
165func ExampleClient_timeseries_range() {
166 ctx := context.Background()
167
168 rdb := redis.NewClient(&redis.Options{
169 Addr: "localhost:6379",
170 Password: "", // no password set
171 DB: 0, // use default DB
172 })
173
174 // REMOVE_START
175 // make sure we are working with fresh database
176 rdb.FlushDB(ctx)
177 rdb.Del(ctx, "rg:1")
178 // REMOVE_END
179
180 // STEP_START range
181 // Add 5 data points to a time series named "rg:1".
182 res1, err := rdb.TSCreate(ctx, "rg:1").Result()
183 if err != nil {
184 panic(err)
185 }
186
187 fmt.Println(res1) // >>> OK
188
189 res2, err := rdb.TSMAdd(ctx, [][]interface{}{
190 {"rg:1", 0, 18},
191 {"rg:1", 1, 14},
192 {"rg:1", 2, 22},
193 {"rg:1", 3, 18},
194 {"rg:1", 4, 24},
195 }).Result()
196 if err != nil {
197 panic(err)
198 }
199
200 fmt.Println(res2) // >>> [0 1 2 3 4]
201
202 // Retrieve all the data points in ascending order.
203 // Note: use 0 and `math.MaxInt64` instead of - and +
204 // to denote the minimum and maximum possible timestamps.
205 res3, err := rdb.TSRange(ctx, "rg:1", 0, math.MaxInt64).Result()
206 if err != nil {
207 panic(err)
208 }
209
210 fmt.Println(res3)
211 // >>> [{0 18} {1 14} {2 22} {3 18} {4 24}]
212
213 // Retrieve data points up to time 1 (inclusive).
214 res4, err := rdb.TSRange(ctx, "rg:1", 0, 1).Result()
215 if err != nil {
216 panic(err)
217 }
218
219 fmt.Println(res4)
220 // >>> [{0 18} {1 14}]
221
222 // Retrieve data points from time 3 onwards.

Callers

nothing calls this directly

Calls 9

FlushDBMethod · 0.65
DelMethod · 0.65
ResultMethod · 0.65
TSCreateMethod · 0.65
TSMAddMethod · 0.65
TSRangeMethod · 0.65
TSRevRangeMethod · 0.65
TSRangeWithArgsMethod · 0.65
TSRevRangeWithArgsMethod · 0.65

Tested by

no test coverage detected