@@ -468,9 +468,13 @@ func ExampleClient_timeseries_query_multi() {
468
468
469
469
// Retrieve the same data points, but include the `unit`
470
470
// label in the results.
471
- res29 , err := rdb .TSMGetWithArgs (ctx , []string {"location=us" }, & redis.TSMGetOptions {
472
- SelectedLabels : []interface {}{"unit" },
473
- }).Result ()
471
+ res29 , err := rdb .TSMGetWithArgs (
472
+ ctx ,
473
+ []string {"location=us" },
474
+ & redis.TSMGetOptions {
475
+ SelectedLabels : []interface {}{"unit" },
476
+ },
477
+ ).Result ()
474
478
475
479
if err != nil {
476
480
panic (err )
@@ -509,6 +513,8 @@ func ExampleClient_timeseries_query_multi() {
509
513
// Retrieve data points up to time 2 (inclusive) from all
510
514
// time series that use millimeters as the unit. Include all
511
515
// labels in the results.
516
+ // Note that the `aggregators` field is empty if no
517
+ // aggregators are specified.
512
518
res30 , err := rdb .TSMRangeWithArgs (
513
519
ctx ,
514
520
0 , 2 ,
@@ -548,7 +554,7 @@ func ExampleClient_timeseries_query_multi() {
548
554
// >>> location: uk
549
555
// >>> unit: mm
550
556
// >>> Aggregators: map[aggregators:[]]
551
- // >>> Data points: [{0 25} {1 18} {2 21}]
557
+ // >>> [{0 25} {1 18} {2 21}]
552
558
553
559
// Retrieve data points from time 1 to time 3 (inclusive) from
554
560
// all time series that use centimeters or millimeters as the unit,
@@ -566,6 +572,7 @@ func ExampleClient_timeseries_query_multi() {
566
572
if err != nil {
567
573
panic (err )
568
574
}
575
+
569
576
res31Keys := slices .Collect (maps .Keys (res31 ))
570
577
sort .Strings (res31Keys )
571
578
@@ -588,6 +595,14 @@ func ExampleClient_timeseries_query_multi() {
588
595
fmt .Printf (" Aggregators: %v\n " , res31 [k ][1 ])
589
596
fmt .Printf (" %v\n " , res31 [k ][2 ])
590
597
}
598
+ // >>> rg:2:
599
+ // >>> location: us
600
+ // >>> Aggregators: map[aggregators:[]]
601
+ // >>> [{3 1.9} {2 2.3} {1 2.1}]
602
+ // >>> rg:4:
603
+ // >>> location: uk
604
+ // >>> Aggregators: map[aggregators:[]]
605
+ // >>> [{3 19} {2 21} {1 18}]
591
606
// STEP_END
592
607
593
608
// Output:
@@ -636,80 +651,131 @@ func ExampleClient_timeseries_aggregation() {
636
651
// REMOVE_START
637
652
// make sure we are working with fresh database
638
653
rdb .FlushDB (ctx )
639
- rdb .Del (ctx , "sensor3 " )
654
+ rdb .Del (ctx , "rg:2 " )
640
655
// REMOVE_END
641
656
642
- // STEP_START aggregation
643
- res1 , err := rdb .TSCreate (ctx , "sensor3" ).Result ()
657
+ // Setup data for aggregation example
658
+ _ , err := rdb .TSCreateWithArgs (ctx , "rg:2" , & redis.TSOptions {
659
+ Labels : map [string ]string {"location" : "us" , "unit" : "cm" },
660
+ }).Result ()
644
661
645
662
if err != nil {
646
663
panic (err )
647
664
}
648
665
649
- fmt .Println (res1 ) // >>> OK
650
-
651
- for i := 1 ; i <= 10 ; i ++ {
652
- _ , err := rdb .TSAdd (ctx , "sensor3" , int64 (i ), float64 (i * 2 )).Result ()
666
+ _ , err = rdb .TSMAdd (ctx , [][]interface {}{
667
+ {"rg:2" , 0 , 1.8 },
668
+ {"rg:2" , 1 , 2.1 },
669
+ {"rg:2" , 2 , 2.3 },
670
+ {"rg:2" , 3 , 1.9 },
671
+ {"rg:2" , 4 , 1.78 },
672
+ }).Result ()
653
673
654
- if err != nil {
655
- panic (err )
656
- }
674
+ if err != nil {
675
+ panic (err )
657
676
}
658
677
659
- res2 , err := rdb .TSRangeWithArgs (
678
+ // STEP_START agg
679
+ res32 , err := rdb .TSRangeWithArgs (
660
680
ctx ,
661
- "sensor3 " ,
681
+ "rg:2 " ,
662
682
0 , math .MaxInt64 ,
663
683
& redis.TSRangeOptions {
664
684
Aggregator : redis .Avg ,
665
- BucketDuration : 5 ,
685
+ BucketDuration : 2 ,
666
686
},
667
687
).Result ()
668
688
669
689
if err != nil {
670
690
panic (err )
671
691
}
672
692
673
- fmt .Println (res2 ) // >>> (0, 6.0) (5, 16.0)
693
+ fmt .Println (res32 )
694
+ // >>> [{0 1.9500000000000002} {2 2.0999999999999996} {4 1.78}]
695
+ // STEP_END
696
+
697
+ // Output:
698
+ // [{0 1.9500000000000002} {2 2.0999999999999996} {4 1.78}]
699
+ }
700
+ func ExampleClient_timeseries_agg_bucket () {
701
+ ctx := context .Background ()
702
+
703
+ rdb := redis .NewClient (& redis.Options {
704
+ Addr : "localhost:6379" ,
705
+ Password : "" , // no password set
706
+ DB : 0 , // use default DB
707
+ })
708
+
709
+ // REMOVE_START
710
+ // make sure we are working with fresh database
711
+ rdb .FlushDB (ctx )
712
+ rdb .Del (ctx , "sensor3" )
713
+ // REMOVE_END
714
+
715
+ // STEP_START agg_bucket
716
+ res1 , err := rdb .TSCreate (ctx , "sensor3" ).Result ()
717
+
718
+ if err != nil {
719
+ panic (err )
720
+ }
721
+
722
+ fmt .Println (res1 ) // >>> OK
723
+
724
+ res2 , err := rdb .TSMAdd (ctx , [][]interface {}{
725
+ {"sensor3" , 10 , 1000 },
726
+ {"sensor3" , 20 , 2000 },
727
+ {"sensor3" , 30 , 3000 },
728
+ {"sensor3" , 40 , 4000 },
729
+ {"sensor3" , 50 , 5000 },
730
+ {"sensor3" , 60 , 6000 },
731
+ {"sensor3" , 70 , 7000 },
732
+ }).Result ()
733
+
734
+ if err != nil {
735
+ panic (err )
736
+ }
737
+
738
+ fmt .Println (res2 ) // >>> [10 20 30 40 50 60 70]
674
739
675
740
res3 , err := rdb .TSRangeWithArgs (
676
741
ctx ,
677
742
"sensor3" ,
678
- 0 , math . MaxInt64 ,
743
+ 10 , 70 ,
679
744
& redis.TSRangeOptions {
680
745
Aggregator : redis .Min ,
681
- BucketDuration : 5 ,
746
+ BucketDuration : 25 ,
682
747
},
683
748
).Result ()
684
749
685
750
if err != nil {
686
751
panic (err )
687
752
}
688
753
689
- fmt .Println (res3 ) // >>> (0, 2.0) (5, 12.0)
754
+ fmt .Println (res3 ) // >>> [{0 1000} {25 3000} {50 5000}]
690
755
691
756
res4 , err := rdb .TSRangeWithArgs (
692
757
ctx ,
693
758
"sensor3" ,
694
- 0 , math . MaxInt64 ,
759
+ 10 , 70 ,
695
760
& redis.TSRangeOptions {
696
- Aggregator : redis .Max ,
697
- BucketDuration : 5 ,
761
+ Aggregator : redis .Min ,
762
+ BucketDuration : 25 ,
763
+ Align : "START" ,
698
764
},
699
765
).Result ()
700
766
701
767
if err != nil {
702
768
panic (err )
703
769
}
704
770
705
- fmt .Println (res4 ) // >>> (0, 10.0) (5, 20.0)
771
+ fmt .Println (res4 ) // >>> [{10 1000} {35 4000} {60 6000}]
706
772
// STEP_END
707
773
708
774
// Output:
709
775
// OK
710
- // [{0 5} {5 14} {10 20} ]
711
- // [{0 2 } {5 10 } {10 20 }]
712
- // [{0 8 } {5 18 } {10 20 }]
776
+ // [10 20 30 40 50 60 70 ]
777
+ // [{0 1000 } {25 3000 } {50 5000 }]
778
+ // [{10 1000 } {35 4000 } {60 6000 }]
713
779
}
714
780
715
781
func ExampleClient_timeseries_downsampling () {
0 commit comments