github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/writer/stats/segstats_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 stats
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/siglens/siglens/pkg/segment/structs"
    24  	. "github.com/siglens/siglens/pkg/segment/structs"
    25  	. "github.com/siglens/siglens/pkg/segment/utils"
    26  	"github.com/stretchr/testify/assert"
    27  	bbp "github.com/valyala/bytebufferpool"
    28  )
    29  
    30  func Test_addSegStatsStr(t *testing.T) {
    31  
    32  	cname := "mycol1"
    33  	sst := make(map[string]*structs.SegStats)
    34  	numRecs := uint64(2000)
    35  
    36  	bb := bbp.Get()
    37  
    38  	for i := uint64(0); i < numRecs; i++ {
    39  		AddSegStatsStr(sst, cname, fmt.Sprintf("%v", i), bb, nil, false)
    40  	}
    41  
    42  	assert.Equal(t, numRecs, sst[cname].Count)
    43  }
    44  
    45  func Test_addSegStatsNums(t *testing.T) {
    46  
    47  	cname := "mycol1"
    48  	sst := make(map[string]*SegStats)
    49  	bb := bbp.Get()
    50  
    51  	AddSegStatsNums(sst, cname, SS_UINT64, 0, uint64(2345), 0, "2345", bb, nil, false)
    52  	assert.NotEqual(t, SS_DT_FLOAT, sst[cname].NumStats.Min.Ntype)
    53  	assert.Equal(t, int64(2345), sst[cname].NumStats.Min.IntgrVal)
    54  
    55  	AddSegStatsNums(sst, cname, SS_FLOAT64, 0, 0, float64(345.1), "345.1", bb, nil, false)
    56  	assert.Equal(t, SS_DT_FLOAT, sst[cname].NumStats.Min.Ntype)
    57  	assert.Equal(t, float64(345.1), sst[cname].NumStats.Min.FloatVal)
    58  
    59  	assert.Equal(t, SS_DT_FLOAT, sst[cname].NumStats.Sum.Ntype)
    60  	assert.Equal(t, float64(345.1+2345), sst[cname].NumStats.Sum.FloatVal)
    61  
    62  }
    63  
    64  func Test_addSegStatsNumsForEvalFunc(t *testing.T) {
    65  
    66  	cname := "duration"
    67  	cname2 := "latitude"
    68  	sst := make(map[string]*SegStats)
    69  	bb := bbp.Get()
    70  
    71  	aggColUsage := make(map[string]AggColUsageMode, 0)
    72  	aggColUsage["duration"] = WithEvalUsage
    73  	aggColUsage["latitude"] = NoEvalUsage
    74  
    75  	AddSegStatsNums(sst, cname, SS_UINT64, 0, uint64(111), 0, "111", bb, aggColUsage, false)
    76  	AddSegStatsNums(sst, cname, SS_UINT64, 0, uint64(333), 0, "333", bb, aggColUsage, false)
    77  	AddSegStatsNums(sst, cname, SS_UINT64, 0, uint64(222), 0, "222", bb, aggColUsage, false)
    78  	assert.Len(t, sst[cname].Records, 3)
    79  	assert.Equal(t, int64(111), sst[cname].Records[0].CVal)
    80  	assert.Equal(t, int64(333), sst[cname].Records[1].CVal)
    81  	assert.Equal(t, int64(222), sst[cname].Records[2].CVal)
    82  
    83  	aggColUsage["latitude"] = NoEvalUsage
    84  	AddSegStatsNums(sst, cname2, SS_FLOAT64, 0, 0, 40.7128, "40.7128", bb, aggColUsage, false)
    85  	AddSegStatsNums(sst, cname2, SS_FLOAT64, 0, 0, -10.5218, "-10.5218", bb, aggColUsage, false)
    86  	assert.Len(t, sst[cname2].Records, 0)
    87  }
    88  
    89  func Test_addSegStatsStrForValuesFunc(t *testing.T) {
    90  
    91  	cname := "mycol1"
    92  	sst := make(map[string]*structs.SegStats)
    93  
    94  	bb := bbp.Get()
    95  
    96  	AddSegStatsStr(sst, cname, "b", bb, nil, false)
    97  	AddSegStatsStr(sst, cname, "d", bb, nil, false)
    98  
    99  	assert.Nil(t, sst[cname].StringStats)
   100  
   101  	AddSegStatsStr(sst, cname, "a", bb, nil, true)
   102  	AddSegStatsStr(sst, cname, "c", bb, nil, true)
   103  
   104  	assert.NotNil(t, sst[cname].StringStats)
   105  	assert.NotNil(t, sst[cname].StringStats.StrSet)
   106  	assert.Equal(t, map[string]struct{}{"a": {}, "c": {}}, sst[cname].StringStats.StrSet)
   107  }