@cryptotaxi247 / kubo / commits / 63e18dac8

humanize for ipfs bitswap stat

License: MIT Signed-off-by: Eric Wu <myself659@163.com>

myself659 committed Apr 26, 2019 at 01:11 UTC 63e18dac8dd34aec17c42402dd82411188afd1f4
2 files changed +38 -5
core/commands/bitswap.go
+15 -3
@@ -90,6 +90,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,
90
91 const (
92 bitswapVerboseOptionName = "verbose"
93 + bitswapHumanOptionName = "human"
94 )
95
96 var bitswapStatCmd = &cmds.Command{
@@ -99,6 +100,7 @@ var bitswapStatCmd = &cmds.Command{
100 },
101 Options: []cmdkit.Option{
102 cmdkit.BoolOption(bitswapVerboseOptionName, "v", "Print extra information"),
103 + cmdkit.BoolOption(bitswapHumanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
104 },
105 Type: bitswap.Stat{},
106 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
@@ -130,15 +132,25 @@ var bitswapStatCmd = &cmds.Command{
132 return err
133 }
134 verbose, _ := req.Options[bitswapVerboseOptionName].(bool)
135 + human, _ := req.Options[bitswapHumanOptionName].(bool)
136
137 fmt.Fprintln(w, "bitswap status")
138 fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize)
139 fmt.Fprintf(w, "\tblocks received: %d\n", s.BlocksReceived)
140 fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
138 - fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
139 - fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
141 + if human {
142 + fmt.Fprintf(w, "\tdata received: %s\n", humanize.Bytes(s.DataReceived))
143 + fmt.Fprintf(w, "\tdata sent: %s\n", humanize.Bytes(s.DataSent))
144 + } else {
145 + fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
146 + fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
147 + }
148 fmt.Fprintf(w, "\tdup blocks received: %d\n", s.DupBlksReceived)
141 - fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
149 + if human {
150 + fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
151 + } else {
152 + fmt.Fprintf(w, "\tdup data received: %d\n", s.DupDataReceived)
153 + }
154 fmt.Fprintf(w, "\twantlist [%d keys]\n", len(s.Wantlist))
155 for _, k := range s.Wantlist {
156 fmt.Fprintf(w, "\t\t%s\n", enc.Encode(k))
test/sharness/t0220-bitswap.sh
+23 -2
@@ -24,7 +24,7 @@ bitswap status
24 data received: 0
25 data sent: 0
26 dup blocks received: 0
27 - dup data received: 0 B
27 + dup data received: 0
28 wantlist [0 keys]
29 partners [0]
30 EOF
@@ -62,7 +62,7 @@ bitswap status
62 data received: 0
63 data sent: 0
64 dup blocks received: 0
65 - dup data received: 0 B
65 + dup data received: 0
66 wantlist [0 keys]
67 partners [0]
68 EOF
@@ -77,6 +77,27 @@ test_expect_success "'ipfs bitswap wantlist -p' output looks good" '
77 test_cmp wantlist_out wantlist_p_out
78 '
79
80 +test_expect_success "'ipfs bitswap stat --human' succeeds" '
81 + ipfs bitswap stat --human >stat_out_human
82 +'
83 +
84 +
85 +test_expect_success "'ipfs bitswap stat --human' output looks good" '
86 + cat <<EOF | unexpand -t2 >expected &&
87 +bitswap status
88 + provides buffer: 0 / 256
89 + blocks received: 0
90 + blocks sent: 0
91 + data received: 0 B
92 + data sent: 0 B
93 + dup blocks received: 0
94 + dup data received: 0 B
95 + wantlist [0 keys]
96 + partners [0]
97 +EOF
98 + test_cmp expected stat_out_human
99 +'
100 +
101 test_kill_ipfs_daemon
102
103 test_done