Skip to content

Commit bd925c0

Browse files
authored
mirror: add the duration time and beautify the output (#5125)
add the duration time and beautify the output
1 parent 030c812 commit bd925c0

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

cmd/accounting-reader.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
package cmd
1919

2020
import (
21-
"fmt"
21+
"strings"
2222
"sync"
2323
"sync/atomic"
2424
"time"
2525

26+
"github.com/fatih/color"
27+
"github.com/minio/pkg/v3/console"
28+
2629
"github.com/cheggaaa/pb"
2730
json "github.com/minio/colorjson"
2831
"github.com/minio/mc/pkg/probe"
@@ -56,14 +59,14 @@ func newAccounter(total int64) *accounter {
5659
}
5760

5861
// write calculate the final speed.
59-
func (a *accounter) write(current int64) float64 {
62+
func (a *accounter) write(current int64) (float64, time.Duration) {
6063
fromStart := time.Since(a.startTime)
6164
currentFromStart := current - a.startValue
6265
if currentFromStart > 0 {
6366
speed := float64(currentFromStart) / (float64(fromStart) / float64(time.Second))
64-
return speed
67+
return speed, fromStart
6568
}
66-
return 0.0
69+
return 0.0, 0
6770
}
6871

6972
// writer update new accounting data for a specified refreshRate.
@@ -81,10 +84,11 @@ func (a *accounter) writer() {
8184

8285
// accountStat cantainer for current stats captured.
8386
type accountStat struct {
84-
Status string `json:"status"`
85-
Total int64 `json:"total"`
86-
Transferred int64 `json:"transferred"`
87-
Speed float64 `json:"speed"`
87+
Status string `json:"status"`
88+
Total int64 `json:"total"`
89+
Transferred int64 `json:"transferred"`
90+
Duration time.Duration `json:"duration"`
91+
Speed float64 `json:"speed"`
8892
}
8993

9094
func (c accountStat) JSON() string {
@@ -96,15 +100,42 @@ func (c accountStat) JSON() string {
96100
}
97101

98102
func (c accountStat) String() string {
103+
dspOrder := []col{colGreen} // Header
104+
dspOrder = append(dspOrder, colGrey)
105+
var printColors []*color.Color
106+
for _, c := range dspOrder {
107+
printColors = append(printColors, getPrintCol(c))
108+
}
109+
110+
tbl := console.NewTable(printColors, []bool{false, false, false, false}, 0)
111+
112+
var builder strings.Builder
113+
cellText := make([][]string, 0, 2)
114+
cellText = append(cellText, []string{
115+
"Total",
116+
"Transferred",
117+
"Duration",
118+
"Speed",
119+
})
120+
99121
speedBox := pb.Format(int64(c.Speed)).To(pb.U_BYTES).String()
100122
if speedBox == "" {
101123
speedBox = "0 MB"
102124
} else {
103125
speedBox = speedBox + "/s"
104126
}
105-
message := fmt.Sprintf("Total: %s, Transferred: %s, Speed: %s", pb.Format(c.Total).To(pb.U_BYTES),
106-
pb.Format(c.Transferred).To(pb.U_BYTES), speedBox)
107-
return message
127+
128+
cellText = append(cellText, []string{
129+
pb.Format(c.Total).To(pb.U_BYTES).String(),
130+
pb.Format(c.Transferred).To(pb.U_BYTES).String(),
131+
pb.Format(int64(c.Duration)).To(pb.U_DURATION).String(),
132+
speedBox,
133+
})
134+
135+
e := tbl.PopulateTable(&builder, cellText)
136+
fatalIf(probe.NewError(e), "unable to populate the table")
137+
138+
return builder.String()
108139
}
109140

110141
// Stat provides current stats captured.
@@ -114,7 +145,7 @@ func (a *accounter) Stat() accountStat {
114145
close(a.isFinished)
115146
acntStat.Total = a.total
116147
acntStat.Transferred = atomic.LoadInt64(&a.current)
117-
acntStat.Speed = a.write(atomic.LoadInt64(&a.current))
148+
acntStat.Speed, acntStat.Duration = a.write(atomic.LoadInt64(&a.current))
118149
})
119150
return acntStat
120151
}

0 commit comments

Comments
 (0)