CloudMigrations: Limit frontend query to get latest snapshots (#93639)
* latest param to endpoint and adapt frontend query * change to sort param * api * remove description
This commit is contained in:
@@ -3,6 +3,7 @@ package fake
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/cloudmigration"
|
||||
@@ -108,18 +109,31 @@ func (m FakeServiceImpl) GetSnapshotList(ctx context.Context, query cloudmigrati
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
}
|
||||
return []cloudmigration.CloudMigrationSnapshot{
|
||||
|
||||
cloudSnapshots := []cloudmigration.CloudMigrationSnapshot{
|
||||
{
|
||||
UID: "fake_uid",
|
||||
SessionUID: query.SessionUID,
|
||||
Status: cloudmigration.SnapshotStatusCreating,
|
||||
Created: time.Date(2024, 6, 5, 17, 30, 40, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
UID: "fake_uid",
|
||||
SessionUID: query.SessionUID,
|
||||
Status: cloudmigration.SnapshotStatusCreating,
|
||||
Created: time.Date(2024, 6, 5, 18, 30, 40, 0, time.UTC),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
if query.Sort == "latest" {
|
||||
sort.Slice(cloudSnapshots, func(first, second int) bool {
|
||||
return cloudSnapshots[first].Created.After(cloudSnapshots[second].Created)
|
||||
})
|
||||
}
|
||||
if query.Limit > 0 {
|
||||
return cloudSnapshots[0:min(len(cloudSnapshots), query.Limit)], nil
|
||||
}
|
||||
return cloudSnapshots, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) UploadSnapshot(ctx context.Context, sessionUid string, snapshotUid string) error {
|
||||
|
||||
@@ -23,9 +23,10 @@ type sqlStore struct {
|
||||
}
|
||||
|
||||
const (
|
||||
tableName = "cloud_migration_resource"
|
||||
secretType = "cloudmigration-snapshot-encryption-key"
|
||||
GetAllSnapshots = -1
|
||||
tableName = "cloud_migration_resource"
|
||||
secretType = "cloudmigration-snapshot-encryption-key"
|
||||
GetAllSnapshots = -1
|
||||
GetSnapshotListSortingLatest = "latest"
|
||||
)
|
||||
|
||||
func (ss *sqlStore) GetMigrationSessionByUID(ctx context.Context, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
@@ -278,7 +279,9 @@ func (ss *sqlStore) GetSnapshotList(ctx context.Context, query cloudmigration.Li
|
||||
offset := (query.Page - 1) * query.Limit
|
||||
sess.Limit(query.Limit, offset)
|
||||
}
|
||||
sess.OrderBy("cloud_migration_snapshot.created DESC")
|
||||
if query.Sort == GetSnapshotListSortingLatest {
|
||||
sess.OrderBy("cloud_migration_snapshot.created DESC")
|
||||
}
|
||||
return sess.Find(&snapshots, &cloudmigration.CloudMigrationSnapshot{
|
||||
SessionUID: query.SessionUID,
|
||||
})
|
||||
|
||||
@@ -3,7 +3,6 @@ package cloudmigrationimpl
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"slices"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
@@ -241,9 +240,46 @@ func TestGetSnapshotList(t *testing.T) {
|
||||
for _, snapshot := range snapshots {
|
||||
ids = append(ids, snapshot.UID)
|
||||
}
|
||||
slices.Sort(ids)
|
||||
|
||||
// There are 3 snapshots in the db but only 2 of them belong to this specific session.
|
||||
assert.Equal(t, []string{"poiuy", "lkjhg"}, ids)
|
||||
})
|
||||
|
||||
t.Run("returns only one snapshot that belongs to a session", func(t *testing.T) {
|
||||
snapshots, err := s.GetSnapshotList(ctx, cloudmigration.ListSnapshotsQuery{SessionUID: sessionUID, Page: 1, Limit: 1})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, snapshots, 1)
|
||||
})
|
||||
|
||||
t.Run("return no snapshots if limit is set to 0", func(t *testing.T) {
|
||||
snapshots, err := s.GetSnapshotList(ctx, cloudmigration.ListSnapshotsQuery{SessionUID: sessionUID, Page: 1, Limit: 0})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, snapshots)
|
||||
})
|
||||
|
||||
t.Run("returns paginated snapshot that belongs to a session", func(t *testing.T) {
|
||||
snapshots, err := s.GetSnapshotList(ctx, cloudmigration.ListSnapshotsQuery{SessionUID: sessionUID, Page: 2, Limit: 1})
|
||||
require.NoError(t, err)
|
||||
|
||||
ids := make([]string, 0)
|
||||
for _, snapshot := range snapshots {
|
||||
ids = append(ids, snapshot.UID)
|
||||
}
|
||||
|
||||
// Return paginated snapshot of the 2 belonging to this specific session
|
||||
assert.Equal(t, []string{"lkjhg"}, ids)
|
||||
})
|
||||
|
||||
t.Run("returns desc sorted list of snapshots that belong to a session", func(t *testing.T) {
|
||||
snapshots, err := s.GetSnapshotList(ctx, cloudmigration.ListSnapshotsQuery{SessionUID: sessionUID, Page: 1, Limit: 100, Sort: "latest"})
|
||||
require.NoError(t, err)
|
||||
|
||||
ids := make([]string, 0)
|
||||
for _, snapshot := range snapshots {
|
||||
ids = append(ids, snapshot.UID)
|
||||
}
|
||||
|
||||
// Return desc sorted snapshots belonging to this specific session
|
||||
assert.Equal(t, []string{"lkjhg", "poiuy"}, ids)
|
||||
})
|
||||
|
||||
@@ -345,7 +381,7 @@ func setUpTest(t *testing.T) (*sqlstore.SQLStore, *sqlStore) {
|
||||
cloud_migration_snapshot (session_uid, uid, created, updated, finished, status)
|
||||
VALUES
|
||||
('qwerty', 'poiuy', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000', '2024-03-27 15:30:43.000', "finished"),
|
||||
('qwerty', 'lkjhg', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000', '2024-03-27 15:30:43.000', "finished"),
|
||||
('qwerty', 'lkjhg', '2024-03-26 15:30:36.000', '2024-03-27 15:30:43.000', '2024-03-27 15:30:43.000', "finished"),
|
||||
('zxcvbn', 'mnbvvc', '2024-03-25 15:30:36.000', '2024-03-27 15:30:43.000', '2024-03-27 15:30:43.000', "finished");
|
||||
`,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user