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

Function ExampleClient_timeseries_aggregation

doctests/timeseries_tut_test.go:619–694  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

617}
618
619func ExampleClient_timeseries_aggregation() {
620 ctx := context.Background()
621
622 rdb := redis.NewClient(&redis.Options{
623 Addr: "localhost:6379",
624 Password: "", // no password set
625 DB: 0, // use default DB
626 })
627
628 // REMOVE_START
629 // make sure we are working with fresh database
630 rdb.FlushDB(ctx)
631 rdb.Del(ctx, "rg:2")
632 // REMOVE_END
633
634 // Setup data for aggregation example
635 _, err := rdb.TSCreateWithArgs(ctx, "rg:2", &redis.TSOptions{
636 Labels: map[string]string{"location": "us", "unit": "cm"},
637 }).Result()
638 if err != nil {
639 panic(err)
640 }
641
642 _, err = rdb.TSMAdd(ctx, [][]interface{}{
643 {"rg:2", 0, 1.8},
644 {"rg:2", 1, 2.1},
645 {"rg:2", 2, 2.3},
646 {"rg:2", 3, 1.9},
647 {"rg:2", 4, 1.78},
648 }).Result()
649 if err != nil {
650 panic(err)
651 }
652
653 // STEP_START agg
654 res32, err := rdb.TSRangeWithArgs(
655 ctx,
656 "rg:2",
657 0,
658 math.MaxInt64,
659 &redis.TSRangeOptions{
660 Aggregator: redis.Avg,
661 BucketDuration: 2,
662 },
663 ).Result()
664 if err != nil {
665 panic(err)
666 }
667
668 fmt.Println(res32)
669 // >>> [{0 1.9500000000000002} {2 2.0999999999999996} {4 1.78}]
670 // STEP_END
671
672 // STEP_START agg_multi
673 res33, err := rdb.TSRangeWithArgs(
674 ctx,
675 "rg:2",
676 0,

Callers

nothing calls this directly

Calls 6

FlushDBMethod · 0.65
DelMethod · 0.65
ResultMethod · 0.65
TSCreateWithArgsMethod · 0.65
TSMAddMethod · 0.65
TSRangeWithArgsMethod · 0.65

Tested by

no test coverage detected