CloudMigrations: replace slicesext.Chunk with stdlib implementation (#93743)

This commit is contained in:
Matheus Macabu 2024-09-25 16:52:22 +02:00 committed by GitHub
parent 5880579be8
commit 1e720306dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 2 additions and 115 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"time"
@ -13,7 +14,6 @@ import (
"github.com/grafana/grafana-cloud-migration-snapshot/src/contracts"
"github.com/grafana/grafana-cloud-migration-snapshot/src/infra/crypto"
"github.com/grafana/grafana/pkg/services/cloudmigration"
"github.com/grafana/grafana/pkg/services/cloudmigration/slicesext"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt"
@ -230,7 +230,7 @@ func (s *Service) buildSnapshot(ctx context.Context, signedInUser *user.SignedIn
cloudmigration.FolderDataType,
cloudmigration.DashboardDataType,
} {
for _, chunk := range slicesext.Chunks(int(maxItemsPerPartition), resourcesGroupedByType[resourceType]) {
for chunk := range slices.Chunk(resourcesGroupedByType[resourceType], int(maxItemsPerPartition)) {
if err := snapshotWriter.Write(string(resourceType), chunk); err != nil {
return fmt.Errorf("writing resources to snapshot writer: resourceType=%s %w", resourceType, err)
}

View File

@ -1,33 +0,0 @@
package slicesext
import "math"
// Partitions the input into slices where the length is <= chunkSize.
//
// Example:
//
// Chunks(2, []int{1, 2, 3, 4})
// => [][]int{{1, 2}, {3, 4}}
func Chunks[T any](chunkSize int, xs []T) [][]T {
if chunkSize < 0 {
panic("chunk size must be greater than or equal to 0")
}
if chunkSize == 0 {
return [][]T{}
}
out := make([][]T, 0, int(math.Ceil(float64(len(xs))/float64(chunkSize))))
for i := 0; i < len(xs); i += chunkSize {
var chunk []T
if i+chunkSize < len(xs) {
chunk = xs[i : i+chunkSize]
} else {
chunk = xs[i:]
}
out = append(out, chunk)
}
return out
}

View File

@ -1,80 +0,0 @@
package slicesext
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestChunks(t *testing.T) {
t.Parallel()
t.Run("chunkSize must be greater than 0", func(t *testing.T) {
t.Parallel()
assert.PanicsWithValue(t, "chunk size must be greater than or equal to 0", func() {
Chunks(-1, []string{})
})
})
t.Run("basic", func(t *testing.T) {
t.Parallel()
cases := []struct {
description string
chunkSize int
input []int
expected [][]int
}{
{
description: "empty slice",
chunkSize: 2,
input: []int{},
expected: [][]int{},
},
{
description: "nil slice",
chunkSize: 2,
input: nil,
expected: [][]int{},
},
{
description: "chunk size is 0",
chunkSize: 0,
input: []int{1, 2, 3},
expected: [][]int{},
},
{
description: "chunk size is greater than slice length",
chunkSize: 3,
input: []int{1},
expected: [][]int{{1}},
},
{
description: "chunk size is 1",
chunkSize: 1,
input: []int{1, 2, 3},
expected: [][]int{{1}, {2}, {3}},
},
{
description: "chunk size is 2 and slice length is 3",
chunkSize: 2,
input: []int{1, 2, 3},
expected: [][]int{{1, 2}, {3}},
},
{
description: "chunk size is 2 and slice length is 6",
chunkSize: 2,
input: []int{1, 2, 3, 4, 5, 6},
expected: [][]int{{1, 2}, {3, 4}, {5, 6}},
},
}
for _, tt := range cases {
t.Run(tt.description, func(t *testing.T) {
result := Chunks(tt.chunkSize, tt.input)
assert.Equal(t, tt.expected, result)
})
}
})
}