Skip to content

Commit 65d1861

Browse files
committed
stats: now the tubes are printed sorted by name, default is guaranteed to be the first #4
1 parent 18464dc commit 65d1861

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

cli/stats.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"errors"
55
"fmt"
6+
"sort"
67
"strconv"
78
"strings"
89

@@ -14,6 +15,7 @@ const (
1415
HighSeverity = iota
1516
NormalSeverity
1617
LowSeverity
18+
DefaultTube = "default"
1719
)
1820

1921
var TubeStatsRetrievalError = errors.New("Unable to retrieve tube stats")
@@ -53,16 +55,22 @@ func (c *StatsCommand) PrintStats() error {
5355
"Name", "Buried", "Delayed", "Ready", "Reserved", "Urgent", "Waiting", "Total",
5456
})
5557

56-
for t, s := range stats {
57-
table.AddRow(c.buildLineFromTubeStats(t, s))
58+
table.AddRow(c.buildLineFromTubeStats(DefaultTube, stats[DefaultTube]))
59+
60+
for _, t := range sortedKeys(stats) {
61+
if t == DefaultTube {
62+
continue
63+
}
64+
65+
table.AddRow(c.buildLineFromTubeStats(t, stats[t]))
5866
}
5967

6068
fmt.Println(table.Render())
6169
return nil
6270
}
6371

6472
func (c *StatsCommand) buildLineFromTubeStats(name string, s *TubeStats) []string {
65-
l := make([]string, 0)
73+
var l []string
6674

6775
l = append(l, name)
6876
l = append(l, addStyle(s.JobsBuried, 8, HighSeverity))
@@ -131,3 +139,16 @@ func mustConvertToInt(s string) int {
131139

132140
return i
133141
}
142+
143+
func sortedKeys(m map[string]*TubeStats) []string {
144+
keys := make([]string, len(m))
145+
146+
i := 0
147+
for key := range m {
148+
keys[i] = key
149+
i++
150+
}
151+
152+
sort.Strings(keys)
153+
return keys
154+
}

0 commit comments

Comments
 (0)