mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-20 10:15:22 -04:00
Improve speed reporting
This commit is contained in:
parent
461d6f4a84
commit
e143f8531d
2 changed files with 49 additions and 8 deletions
|
@ -9,6 +9,44 @@ import (
|
|||
"github.com/gotk3/gotk3/gtk"
|
||||
)
|
||||
|
||||
const (
|
||||
MAX_SPEEDS = 32
|
||||
SMOOTHING_FACTOR = 0.2
|
||||
)
|
||||
|
||||
type SpeedAverager struct {
|
||||
speeds []int64
|
||||
averageSpeed int64
|
||||
}
|
||||
|
||||
func newSpeedAverager() *SpeedAverager {
|
||||
return &SpeedAverager{
|
||||
speeds: make([]int64, MAX_SPEEDS),
|
||||
averageSpeed: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (sa *SpeedAverager) AddSpeed(speed int64) {
|
||||
if len(sa.speeds) >= MAX_SPEEDS {
|
||||
copy(sa.speeds[:MAX_SPEEDS/2], sa.speeds[MAX_SPEEDS/2:])
|
||||
sa.speeds = sa.speeds[:MAX_SPEEDS/2]
|
||||
}
|
||||
sa.speeds = append(sa.speeds, speed)
|
||||
}
|
||||
|
||||
func (sa *SpeedAverager) calculateAverageOfSpeeds() {
|
||||
var total int64
|
||||
for _, speed := range sa.speeds {
|
||||
total += speed
|
||||
}
|
||||
sa.averageSpeed = total / int64(len(sa.speeds))
|
||||
}
|
||||
|
||||
func (sa *SpeedAverager) GetAverageSpeed() float64 {
|
||||
sa.calculateAverageOfSpeeds()
|
||||
return SMOOTHING_FACTOR*float64(sa.speeds[len(sa.speeds)-1]) + (1-SMOOTHING_FACTOR)*float64(sa.averageSpeed)
|
||||
}
|
||||
|
||||
type ProgressWindow struct {
|
||||
Window *gtk.Window
|
||||
box *gtk.Box
|
||||
|
@ -19,6 +57,7 @@ type ProgressWindow struct {
|
|||
cancelFunc context.CancelFunc
|
||||
totalToDownload int64
|
||||
totalDownloaded int64
|
||||
speedAverager *SpeedAverager
|
||||
}
|
||||
|
||||
func (pw *ProgressWindow) SetGameTitle(title string) {
|
||||
|
@ -35,7 +74,8 @@ func (pw *ProgressWindow) UpdateDownloadProgress(downloaded, speed int64, filePa
|
|||
pw.cancelButton.SetSensitive(true)
|
||||
currentDownload := downloaded + pw.totalDownloaded
|
||||
pw.bar.SetFraction(float64(currentDownload) / float64(pw.totalToDownload))
|
||||
pw.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(currentDownload)), humanize.Bytes(uint64(pw.totalToDownload)), humanize.Bytes(uint64(speed))))
|
||||
pw.speedAverager.AddSpeed(speed)
|
||||
pw.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(currentDownload)), humanize.Bytes(uint64(pw.totalToDownload)), humanize.Bytes(uint64(int64(pw.speedAverager.GetAverageSpeed())))))
|
||||
})
|
||||
for gtk.EventsPending() {
|
||||
gtk.MainIteration()
|
||||
|
@ -118,12 +158,13 @@ func createProgressWindow(parent *gtk.ApplicationWindow) (*ProgressWindow, error
|
|||
box.PackEnd(bottomhBox, false, false, 0)
|
||||
|
||||
progressWindow := ProgressWindow{
|
||||
Window: win,
|
||||
box: box,
|
||||
gameLabel: gameLabel,
|
||||
bar: progressBar,
|
||||
cancelButton: cancelButton,
|
||||
cancelled: false,
|
||||
Window: win,
|
||||
box: box,
|
||||
gameLabel: gameLabel,
|
||||
bar: progressBar,
|
||||
cancelButton: cancelButton,
|
||||
cancelled: false,
|
||||
speedAverager: newSpeedAverager(),
|
||||
}
|
||||
|
||||
progressWindow.cancelButton.Connect("clicked", func() {
|
||||
|
|
|
@ -43,7 +43,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
|||
filePath := filepath.Base(dstPath)
|
||||
|
||||
startTime := time.Now()
|
||||
ticker := time.NewTicker(250 * time.Millisecond)
|
||||
ticker := time.NewTicker(50 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
isError := false
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue