simplify readability of timeseries filtering by using the slices package

Signed-off-by: Callum Styan <callumstyan@gmail.com>
This commit is contained in:
Callum Styan 2024-06-19 14:08:32 -07:00
parent f9ca6c4ae6
commit 9ef2bcd9db

View file

@ -17,6 +17,7 @@ import (
"context" "context"
"errors" "errors"
"math" "math"
"slices"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -1741,10 +1742,12 @@ func buildTimeSeries(timeSeries []prompb.TimeSeries, filter func(prompb.TimeSeri
var lowest int64 var lowest int64
var droppedSamples, droppedExemplars, droppedHistograms int var droppedSamples, droppedExemplars, droppedHistograms int
keepIdx := 0 //keepIdx := 0
lowest = math.MaxInt64 lowest = math.MaxInt64
for i, ts := range timeSeries { //for i, ts := range timeSeries {
timeSeries = slices.DeleteFunc(timeSeries, func(ts prompb.TimeSeries) bool {
if filter != nil && filter(ts) { if filter != nil && filter(ts) {
//fmt.Println("deleteing")
if len(ts.Samples) > 0 { if len(ts.Samples) > 0 {
droppedSamples++ droppedSamples++
} }
@ -1754,7 +1757,7 @@ func buildTimeSeries(timeSeries []prompb.TimeSeries, filter func(prompb.TimeSeri
if len(ts.Histograms) > 0 { if len(ts.Histograms) > 0 {
droppedHistograms++ droppedHistograms++
} }
continue return true
} }
// At the moment we only ever append a TimeSeries with a single sample or exemplar in it. // At the moment we only ever append a TimeSeries with a single sample or exemplar in it.
@ -1778,13 +1781,14 @@ func buildTimeSeries(timeSeries []prompb.TimeSeries, filter func(prompb.TimeSeri
if len(ts.Histograms) > 0 && ts.Histograms[0].Timestamp < lowest { if len(ts.Histograms) > 0 && ts.Histograms[0].Timestamp < lowest {
lowest = ts.Histograms[0].Timestamp lowest = ts.Histograms[0].Timestamp
} }
return false
// Move the current element to the write position and increment the write pointer // Move the current element to the write position and increment the write pointer
timeSeries[keepIdx] = timeSeries[i] //timeSeries[keepIdx] = timeSeries[i]
keepIdx++ //keepIdx++
} })
timeSeries = timeSeries[:keepIdx] //timeSeries = timeSeries[:keepIdx]
//fmt.Println("timeseries: ", timeSeries)
return highest, lowest, timeSeries, droppedSamples, droppedExemplars, droppedHistograms return highest, lowest, timeSeries, droppedSamples, droppedExemplars, droppedHistograms
} }