From 2efd059b495088f61778d534fa4cb9a35513afb1 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Wed, 17 May 2023 13:46:59 -0400 Subject: [PATCH] Slight improvements to `GetFactorOfTime` (#9473) * correctly returns zero for non-overlapping data * adds tests --- pkg/querier/queryrange/index_stats_cache.go | 2 +- pkg/util/time.go | 12 +++-- pkg/util/time_test.go | 50 +++++++++++++++++++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/pkg/querier/queryrange/index_stats_cache.go b/pkg/querier/queryrange/index_stats_cache.go index d6cde9a8a1..c01f9c93a3 100644 --- a/pkg/querier/queryrange/index_stats_cache.go +++ b/pkg/querier/queryrange/index_stats_cache.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{ diff --git a/pkg/util/time.go b/pkg/util/time.go index 806e01c58c..cd80ceb66a 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -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 } diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index 2bd2fac114..074e53db7a 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -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) + }) + } +}