TestData: Drop some percentage of CSV values from a request (#70404)

This commit is contained in:
Ryan McKinley
2023-06-21 13:17:10 -05:00
committed by GitHub
parent 1db0ace5e7
commit ae688adabc
9 changed files with 97 additions and 9 deletions
+26
View File
@@ -3,6 +3,8 @@ package testdatasource
import (
"math/rand"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
)
type randomStringProvider struct {
@@ -20,3 +22,27 @@ func newRandomStringProvider(data []string) *randomStringProvider {
func (p *randomStringProvider) Next() string {
return p.data[p.r.Int31n(int32(len(p.data)))]
}
func dropValues(frame *data.Frame, percent float64) (*data.Frame, error) {
if frame == nil || percent <= 0 || percent >= 100 {
return frame, nil
}
rows, err := frame.RowLen()
copy := frame.EmptyCopy()
percentage := percent / 100.0
seed := time.Now().UnixMilli()
r := rand.New(rand.NewSource(seed))
for i := 0; i < rows; i++ {
if r.Float64() < percentage { // .2 == 20
continue
}
// copy the row
for fidx, f := range copy.Fields {
f.Append(frame.Fields[fidx].At(i))
}
}
return copy, err
}