@@ -76,7 +76,7 @@ func (d *DiagnosticClient) runSnapshotListener(rootCtx context.Context) {
7676 downloadTimeLeft := CalculateTime (remainingBytes , info .DownloadRate )
7777 totalDownloadTimeString := time .Duration (info .TotalTime ) * time .Second
7878
79- d .updateSnapshotStageStats (SyncStageStats {
79+ d .UpdateSnapshotStageStats (SyncStageStats {
8080 TimeElapsed : totalDownloadTimeString .String (),
8181 TimeLeft : downloadTimeLeft ,
8282 Progress : downloadedPercent ,
@@ -105,10 +105,14 @@ func GetShanpshotsPercentDownloaded(downloaded uint64, total uint64, torrentMeta
105105 return fmt .Sprintf ("%.2f%%" , percent )
106106}
107107
108- func (d * DiagnosticClient ) updateSnapshotStageStats (stats SyncStageStats , subStageInfo string ) {
108+ func (d * DiagnosticClient ) UpdateSnapshotStageStats (stats SyncStageStats , subStageInfo string ) {
109109 d .mu .Lock ()
110110 defer d .mu .Unlock ()
111- idxs := d .GetCurrentSyncIdxs ()
111+ d .updateSnapshotStageStats (stats , subStageInfo )
112+ }
113+
114+ func (d * DiagnosticClient ) updateSnapshotStageStats (stats SyncStageStats , subStageInfo string ) {
115+ idxs := d .getCurrentSyncIdxs ()
112116 if idxs .Stage == - 1 || idxs .SubStage == - 1 {
113117 log .Debug ("[Diagnostics] Can't find running stage or substage while updating Snapshots stage stats." , "stages:" , d .syncStages , "stats:" , stats , "subStageInfo:" , subStageInfo )
114118 return
@@ -181,8 +185,8 @@ func (d *DiagnosticClient) runSegmentIndexingListener(rootCtx context.Context) {
181185 return
182186 case info := <- ch :
183187 d .addOrUpdateSegmentIndexingState (info )
184- d . updateIndexingStatus ()
185- if d . syncStats . SnapshotIndexing . IndexingFinished {
188+ indexingFinished := d . UpdateIndexingStatus ()
189+ if indexingFinished {
186190 d .SaveData ()
187191 return
188192 }
@@ -222,20 +226,20 @@ func (d *DiagnosticClient) runSegmentIndexingFinishedListener(rootCtx context.Co
222226
223227 d .mu .Unlock ()
224228
225- d .updateIndexingStatus ()
229+ d .UpdateIndexingStatus ()
226230 }
227231 }
228232 }()
229233}
230234
231- func (d * DiagnosticClient ) updateIndexingStatus ( ) {
235+ func (d * DiagnosticClient ) UpdateIndexingStatus () ( indexingFinished bool ) {
232236 totalProgressPercent := 0
233237 d .mu .Lock ()
238+ defer d .mu .Unlock ()
234239
235240 for _ , seg := range d .syncStats .SnapshotIndexing .Segments {
236241 totalProgressPercent += seg .Percent
237242 }
238- d .mu .Unlock ()
239243
240244 totalProgress := totalProgressPercent / len (d .syncStats .SnapshotIndexing .Segments )
241245
@@ -248,6 +252,7 @@ func (d *DiagnosticClient) updateIndexingStatus() {
248252 if totalProgress >= 100 {
249253 d .syncStats .SnapshotIndexing .IndexingFinished = true
250254 }
255+ return d .syncStats .SnapshotIndexing .IndexingFinished
251256}
252257
253258func (d * DiagnosticClient ) addOrUpdateSegmentIndexingState (upd SnapshotIndexingStatistics ) {
@@ -399,7 +404,7 @@ func (d *DiagnosticClient) runFillDBListener(rootCtx context.Context) {
399404
400405 totalTimeString := time .Duration (info .TimeElapsed ) * time .Second
401406
402- d .updateSnapshotStageStats (SyncStageStats {
407+ d .UpdateSnapshotStageStats (SyncStageStats {
403408 TimeElapsed : totalTimeString .String (),
404409 TimeLeft : "unknown" ,
405410 Progress : fmt .Sprintf ("%d%%" , (info .Stage .Current * 100 )/ info .Stage .Total ),
0 commit comments