github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/results/blockresults/runningstats_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package blockresults
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/siglens/siglens/pkg/segment/structs"
    25  	"github.com/siglens/siglens/pkg/segment/utils"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  // TODO: more tests for more types of AggFunc
    30  func Test_RunningStatsAdd(t *testing.T) {
    31  
    32  	aggs := &structs.QueryAggregators{
    33  		TimeHistogram: &structs.TimeBucket{},
    34  	}
    35  	bRes, err := InitBlockResults(10, aggs, 0)
    36  	assert.NoError(t, err)
    37  	for i := uint64(0); i < 10; i++ {
    38  		bRes.AddKeyToTimeBucket(i, 5)
    39  	}
    40  	assert.NotNil(t, bRes.TimeAggregation)
    41  	assert.Len(t, bRes.TimeAggregation.AllRunningBuckets, 10)
    42  	for _, bucket := range bRes.TimeAggregation.AllRunningBuckets {
    43  		assert.Equal(t, bucket.count, uint64(5))
    44  	}
    45  }
    46  
    47  func Test_JoinStats(t *testing.T) {
    48  	aggs := &structs.QueryAggregators{
    49  		GroupByRequest: &structs.GroupByRequest{
    50  			GroupByColumns: []string{"a", "b"},
    51  			MeasureOperations: []*structs.MeasureAggregator{
    52  				{MeasureCol: "c", MeasureFunc: utils.Min},
    53  				{MeasureCol: "d", MeasureFunc: utils.Max},
    54  			},
    55  			BucketCount: 100,
    56  		},
    57  	}
    58  	bRes, err := InitBlockResults(10, aggs, 0)
    59  	assert.NoError(t, err)
    60  	for i := uint64(0); i < 10; i++ {
    61  		var buf bytes.Buffer
    62  		buf.WriteString(fmt.Sprintf("%v", i))
    63  		mRes := []utils.CValueEnclosure{
    64  			{CVal: uint64(1), Dtype: utils.SS_DT_UNSIGNED_NUM},
    65  			{CVal: uint64(1), Dtype: utils.SS_DT_UNSIGNED_NUM},
    66  		}
    67  		bRes.AddMeasureResultsToKey(buf, mRes, "", false, 5)
    68  	}
    69  	assert.NotNil(t, bRes.GroupByAggregation)
    70  	assert.Len(t, bRes.GroupByAggregation.AllRunningBuckets, 10)
    71  	for _, bucket := range bRes.GroupByAggregation.AllRunningBuckets {
    72  		assert.Len(t, bucket.runningStats, 2)
    73  		assert.Equal(t, bucket.runningStats[0].rawVal, utils.CValueEnclosure{Dtype: utils.SS_DT_UNSIGNED_NUM, CVal: uint64(1)})
    74  		assert.Equal(t, bucket.runningStats[1].rawVal, utils.CValueEnclosure{Dtype: utils.SS_DT_UNSIGNED_NUM, CVal: uint64(1)})
    75  		assert.Equal(t, bucket.count, uint64(1))
    76  	}
    77  
    78  	toMerge, err := InitBlockResults(10, aggs, 0)
    79  	assert.NoError(t, err)
    80  	for i := uint64(0); i < 10; i++ {
    81  		var buf bytes.Buffer
    82  		buf.WriteString(fmt.Sprintf("%v", i))
    83  		mRes := []utils.CValueEnclosure{
    84  			{CVal: uint64(1), Dtype: utils.SS_DT_UNSIGNED_NUM},
    85  			{CVal: i, Dtype: utils.SS_DT_UNSIGNED_NUM},
    86  		}
    87  		toMerge.AddMeasureResultsToKey(buf, mRes, "", false, 5)
    88  	}
    89  
    90  	bRes.MergeBuckets(toMerge)
    91  	assert.Len(t, bRes.GroupByAggregation.StringBucketIdx, 10)
    92  	for i := uint64(0); i < 10; i++ {
    93  		key := fmt.Sprintf("%v", i)
    94  		idx, ok := bRes.GroupByAggregation.StringBucketIdx[key]
    95  		assert.True(t, ok)
    96  		bucket := bRes.GroupByAggregation.AllRunningBuckets[idx]
    97  		assert.Equal(t, bucket.count, uint64(2))
    98  		assert.Equal(t, bucket.runningStats[0].rawVal, utils.CValueEnclosure{Dtype: utils.SS_DT_UNSIGNED_NUM, CVal: uint64(1)}, "min stays the same")
    99  
   100  		var max = i
   101  		if i == 0 {
   102  			max = uint64(1)
   103  		}
   104  		assert.Equal(t, bucket.runningStats[1].rawVal, utils.CValueEnclosure{Dtype: utils.SS_DT_UNSIGNED_NUM, CVal: max}, "max should be i or 1 if i==0")
   105  	}
   106  }