-
Notifications
You must be signed in to change notification settings - Fork 4
feat(backup): enhance backup progress tracking and state management #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| export type BackupProgressState = { | ||
| readonly processedItems: number; | ||
| readonly backupWeights: ReadonlyMap<string, number>; | ||
| readonly backupTotals: ReadonlyMap<string, number>; | ||
| readonly currentBackupId: string; | ||
| readonly completedBackups: ReadonlySet<string>; | ||
| }; | ||
|
|
||
| export const createInitialState = (): BackupProgressState => ({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for better structure, its better to use function rather than arrow function unless its necessary |
||
| processedItems: 0, | ||
| backupWeights: new Map(), | ||
| backupTotals: new Map(), | ||
| currentBackupId: '', | ||
| completedBackups: new Set(), | ||
| }); | ||
|
|
||
| export const initializeWeights = ( | ||
| state: BackupProgressState, | ||
| backupIds: string[], | ||
| fileCounts: ReadonlyMap<string, number>, | ||
| ): BackupProgressState => { | ||
| const totalFiles = Array.from(fileCounts.values()).reduce((a, b) => a + b, 0); | ||
|
|
||
| if (totalFiles === 0) { | ||
| return state; | ||
| } | ||
|
|
||
| const weights = new Map<string, number>(); | ||
| const totals = new Map<string, number>(); | ||
|
|
||
| backupIds.forEach((id) => { | ||
| const count = fileCounts.get(id) || 1; | ||
| weights.set(id, count / totalFiles); | ||
| totals.set(id, count); | ||
| }); | ||
|
|
||
| return { | ||
| ...state, | ||
| backupWeights: weights, | ||
| backupTotals: totals, | ||
| }; | ||
| }; | ||
|
|
||
| export const setCurrentBackupId = (state: BackupProgressState, backupId: string): BackupProgressState => ({ | ||
| ...state, | ||
| currentBackupId: backupId, | ||
| processedItems: 0, | ||
| }); | ||
|
|
||
| export const incrementProcessed = (state: BackupProgressState, count: number = 1): BackupProgressState => ({ | ||
| ...state, | ||
| processedItems: state.processedItems + count, | ||
| }); | ||
|
|
||
| export const markBackupAsCompleted = (state: BackupProgressState, backupId: string): BackupProgressState => ({ | ||
| ...state, | ||
| completedBackups: new Set([...state.completedBackups, backupId]), | ||
| }); | ||
|
|
||
| export const getPercentage = (state: BackupProgressState): number => { | ||
| let weightedProgress = 0; | ||
|
|
||
| for (const backupId of state.completedBackups) { | ||
| const weight = state.backupWeights.get(backupId) || 0; | ||
| weightedProgress += weight * 100; | ||
| } | ||
|
|
||
| if (state.backupWeights.has(state.currentBackupId) && !state.completedBackups.has(state.currentBackupId)) { | ||
| const currentWeight = state.backupWeights.get(state.currentBackupId)!; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. careful with this |
||
| const currentTotal = state.backupTotals.get(state.currentBackupId) || 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be always <=1 right? |
||
|
|
||
| if (currentTotal > 0) { | ||
| const backupProgress = (state.processedItems / currentTotal) * 100; | ||
| weightedProgress += currentWeight * backupProgress; | ||
| } | ||
| } | ||
|
|
||
| return Math.min(100, Math.round(weightedProgress)); | ||
| }; | ||
|
|
||
| export const resetState = (): BackupProgressState => createInitialState(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is not really neessary then |
||
|
|
||
| export const initializeAndSetBackup = ( | ||
| state: BackupProgressState, | ||
| backupIds: string[], | ||
| fileCounts: ReadonlyMap<string, number>, | ||
| firstBackupId: string, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be the first item of the array, right? |
||
| ): BackupProgressState => { | ||
| const weightedState = initializeWeights(state, backupIds, fileCounts); | ||
| return setCurrentBackupId(weightedState, firstBackupId); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file lacks tests