github.com/openfga/openfga@v1.5.4-rc1/internal/utils/bucket.go (about)

     1  package utils
     2  
     3  import "strconv"
     4  
     5  // Bucketize will put the value into the correct bucket.
     6  // It is expected that the buckets are already sorted in increasing order and non-empty.
     7  func Bucketize(value uint, buckets []uint) string {
     8  	for _, bucketValue := range buckets {
     9  		if value <= bucketValue {
    10  			return strconv.Itoa(int(bucketValue))
    11  		}
    12  	}
    13  	return ">" + strconv.Itoa(int(buckets[len(buckets)-1]))
    14  }
    15  
    16  // LinearBuckets returns an evenly distributed range of buckets in the closed interval
    17  // [min...max]. The min and max count toward the bucket count since they are included
    18  // in the range.
    19  func LinearBuckets(min, max float64, count int) []float64 {
    20  	var buckets []float64
    21  
    22  	width := (max - min) / float64(count-1)
    23  
    24  	for i := min; i <= max; i += width {
    25  		buckets = append(buckets, i)
    26  	}
    27  
    28  	return buckets
    29  }