Skip to content

Commit ff62603

Browse files
DOC-5472 added agg bucket examples
1 parent 7a681d3 commit ff62603

File tree

1 file changed

+94
-28
lines changed

1 file changed

+94
-28
lines changed

doctests/timeseries_tut_test.go

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,13 @@ func ExampleClient_timeseries_query_multi() {
468468

469469
// Retrieve the same data points, but include the `unit`
470470
// 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()
474478

475479
if err != nil {
476480
panic(err)
@@ -509,6 +513,8 @@ func ExampleClient_timeseries_query_multi() {
509513
// Retrieve data points up to time 2 (inclusive) from all
510514
// time series that use millimeters as the unit. Include all
511515
// labels in the results.
516+
// Note that the `aggregators` field is empty if no
517+
// aggregators are specified.
512518
res30, err := rdb.TSMRangeWithArgs(
513519
ctx,
514520
0, 2,
@@ -548,7 +554,7 @@ func ExampleClient_timeseries_query_multi() {
548554
// >>> location: uk
549555
// >>> unit: mm
550556
// >>> Aggregators: map[aggregators:[]]
551-
// >>> Data points: [{0 25} {1 18} {2 21}]
557+
// >>> [{0 25} {1 18} {2 21}]
552558

553559
// Retrieve data points from time 1 to time 3 (inclusive) from
554560
// all time series that use centimeters or millimeters as the unit,
@@ -566,6 +572,7 @@ func ExampleClient_timeseries_query_multi() {
566572
if err != nil {
567573
panic(err)
568574
}
575+
569576
res31Keys := slices.Collect(maps.Keys(res31))
570577
sort.Strings(res31Keys)
571578

@@ -588,6 +595,14 @@ func ExampleClient_timeseries_query_multi() {
588595
fmt.Printf(" Aggregators: %v\n", res31[k][1])
589596
fmt.Printf(" %v\n", res31[k][2])
590597
}
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}]
591606
// STEP_END
592607

593608
// Output:
@@ -636,80 +651,131 @@ func ExampleClient_timeseries_aggregation() {
636651
// REMOVE_START
637652
// make sure we are working with fresh database
638653
rdb.FlushDB(ctx)
639-
rdb.Del(ctx, "sensor3")
654+
rdb.Del(ctx, "rg:2")
640655
// REMOVE_END
641656

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()
644661

645662
if err != nil {
646663
panic(err)
647664
}
648665

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()
653673

654-
if err != nil {
655-
panic(err)
656-
}
674+
if err != nil {
675+
panic(err)
657676
}
658677

659-
res2, err := rdb.TSRangeWithArgs(
678+
// STEP_START agg
679+
res32, err := rdb.TSRangeWithArgs(
660680
ctx,
661-
"sensor3",
681+
"rg:2",
662682
0, math.MaxInt64,
663683
&redis.TSRangeOptions{
664684
Aggregator: redis.Avg,
665-
BucketDuration: 5,
685+
BucketDuration: 2,
666686
},
667687
).Result()
668688

669689
if err != nil {
670690
panic(err)
671691
}
672692

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]
674739

675740
res3, err := rdb.TSRangeWithArgs(
676741
ctx,
677742
"sensor3",
678-
0, math.MaxInt64,
743+
10, 70,
679744
&redis.TSRangeOptions{
680745
Aggregator: redis.Min,
681-
BucketDuration: 5,
746+
BucketDuration: 25,
682747
},
683748
).Result()
684749

685750
if err != nil {
686751
panic(err)
687752
}
688753

689-
fmt.Println(res3) // >>> (0, 2.0) (5, 12.0)
754+
fmt.Println(res3) // >>> [{0 1000} {25 3000} {50 5000}]
690755

691756
res4, err := rdb.TSRangeWithArgs(
692757
ctx,
693758
"sensor3",
694-
0, math.MaxInt64,
759+
10, 70,
695760
&redis.TSRangeOptions{
696-
Aggregator: redis.Max,
697-
BucketDuration: 5,
761+
Aggregator: redis.Min,
762+
BucketDuration: 25,
763+
Align: "START",
698764
},
699765
).Result()
700766

701767
if err != nil {
702768
panic(err)
703769
}
704770

705-
fmt.Println(res4) // >>> (0, 10.0) (5, 20.0)
771+
fmt.Println(res4) // >>> [{10 1000} {35 4000} {60 6000}]
706772
// STEP_END
707773

708774
// Output:
709775
// 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}]
713779
}
714780

715781
func ExampleClient_timeseries_downsampling() {

0 commit comments

Comments
 (0)