@cryptotaxi247 / kubo / commits / f40dc731e

refactor: improved humanNumber and humanSI

This hides "(n)" when value is same as SI, and makes SI "3k" (instead of "3 k")

Marcin Rataj committed May 27, 2021 at 22:03 UTC f40dc731e9d7e3d452d03c632d99ef9577d1fb2d
1 file changed +15 -7
core/commands/stat_provide.go
+15 -7
@@ -55,12 +55,10 @@ This interface is not stable and may change from release to release.
55 wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
56 defer wtr.Flush()
57
58 - tp := float64(s.TotalProvides)
59 - fmt.Fprintf(wtr, "TotalProvides:\t%s\t(%s)\n", humanSI(tp, 0), humanFull(tp, 0))
60 - fmt.Fprintf(wtr, "AvgProvideDuration:\t%v\n", humanDuration(s.AvgProvideDuration))
61 - fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", humanDuration(s.LastReprovideDuration))
62 - lrbs := float64(s.LastReprovideBatchSize)
63 - fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0))
58 + fmt.Fprintf(wtr, "TotalProvides:\t%s\n", humanNumber(s.TotalProvides))
59 + fmt.Fprintf(wtr, "AvgProvideDuration:\t%s\n", humanDuration(s.AvgProvideDuration))
60 + fmt.Fprintf(wtr, "LastReprovideDuration:\t%s\n", humanDuration(s.LastReprovideDuration))
61 + fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\n", humanNumber(s.LastReprovideBatchSize))
62 return nil
63 }),
64 },
@@ -71,9 +69,19 @@ func humanDuration(val time.Duration) string {
69 return val.Truncate(time.Microsecond).String()
70 }
71
72 +func humanNumber(n int) string {
73 + nf := float64(n)
74 + str := humanSI(nf, 0)
75 + fullStr := humanFull(nf, 0)
76 + if str != fullStr {
77 + return fmt.Sprintf("%s\t(%s)", str, fullStr)
78 + }
79 + return str
80 +}
81 +
82 func humanSI(val float64, decimals int) string {
83 v, unit := humanize.ComputeSI(val)
76 - return humanize.SIWithDigits(v, decimals, unit)
84 + return fmt.Sprintf("%s%s", humanFull(v, decimals), unit)
85 }
86
87 func humanFull(val float64, decimals int) string {