From 21d5ba18208d7590f3bd71db2f2cafee2657dcbc Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 27 May 2021 16:22:55 +0200 Subject: [PATCH] feat: humanized numbers in stat provide https://github.com/ipfs/go-ipfs/pull/8154#discussion_r639231040 --- core/commands/stat_provide.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/core/commands/stat_provide.go b/core/commands/stat_provide.go index 073bae159a34..7d9c44cd0df2 100644 --- a/core/commands/stat_provide.go +++ b/core/commands/stat_provide.go @@ -5,6 +5,7 @@ import ( "io" "text/tabwriter" + humanize "github.com/dustin/go-humanize" cmds "github.com/ipfs/go-ipfs-cmds" "github.com/ipfs/go-ipfs/core/commands/cmdenv" @@ -50,15 +51,26 @@ This interface is not stable and may change from release to release. }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *batched.BatchedProviderStats) error { - wtr := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0) + wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) defer wtr.Flush() - fmt.Fprintf(wtr, "TotalProvides: %d\n", s.TotalProvides) - fmt.Fprintf(wtr, "AvgProvideDuration: %v\n", s.AvgProvideDuration) - fmt.Fprintf(wtr, "LastReprovideDuration: %v\n", s.LastReprovideDuration) - fmt.Fprintf(wtr, "LastReprovideBatchSize: %d\n", s.LastReprovideBatchSize) + tp := float64(s.TotalProvides) + fmt.Fprintf(wtr, "TotalProvides:\t%s\t(%s)\n", humanSI(tp, 0), humanFull(tp, 0)) + fmt.Fprintf(wtr, "AvgProvideDuration:\t%v\n", s.AvgProvideDuration) + fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", s.LastReprovideDuration) + lrbs := float64(s.LastReprovideBatchSize) + fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0)) return nil }), }, Type: batched.BatchedProviderStats{}, } + +func humanSI(val float64, decimals int) string { + v, unit := humanize.ComputeSI(val) + return humanize.SIWithDigits(v, decimals, unit) +} + +func humanFull(val float64, decimals int) string { + return humanize.CommafWithDigits(val, decimals) +}