github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/u2u/genesisstore/filelog/filelog.go (about) 1 package filelog 2 3 import ( 4 "fmt" 5 "io" 6 "time" 7 8 "github.com/unicornultrafoundation/go-u2u/log" 9 10 "github.com/unicornultrafoundation/go-u2u/utils" 11 ) 12 13 type Filelog struct { 14 io.Reader 15 name string 16 size uint64 17 period time.Duration 18 consumed uint64 19 prevLog time.Time 20 start time.Time 21 } 22 23 func (f *Filelog) Read(p []byte) (n int, err error) { 24 n, err = f.Reader.Read(p) 25 f.consumed += uint64(n) 26 if f.prevLog.IsZero() { 27 log.Info(fmt.Sprintf("- Reading %s", f.name)) 28 f.prevLog = time.Now() 29 f.start = time.Now() 30 } else if f.consumed > 0 && f.consumed < f.size && time.Since(f.prevLog) >= f.period { 31 elapsed := time.Since(f.start) 32 eta := float64(f.size-f.consumed) / float64(f.consumed) * float64(elapsed) 33 progress := float64(f.consumed) / float64(f.size) 34 eta *= 1.0 + (1.0-progress)/2.0 // show slightly higher ETA as performance degrades over larger volumes of data 35 progressStr := fmt.Sprintf("%.2f%%", 100*progress) 36 log.Info(fmt.Sprintf("- Reading %s", f.name), "progress", progressStr, "elapsed", utils.PrettyDuration(elapsed), "eta", utils.PrettyDuration(eta)) 37 f.prevLog = time.Now() 38 } 39 return 40 } 41 42 func Wrap(r io.Reader, name string, size uint64, period time.Duration) *Filelog { 43 return &Filelog{ 44 Reader: r, 45 name: name, 46 size: size, 47 period: period, 48 } 49 }