CloudMigrations: Add sorting and error filtering to Snapshot Results backend (#102753)

* implement sorting

* swagger gen

* minor fixes

* clean up param reading

* add todo

* add errors only prop

* codegen stuff

* fix copy paste error

* forgot the api gen

* cleanup

* remove tests that are obe

* fix test
This commit is contained in:
Michael Mandrus
2025-03-30 10:42:45 -04:00
committed by GitHub
parent f84654d162
commit 88e51d549c
13 changed files with 438 additions and 40 deletions
@@ -241,7 +241,7 @@ func (ss *sqlStore) deleteSnapshot(ctx context.Context, snapshotUid string) erro
})
}
func (ss *sqlStore) GetSnapshotByUID(ctx context.Context, orgID int64, sessionUid, uid string, resultPage int, resultLimit int) (*cloudmigration.CloudMigrationSnapshot, error) {
func (ss *sqlStore) GetSnapshotByUID(ctx context.Context, orgID int64, sessionUid, uid string, params cloudmigration.SnapshotResultQueryParams) (*cloudmigration.CloudMigrationSnapshot, error) {
// first we check if the session exists, using orgId and sessionUid
session, err := ss.GetMigrationSessionByUID(ctx, orgID, sessionUid)
if err != nil || session == nil {
@@ -272,7 +272,7 @@ func (ss *sqlStore) GetSnapshotByUID(ctx context.Context, orgID int64, sessionUi
snapshot.EncryptionKey = []byte(secret)
}
resources, err := ss.getSnapshotResources(ctx, uid, resultPage, resultLimit)
resources, err := ss.getSnapshotResources(ctx, uid, params)
if err == nil {
snapshot.Resources = resources
}
@@ -421,19 +421,17 @@ func (ss *sqlStore) UpdateSnapshotResources(ctx context.Context, snapshotUid str
})
}
func (ss *sqlStore) getSnapshotResources(ctx context.Context, snapshotUid string, page int, limit int) ([]cloudmigration.CloudMigrationResource, error) {
if page < 1 {
page = 1
}
if limit == 0 {
limit = 100
}
func (ss *sqlStore) getSnapshotResources(ctx context.Context, snapshotUid string, params cloudmigration.SnapshotResultQueryParams) ([]cloudmigration.CloudMigrationResource, error) {
page, limit, col, dir, errorsOnly := int(params.ResultPage), int(params.ResultLimit), string(params.SortColumn), string(params.SortOrder), params.ErrorsOnly
var resources []cloudmigration.CloudMigrationResource
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
offset := (page - 1) * limit
sess.Limit(limit, offset)
return sess.OrderBy("id ASC").Find(&resources, &cloudmigration.CloudMigrationResource{
if errorsOnly {
sess.Where("status = ?", cloudmigration.ItemStatusError)
}
return sess.OrderBy(fmt.Sprintf("%s %s", col, dir)).Find(&resources, &cloudmigration.CloudMigrationResource{
SnapshotUID: snapshotUid,
})
})