Commit b010d069a996cc4607a7951dd7ede6898688f52a
dx: improve drawer progress estimate calculation
Andre Staltz committed on 3/5/2021, 10:31:23 AMParent: 3edce198aabd29880218a253eae3c4c70be2d33f
Files changed
src/frontend/screens/drawer/model.ts | changed |
src/frontend/screens/drawer/model.ts | ||
---|---|---|
@@ -15,8 +15,9 @@ | ||
15 | 15 | indexingProgress: number; |
16 | 16 | combinedProgress: number; |
17 | 17 | checkpointCombinedProgress: number; |
18 | 18 | checkpoint: number; |
19 | + recentEstimates: Array<number>; | |
19 | 20 | estimateProgressDone: number; |
20 | 21 | canPublishSSB: boolean; |
21 | 22 | selfAvatarUrl?: string; |
22 | 23 | name?: string; |
@@ -44,8 +45,9 @@ | ||
44 | 45 | indexingProgress: 0, |
45 | 46 | combinedProgress: 0, |
46 | 47 | checkpointCombinedProgress: 0, |
47 | 48 | checkpoint: Date.now(), |
49 | + recentEstimates: [], | |
48 | 50 | estimateProgressDone: 0, |
49 | 51 | canPublishSSB: true, |
50 | 52 | }; |
51 | 53 | } else { |
@@ -72,8 +74,9 @@ | ||
72 | 74 | migrationProgress: 0, |
73 | 75 | indexingProgress: 0, |
74 | 76 | combinedProgress: 0, |
75 | 77 | checkpointCombinedProgress: 0, |
78 | + recentEstimates: [], | |
76 | 79 | estimateProgressDone: 0, |
77 | 80 | checkpoint: Date.now(), |
78 | 81 | canPublishSSB: true, |
79 | 82 | }; |
@@ -90,16 +93,22 @@ | ||
90 | 93 | function updateEstimateProgressDone_mutating(state: State) { |
91 | 94 | const now = Date.now(); |
92 | 95 | if (state.combinedProgress <= 0 || state.combinedProgress >= 1) { |
93 | 96 | state.estimateProgressDone = 0; |
97 | + state.recentEstimates = []; | |
94 | 98 | state.checkpoint = now; |
95 | 99 | } else if (state.checkpoint + CHECKPOINT_INTERVAL < now) { |
96 | 100 | const rateOfProgress = |
97 | 101 | (state.combinedProgress - state.checkpointCombinedProgress) / |
98 | 102 | (now - state.checkpoint); |
99 | 103 | const remaining = 1 - state.combinedProgress; |
100 | 104 | if (state.combinedProgress > state.checkpointCombinedProgress) { |
101 | - state.estimateProgressDone = remaining / rateOfProgress; | |
105 | + // Calculate a moving average of the last 6 estimates | |
106 | + state.recentEstimates.push(remaining / rateOfProgress); | |
107 | + if (state.recentEstimates.length > 3) state.recentEstimates.shift(); | |
108 | + state.estimateProgressDone = | |
109 | + state.recentEstimates.reduce((acc, x) => acc + x, 0) / | |
110 | + state.recentEstimates.length; | |
102 | 111 | } |
103 | 112 | state.checkpointCombinedProgress = state.combinedProgress; |
104 | 113 | state.checkpoint = now; |
105 | 114 | } |
Built with git-ssb-web