From 1940d3a1f16c6493a91658a00470a62aea5e77ca Mon Sep 17 00:00:00 2001 From: Abhishek Pandey Date: Fri, 22 Dec 2023 17:15:48 -0800 Subject: [PATCH] Minor refactor of status update code (#4912) I missed this change in https://github.com/alcionai/corso/pull/4906. Moving out the status updater code out of `prefetchCollection` scope so that `lazyFetchCollection` can also use it in upcoming PRs. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [x] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- .../m365/collection/groups/collection.go | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/internal/m365/collection/groups/collection.go b/src/internal/m365/collection/groups/collection.go index 5029f5cf5..b8ee2c8b2 100644 --- a/src/internal/m365/collection/groups/collection.go +++ b/src/internal/m365/collection/groups/collection.go @@ -28,6 +28,33 @@ const ( numberOfRetries = 4 ) +// updateStatus is a utility function used to send the status update through +// the channel. +func updateStatus( + ctx context.Context, + statusUpdater support.StatusUpdater, + attempted int, + streamedItems int64, + totalBytes int64, + folderPath string, + err error, +) { + status := support.CreateStatus( + ctx, + support.Backup, + 1, + support.CollectionMetrics{ + Objects: attempted, + Successes: int(streamedItems), + Bytes: totalBytes, + }, + folderPath) + + logger.Ctx(ctx).Debugw("done streaming items", "status", status.String()) + + statusUpdater(status) +} + type prefetchCollection[C graph.GetIDer, I groupsItemer] struct { data.BaseCollection protectedResource string @@ -97,10 +124,19 @@ func (col *prefetchCollection[C, I]) streamItems(ctx context.Context, errs *faul ctx = clues.Add(ctx, "category", col.Category().String()) defer func() { + close(col.stream) logger.Ctx(ctx).Infow( "finished stream backup collection items", "stats", col.Counter.Values()) - col.finishPopulation(ctx, streamedItems, totalBytes, errs.Failure()) + + updateStatus( + ctx, + col.statusUpdater, + len(col.added)+len(col.removed), + streamedItems, + totalBytes, + col.FullPath().Folder(false), + errs.Failure()) }() if len(col.added)+len(col.removed) > 0 { @@ -212,29 +248,3 @@ func (col *prefetchCollection[C, I]) streamItems(ctx context.Context, errs *faul wg.Wait() } - -// finishPopulation is a utility function used to close a collection's data channel -// and to send the status update through the channel. -func (col *prefetchCollection[C, I]) finishPopulation( - ctx context.Context, - streamedItems, totalBytes int64, - err error, -) { - close(col.stream) - - attempted := len(col.added) + len(col.removed) - status := support.CreateStatus( - ctx, - support.Backup, - 1, - support.CollectionMetrics{ - Objects: attempted, - Successes: int(streamedItems), - Bytes: totalBytes, - }, - col.FullPath().Folder(false)) - - logger.Ctx(ctx).Debugw("done streaming items", "status", status.String()) - - col.statusUpdater(status) -}