Skip to content

Commit

Permalink
Update the stream formatter to display custom unit numbers.
Browse files Browse the repository at this point in the history
Signed-off-by: Ying Li <[email protected]>
  • Loading branch information
cyli committed May 11, 2017
1 parent 1847bb8 commit a771c16
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion daemon/cluster/convert/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.Clu
spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert)
}
if s.CAConfig.SigningCAKey != "" {
// do prpagate the signing CA key here because we want to provide it TO the swarm APIs
// do propagate the signing CA key here because we want to provide it TO the swarm APIs
spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey)
}
spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate
Expand Down
28 changes: 23 additions & 5 deletions pkg/jsonmessage/jsonmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type JSONProgress struct {
Total int64 `json:"total,omitempty"`
Start int64 `json:"start,omitempty"`
// If true, don't show xB/yB
HideCounts bool `json:"hidecounts,omitempty"`
HideCounts bool `json:"hidecounts,omitempty"`
Units string `json:"units,omitempty"`
}

func (p *JSONProgress) String() string {
Expand All @@ -55,11 +56,16 @@ func (p *JSONProgress) String() string {
if p.Current <= 0 && p.Total <= 0 {
return ""
}
current := units.HumanSize(float64(p.Current))
if p.Total <= 0 {
return fmt.Sprintf("%8v", current)
switch p.Units {
case "":
current := units.HumanSize(float64(p.Current))
return fmt.Sprintf("%8v", current)
default:
return fmt.Sprintf("%d %s", p.Current, p.Units)
}
}
total := units.HumanSize(float64(p.Total))

percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
if percentage > 50 {
percentage = 50
Expand All @@ -73,13 +79,25 @@ func (p *JSONProgress) String() string {
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
}

if !p.HideCounts {
switch {
case p.HideCounts:
case p.Units == "": // no units, use bytes
current := units.HumanSize(float64(p.Current))
total := units.HumanSize(float64(p.Total))

numbersBox = fmt.Sprintf("%8v/%v", current, total)

if p.Current > p.Total {
// remove total display if the reported current is wonky.
numbersBox = fmt.Sprintf("%8v", current)
}
default:
numbersBox = fmt.Sprintf("%d/%d %s", p.Current, p.Total, p.Units)

if p.Current > p.Total {
// remove total display if the reported current is wonky.
numbersBox = fmt.Sprintf("%d %s", p.Current, p.Units)
}
}

if p.Current > 0 && p.Start > 0 && percentage < 50 {
Expand Down
44 changes: 36 additions & 8 deletions pkg/jsonmessage/jsonmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,50 @@ func TestProgress(t *testing.T) {
if jp5.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp5.String())
}

expected = "[=========================> ] 50/100 units"
if termsz != nil && termsz.Width <= 110 {
expected = " 50/100 units"
}
jp6 := JSONProgress{Current: 50, Total: 100, Units: "units"}
if jp6.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp6.String())
}

// this number can't be negative
expected = "[==================================================>] 50 units"
if termsz != nil && termsz.Width <= 110 {
expected = " 50 units"
}
jp7 := JSONProgress{Current: 50, Total: 40, Units: "units"}
if jp7.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp7.String())
}

expected = "[=========================> ] "
if termsz != nil && termsz.Width <= 110 {
expected = ""
}
jp8 := JSONProgress{Current: 50, Total: 100, HideCounts: true}
if jp8.String() != expected {
t.Fatalf("Expected %q, got %q", expected, jp8.String())
}
}

func TestJSONMessageDisplay(t *testing.T) {
now := time.Now()
messages := map[JSONMessage][]string{
// Empty
JSONMessage{}: {"\n", "\n"},
{}: {"\n", "\n"},
// Status
JSONMessage{
{
Status: "status",
}: {
"status\n",
"status\n",
},
// General
JSONMessage{
{
Time: now.Unix(),
ID: "ID",
From: "From",
Expand All @@ -90,7 +118,7 @@ func TestJSONMessageDisplay(t *testing.T) {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(now.Unix(), 0).Format(jsonlog.RFC3339NanoFixed)),
},
// General, with nano precision time
JSONMessage{
{
TimeNano: now.UnixNano(),
ID: "ID",
From: "From",
Expand All @@ -100,7 +128,7 @@ func TestJSONMessageDisplay(t *testing.T) {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
},
// General, with both times Nano is preferred
JSONMessage{
{
Time: now.Unix(),
TimeNano: now.UnixNano(),
ID: "ID",
Expand All @@ -111,23 +139,23 @@ func TestJSONMessageDisplay(t *testing.T) {
fmt.Sprintf("%v ID: (from From) status\n", time.Unix(0, now.UnixNano()).Format(jsonlog.RFC3339NanoFixed)),
},
// Stream over status
JSONMessage{
{
Status: "status",
Stream: "stream",
}: {
"stream",
"stream",
},
// With progress message
JSONMessage{
{
Status: "status",
ProgressMessage: "progressMessage",
}: {
"status progressMessage",
"status progressMessage",
},
// With progress, stream empty
JSONMessage{
{
Status: "status",
Stream: "",
Progress: &JSONProgress{Current: 1},
Expand Down
2 changes: 2 additions & 0 deletions pkg/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Progress struct {

// If true, don't show xB/yB
HideCounts bool
// If not empty, use units instead of bytes for counts
Units string

// Aux contains extra information not presented to the user, such as
// digests for push signing.
Expand Down
2 changes: 1 addition & 1 deletion pkg/streamformatter/streamformatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error {
if prog.Message != "" {
formatted = out.sf.formatStatus(prog.ID, prog.Message)
} else {
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts}
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts, Units: prog.Units}
formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
}
_, err := out.out.Write(formatted)
Expand Down

0 comments on commit a771c16

Please sign in to comment.