-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add speed and ETA tracking to progress
- Loading branch information
1 parent
45e34cc
commit 7001f5f
Showing
4 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package utils | ||
|
||
import ( | ||
"maps" | ||
"time" | ||
) | ||
|
||
type ProgressTracker struct { | ||
windowSize time.Duration | ||
data map[time.Time]int64 | ||
Total int64 | ||
} | ||
|
||
func NewProgressTracker(windowSize time.Duration) *ProgressTracker { | ||
return &ProgressTracker{ | ||
windowSize: windowSize, | ||
data: make(map[time.Time]int64), | ||
} | ||
} | ||
|
||
func (pt *ProgressTracker) Add(value int64) { | ||
pt.Total += value | ||
pt.data[time.Now()] = value | ||
pt.evict() | ||
} | ||
|
||
func (pt *ProgressTracker) Speed() float64 { | ||
pt.evict() | ||
var first, last time.Time | ||
for t := range pt.data { | ||
if first.IsZero() || t.Before(first) { | ||
first = t | ||
} | ||
if last.IsZero() || t.After(last) { | ||
last = t | ||
} | ||
} | ||
firstData := pt.data[first] | ||
lastData := pt.data[last] | ||
return float64(lastData-firstData) / pt.windowSize.Seconds() | ||
} | ||
|
||
func (pt *ProgressTracker) ETA() time.Duration { | ||
speed := pt.Speed() | ||
if speed == 0 { | ||
return 0 | ||
} | ||
var latest time.Time | ||
for t := range pt.data { | ||
if latest.IsZero() || t.After(latest) { | ||
latest = t | ||
} | ||
} | ||
latestData := pt.data[latest] | ||
return time.Duration(float64(pt.Total-latestData)/speed) * time.Second | ||
} | ||
|
||
func (pt *ProgressTracker) evict() { | ||
maps.DeleteFunc(pt.data, func(t time.Time, _ int64) bool { | ||
return time.Since(t) > pt.windowSize | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters