change upload stats report

This commit is contained in:
Celogeek 2024-03-03 17:15:38 +01:00
parent 2df5388b4c
commit 7969ec305f
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc

View File

@ -2,43 +2,73 @@ package piwigotools
import ( import (
"fmt" "fmt"
"github.com/apoorvam/goterminal"
ct "github.com/daviddengcn/go-colortext"
"os" "os"
"strings"
"sync" "sync"
"time"
)
"github.com/schollz/progressbar/v3" const (
megabytes = 1 << 20
kilobytes = 1 << 10
) )
type FileToUploadStat struct { type FileToUploadStat struct {
Checked int64 Checked uint32
Total int64 Total uint32
TotalBytes int64 Uploaded uint32
Uploaded int64 Skipped uint32
Failed uint32
UploadedBytes int64 UploadedBytes int64
Skipped int64 TotalBytes int64
Failed int64
Progress *progressbar.ProgressBar progress *goterminal.Writer
mu sync.Mutex mu sync.Mutex
lastRefresh time.Time
} }
func NewFileToUploadStat() *FileToUploadStat { func NewFileToUploadStat() *FileToUploadStat {
bar := progressbar.DefaultBytes(1, "...")
progressbar.OptionOnCompletion(func() { _, _ = os.Stderr.WriteString("\n") })(bar)
return &FileToUploadStat{ return &FileToUploadStat{
Progress: bar, progress: goterminal.New(os.Stdout),
} }
} }
func (s *FileToUploadStat) Refresh() { func (s *FileToUploadStat) formatBytes(b int64) string {
s.Progress.Describe(fmt.Sprintf( if b > megabytes {
"%d / %d - check:%d, upload:%d, skip:%d, fail:%d", return fmt.Sprintf("%.2fMB", float64(b)/(1<<20))
s.Uploaded+s.Skipped+s.Failed, } else if b > kilobytes {
s.Total, return fmt.Sprintf("%.2fKB", float64(b)/(1<<10))
s.Checked, } else {
s.Uploaded, return fmt.Sprintf("%dB", b)
s.Skipped, }
s.Failed, }
),
func (s *FileToUploadStat) refreshIfNeeded() {
if time.Since(s.lastRefresh) > 200*time.Millisecond {
s.refresh()
}
}
func (s *FileToUploadStat) refresh() {
s.progress.Clear()
_, _ = s.progress.Write([]byte("Statistics:\n"))
_, _ = fmt.Fprintf(
s.progress,
strings.Repeat("%20s: %d\n", 5)+strings.Repeat("%20s: %s\n", 2),
"Checked", s.Checked,
"Uploaded", s.Uploaded,
"Skipped", s.Skipped,
"Failed", s.Failed,
"Total", s.Total,
"Uploaded Bytes", s.formatBytes(s.UploadedBytes),
"Total Bytes", s.formatBytes(s.TotalBytes),
) )
_ = s.progress.Print()
s.lastRefresh = time.Now()
} }
func (s *FileToUploadStat) Check() { func (s *FileToUploadStat) Check() {
@ -46,16 +76,7 @@ func (s *FileToUploadStat) Check() {
defer s.mu.Unlock() defer s.mu.Unlock()
s.Checked++ s.Checked++
s.Refresh() s.refreshIfNeeded()
}
func (s *FileToUploadStat) AddBytes(filesize int64) {
s.mu.Lock()
defer s.mu.Unlock()
s.TotalBytes += filesize
s.Progress.ChangeMax64(s.TotalBytes + 1)
s.Refresh()
} }
func (s *FileToUploadStat) Add() { func (s *FileToUploadStat) Add() {
@ -63,7 +84,15 @@ func (s *FileToUploadStat) Add() {
defer s.mu.Unlock() defer s.mu.Unlock()
s.Total++ s.Total++
s.Refresh() s.refreshIfNeeded()
}
func (s *FileToUploadStat) AddBytes(filesize int64) {
s.mu.Lock()
defer s.mu.Unlock()
s.TotalBytes += filesize
s.refreshIfNeeded()
} }
func (s *FileToUploadStat) Commit(fileread int64) { func (s *FileToUploadStat) Commit(fileread int64) {
@ -71,7 +100,7 @@ func (s *FileToUploadStat) Commit(fileread int64) {
defer s.mu.Unlock() defer s.mu.Unlock()
s.UploadedBytes += fileread s.UploadedBytes += fileread
_ = s.Progress.Set64(s.UploadedBytes) s.refreshIfNeeded()
} }
func (s *FileToUploadStat) Done() { func (s *FileToUploadStat) Done() {
@ -79,11 +108,7 @@ func (s *FileToUploadStat) Done() {
defer s.mu.Unlock() defer s.mu.Unlock()
s.Uploaded++ s.Uploaded++
s.Refresh() s.refreshIfNeeded()
}
func (s *FileToUploadStat) Close() {
_ = s.Progress.Close()
} }
func (s *FileToUploadStat) Fail() { func (s *FileToUploadStat) Fail() {
@ -91,7 +116,7 @@ func (s *FileToUploadStat) Fail() {
defer s.mu.Unlock() defer s.mu.Unlock()
s.Failed++ s.Failed++
s.Refresh() s.refreshIfNeeded()
} }
func (s *FileToUploadStat) Skip() { func (s *FileToUploadStat) Skip() {
@ -99,14 +124,23 @@ func (s *FileToUploadStat) Skip() {
defer s.mu.Unlock() defer s.mu.Unlock()
s.Skipped++ s.Skipped++
s.Refresh() s.refreshIfNeeded()
} }
func (s *FileToUploadStat) Error(origin string, filename string, err error) { func (s *FileToUploadStat) Error(origin string, filename string, err error) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
_ = s.Progress.Clear() s.progress.Clear()
fmt.Printf("[%s] %s: %s\n", origin, filename, err) ct.Foreground(ct.Red, false)
_ = s.Progress.RenderBlank() _, _ = fmt.Fprintf(s.progress, "[%s] %s: %s\n", origin, filename, err)
_ = s.progress.Print()
ct.ResetColor()
s.progress.Reset()
s.refreshIfNeeded()
}
func (s *FileToUploadStat) Close() {
s.refresh()
} }