|
14 | 14 | r.delete(
|
15 | 15 | "thermometer:1", "thermometer:2", "thermometer:3",
|
16 | 16 | "rg:1", "rg:2", "rg:3", "rg:4",
|
17 |
| - "sensor:3", |
| 17 | + "sensor3", |
18 | 18 | "wind:1", "wind:2", "wind:3", "wind:4",
|
19 | 19 | "hyg:1", "hyg:compacted"
|
20 | 20 | )
|
|
278 | 278 | {'rg:4': [{'location': 'uk'}, [(3, 19.0), (2, 21.0), (1, 18.0)]]}
|
279 | 279 | ]
|
280 | 280 | # REMOVE_END
|
| 281 | + |
| 282 | +# STEP_START agg |
| 283 | +res32 = r.ts().range( |
| 284 | + "rg:2", "-", "+", |
| 285 | + aggregation_type="avg", |
| 286 | + bucket_size_msec=2 |
| 287 | +) |
| 288 | +print(res32) |
| 289 | +# >>> [(0, 1.9500000000000002), (2, 2.0999999999999996), (4, 1.78)] |
| 290 | +# STEP_END |
| 291 | +# REMOVE_START |
| 292 | +assert res32 == [ |
| 293 | + (0, 1.9500000000000002), (2, 2.0999999999999996), |
| 294 | + (4, 1.78) |
| 295 | +] |
| 296 | +# REMOVE_END |
| 297 | + |
| 298 | +# STEP_START agg_bucket |
| 299 | +res33 = r.ts().create("sensor3") |
| 300 | +print(res33) # >>> True |
| 301 | + |
| 302 | +res34 = r.ts().madd([ |
| 303 | + ("sensor3", 10, 1000), |
| 304 | + ("sensor3", 20, 2000), |
| 305 | + ("sensor3", 30, 3000), |
| 306 | + ("sensor3", 40, 4000), |
| 307 | + ("sensor3", 50, 5000), |
| 308 | + ("sensor3", 60, 6000), |
| 309 | + ("sensor3", 70, 7000), |
| 310 | +]) |
| 311 | +print(res34) # >>> [10, 20, 30, 40, 50, 60, 70] |
| 312 | + |
| 313 | +res35 = r.ts().range( |
| 314 | + "sensor3", 10, 70, |
| 315 | + aggregation_type="min", |
| 316 | + bucket_size_msec=25 |
| 317 | +) |
| 318 | +print(res35) |
| 319 | +# >>> [(0, 1000.0), (25, 3000.0), (50, 5000.0)] |
| 320 | +# STEP_END |
| 321 | +# REMOVE_START |
| 322 | +assert res33 is True |
| 323 | +assert res34 == [10, 20, 30, 40, 50, 60, 70] |
| 324 | +assert res35 == [(0, 1000.0), (25, 3000.0), (50, 5000.0)] |
| 325 | +# REMOVE_END |
| 326 | + |
| 327 | +# STEP_START agg_align |
| 328 | +res36 = r.ts().range( |
| 329 | + "sensor3", 10, 70, |
| 330 | + aggregation_type="min", |
| 331 | + bucket_size_msec=25, |
| 332 | + align="START" |
| 333 | +) |
| 334 | +print(res36) |
| 335 | +# >>> [(10, 1000.0), (35, 4000.0), (60, 6000.0)] |
| 336 | +# STEP_END |
| 337 | +# REMOVE_START |
| 338 | +assert res36 == [(10, 1000.0), (35, 4000.0), (60, 6000.0)] |
| 339 | +# REMOVE_END |
| 340 | + |
| 341 | +# STEP_START agg_multi |
| 342 | +res37 = r.ts().create( |
| 343 | + "wind:1", |
| 344 | + labels={"country": "uk"} |
| 345 | +) |
| 346 | +print(res37) # >>> True |
| 347 | + |
| 348 | +res38 = r.ts().create( |
| 349 | + "wind:2", |
| 350 | + labels={"country": "uk"} |
| 351 | +) |
| 352 | +print(res38) # >>> True |
| 353 | + |
| 354 | +res39 = r.ts().create( |
| 355 | + "wind:3", |
| 356 | + labels={"country": "us"} |
| 357 | +) |
| 358 | +print(res39) # >>> True |
| 359 | + |
| 360 | +res40 = r.ts().create( |
| 361 | + "wind:4", |
| 362 | + labels={"country": "us"} |
| 363 | +) |
| 364 | +print(res40) # >>> True |
| 365 | + |
| 366 | +res41 = r.ts().madd([ |
| 367 | + ("wind:1", 1, 12), |
| 368 | + ("wind:2", 1, 18), |
| 369 | + ("wind:3", 1, 5), |
| 370 | + ("wind:4", 1, 20), |
| 371 | +]) |
| 372 | +print(res41) # >>> [1, 1, 1, 1] |
| 373 | + |
| 374 | +res42 = r.ts().madd([ |
| 375 | + ("wind:1", 2, 14), |
| 376 | + ("wind:2", 2, 21), |
| 377 | + ("wind:3", 2, 4), |
| 378 | + ("wind:4", 2, 25), |
| 379 | +]) |
| 380 | +print(res42) # >>> [2, 2, 2, 2] |
| 381 | + |
| 382 | +res43 = r.ts().madd([ |
| 383 | + ("wind:1", 3, 10), |
| 384 | + ("wind:2", 3, 24), |
| 385 | + ("wind:3", 3, 8), |
| 386 | + ("wind:4", 3, 18), |
| 387 | +]) |
| 388 | +print(res43) # >>> [3, 3, 3, 3] |
| 389 | + |
| 390 | +# The result pairs contain the timestamp and the maximum wind speed |
| 391 | +# for the country at that timestamp. |
| 392 | +res44 = r.ts().mrange( |
| 393 | + "-", "+", |
| 394 | + filters=["country=(us,uk)"], |
| 395 | + groupby="country", |
| 396 | + reduce="max" |
| 397 | +) |
| 398 | +print(res44) |
| 399 | +# >>> [{'country=uk': [{}, [(1, 18.0), (2, 21.0), (3, 24.0)]]}, {'country=us': [{}, [(1, 20.0), (2, 25.0), (3, 18.0)]]}] |
| 400 | + |
| 401 | +# The result pairs contain the timestamp and the average wind speed |
| 402 | +# for the country at that timestamp. |
| 403 | +res45 = r.ts().mrange( |
| 404 | + "-", "+", |
| 405 | + filters=["country=(us,uk)"], |
| 406 | + groupby="country", |
| 407 | + reduce="avg" |
| 408 | +) |
| 409 | +print(res45) |
| 410 | +# >>> [{'country=uk': [{}, [(1, 15.0), (2, 17.5), (3, 17.0)]]}, {'country=us': [{}, [(1, 12.5), (2, 14.5), (3, 11.5)]]}] |
| 411 | +# STEP_END |
| 412 | +# REMOVE_START |
| 413 | +assert res37 is True |
| 414 | +assert res38 is True |
| 415 | +assert res39 is True |
| 416 | +assert res40 is True |
| 417 | +assert res41 == [1, 1, 1, 1] |
| 418 | +assert res42 == [2, 2, 2, 2] |
| 419 | +assert res43 == [3, 3, 3, 3] |
| 420 | +assert res44 == [ |
| 421 | + {'country=uk': [{}, [(1, 18.0), (2, 21.0), (3, 24.0)]]}, |
| 422 | + {'country=us': [{}, [(1, 20.0), (2, 25.0), (3, 18.0)]]} |
| 423 | +] |
| 424 | +assert res45 == [ |
| 425 | + {'country=uk': [{}, [(1, 15.0), (2, 17.5), (3, 17.0)]]}, |
| 426 | + {'country=us': [{}, [(1, 12.5), (2, 14.5), (3, 13.0)]]} |
| 427 | +] |
| 428 | +# REMOVE_END |
0 commit comments