feat: humanized numbers in stat provide
https://github.com/ipfs/go-ipfs/pull/8154#discussion_r639231040 (cherry picked from commit c082c73135d4bf9ba44fcc901d84434ed78a652c)
Marcin Rataj committed
May 27, 2021 at 16:22 UTC
bd46b0bbe6759c4b9d1436e026175a0bcd5cef04
1 file changed
+17
-5
core/commands/stat_provide.go
+17
-5
@@ -5,6 +5,7 @@ import (
5
"io"
6
"text/tabwriter"
7
8
+ humanize "github.com/dustin/go-humanize"
9
cmds "github.com/ipfs/go-ipfs-cmds"
10
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
11
@@ -50,15 +51,26 @@ This interface is not stable and may change from release to release.
51
},
52
Encoders: cmds.EncoderMap{
53
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *batched.BatchedProviderStats) error {
53
- wtr := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
54
+ wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
55
defer wtr.Flush()
56
56
- fmt.Fprintf(wtr, "TotalProvides: %d\n", s.TotalProvides)
57
- fmt.Fprintf(wtr, "AvgProvideDuration: %v\n", s.AvgProvideDuration)
58
- fmt.Fprintf(wtr, "LastReprovideDuration: %v\n", s.LastReprovideDuration)
59
- fmt.Fprintf(wtr, "LastReprovideBatchSize: %d\n", s.LastReprovideBatchSize)
57
+ tp := float64(s.TotalProvides)
58
+ fmt.Fprintf(wtr, "TotalProvides:\t%s\t(%s)\n", humanSI(tp, 0), humanFull(tp, 0))
59
+ fmt.Fprintf(wtr, "AvgProvideDuration:\t%v\n", s.AvgProvideDuration)
60
+ fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", s.LastReprovideDuration)
61
+ lrbs := float64(s.LastReprovideBatchSize)
62
+ fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0))
63
return nil
64
}),
65
},
66
Type: batched.BatchedProviderStats{},
67
}
68
+
69
+func humanSI(val float64, decimals int) string {
70
+ v, unit := humanize.ComputeSI(val)
71
+ return humanize.SIWithDigits(v, decimals, unit)
72
+}
73
+
74
+func humanFull(val float64, decimals int) string {
75
+ return humanize.CommafWithDigits(val, decimals)
76
+}