Slight improvements to `GetFactorOfTime` (#9473)

* correctly returns zero for non-overlapping data
* adds tests
pull/9474/head
Owen Diehl 2 years ago committed by GitHub
parent df77bf6024
commit 2efd059b49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pkg/querier/queryrange/index_stats_cache.go
  2. 12
      pkg/util/time.go
  3. 50
      pkg/util/time_test.go

@ -27,7 +27,7 @@ type IndexStatsExtractor struct{}
// Extract favors the ability to cache over exactness of results. It assumes a constant distribution
// of log volumes over a range and will extract subsets proportionally.
func (p IndexStatsExtractor) Extract(start, end int64, res queryrangebase.Response, resStart, resEnd int64) queryrangebase.Response {
factor, _, _ := util.GetFactorOfTime(start, end, resStart, resEnd)
factor := util.GetFactorOfTime(start, end, resStart, resEnd)
statsRes := res.(*IndexStatsResponse)
return &IndexStatsResponse{

@ -128,11 +128,15 @@ func ForInterval(interval time.Duration, start, end time.Time, endTimeInclusive
//
// We get the percentage of time that fits into C
// factor = C = (T - (A + B)) / T = (chunkTime - (leadingTime + trailingTime)) / chunkTime
func GetFactorOfTime(from, through int64, minTime, maxTime int64) (factor float64, leadingTime, trailingTime int64) {
func GetFactorOfTime(from, through int64, minTime, maxTime int64) (factor float64) {
if from > maxTime || through < minTime {
return 0
}
totalTime := maxTime - minTime
leadingTime = utilsMath.Max64(0, from-minTime)
trailingTime = utilsMath.Max64(0, maxTime-through)
leadingTime := utilsMath.Max64(0, from-minTime)
trailingTime := utilsMath.Max64(0, maxTime-through)
factor = float64(totalTime-(leadingTime+trailingTime)) / float64(totalTime)
return factor, leadingTime, trailingTime
return factor
}

@ -227,3 +227,53 @@ func TestForInterval(t *testing.T) {
})
}
}
func TestGetFactorOfTime(t *testing.T) {
for _, tc := range []struct {
desc string
from, through, extentMin, extentMax int64
exp float64
}{
{
desc: "equal",
from: 10, through: 20,
extentMin: 10, extentMax: 20,
exp: 1,
},
{
desc: "50% overlap on left",
from: 10, through: 20,
extentMin: 5, extentMax: 15,
exp: 0.5,
},
{
desc: "50% overlap on right",
from: 10, through: 20,
extentMin: 15, extentMax: 25,
exp: 0.5,
},
{
desc: "10% overlap on right",
from: 15, through: 16,
extentMin: 15, extentMax: 25,
exp: 0.1,
},
{
desc: "no overlap",
from: 10, through: 20,
extentMin: 25, extentMax: 35,
exp: 0,
},
{
desc: "no overlap, through=extentMin",
from: 10, through: 20,
extentMin: 20, extentMax: 35,
exp: 0,
},
} {
t.Run(tc.desc, func(t *testing.T) {
factor := GetFactorOfTime(tc.from, tc.through, tc.extentMin, tc.extentMax)
require.Equal(t, tc.exp, factor)
})
}
}

Loading…
Cancel
Save