add left time for upload

This commit is contained in:
tickstep 2024-08-16 22:57:15 +08:00
parent 7132342ed3
commit 6f45a8048d
2 changed files with 24 additions and 1 deletions

View File

@ -14,6 +14,7 @@
package uploader package uploader
import ( import (
"sync/atomic"
"time" "time"
) )
@ -24,6 +25,7 @@ type (
Uploaded() int64 // 已上传数据 Uploaded() int64 // 已上传数据
SpeedsPerSecond() int64 // 每秒的上传速度 SpeedsPerSecond() int64 // 每秒的上传速度
TimeElapsed() time.Duration // 上传时间 TimeElapsed() time.Duration // 上传时间
TimeLeft() time.Duration // 预计剩余时间, 负数代表未知
} }
// UploadStatus 上传状态 // UploadStatus 上传状态
@ -67,6 +69,18 @@ func (us *UploadStatus) TimeElapsed() time.Duration {
return us.timeElapsed return us.timeElapsed
} }
// TimeLeft 返回预计剩余时间, 负数代表未知
func (us *UploadStatus) TimeLeft() time.Duration {
var left time.Duration
speeds := atomic.LoadInt64(&us.speedsPerSecond)
if speeds <= 0 {
left = -1
} else {
left = time.Duration((us.totalSize-us.uploaded)/(speeds)) * time.Second
}
return left
}
// GetStatusChan 获取上传状态 // GetStatusChan 获取上传状态
func (u *Uploader) GetStatusChan() <-chan Status { func (u *Uploader) GetStatusChan() <-chan Status {
c := make(chan Status) c := make(chan Status)

View File

@ -183,14 +183,23 @@ func (utu *UploadTaskUnit) upload() (result *taskframework.TaskUnitRunResult) {
} }
if utu.ShowProgress { if utu.ShowProgress {
// 如果下载速度为0, 剩余下载时间未知, 则用 - 代替
var leftStr string
left := status.TimeLeft()
if left < 0 {
leftStr = "-"
} else {
leftStr = left.String()
}
uploadedPercentage := fmt.Sprintf("%.2f%%", float64(status.Uploaded())/float64(status.TotalSize())*100) uploadedPercentage := fmt.Sprintf("%.2f%%", float64(status.Uploaded())/float64(status.TotalSize())*100)
fmt.Printf("\r[%s] ↑ %s/%s(%s) %s/s(%s/s) in %s ............", utu.taskInfo.Id(), fmt.Printf("\r[%s] ↑ %s/%s(%s) %s/s(%s/s) in %s, left %s ............", utu.taskInfo.Id(),
converter.ConvertFileSize(status.Uploaded(), 2), converter.ConvertFileSize(status.Uploaded(), 2),
converter.ConvertFileSize(status.TotalSize(), 2), converter.ConvertFileSize(status.TotalSize(), 2),
uploadedPercentage, uploadedPercentage,
converter.ConvertFileSize(status.SpeedsPerSecond(), 2), converter.ConvertFileSize(status.SpeedsPerSecond(), 2),
converter.ConvertFileSize(utu.GlobalSpeedsStat.GetSpeeds(), 2), converter.ConvertFileSize(utu.GlobalSpeedsStat.GetSpeeds(), 2),
status.TimeElapsed(), status.TimeElapsed(),
leftStr,
) )
} }
}) })