From 20d10beccfbec6fa3d4c26252b9befe661f3f646 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Tue, 25 May 2021 17:47:58 -0400 Subject: [PATCH 1/4] feat: add a text output encoding for the stats provide command --- core/commands/stat_provide.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/core/commands/stat_provide.go b/core/commands/stat_provide.go index ac02c344cd6..073bae159a3 100644 --- a/core/commands/stat_provide.go +++ b/core/commands/stat_provide.go @@ -2,6 +2,8 @@ package commands import ( "fmt" + "io" + "text/tabwriter" cmds "github.com/ipfs/go-ipfs-cmds" "github.com/ipfs/go-ipfs/core/commands/cmdenv" @@ -46,6 +48,17 @@ This interface is not stable and may change from release to release. return nil }, - Encoders: cmds.EncoderMap{}, - Type: batched.BatchedProviderStats{}, + 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) + 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) + return nil + }), + }, + Type: batched.BatchedProviderStats{}, } From ec90a629b62149bb8ebd69477a3418721a0d51ba Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 27 May 2021 16:22:55 +0200 Subject: [PATCH 2/4] 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 073bae159a3..7d9c44cd0df 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) +} From a4c4b7deae9a6574b60508241b62cfd1f3a42f56 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 27 May 2021 16:49:13 +0200 Subject: [PATCH 3/4] feat: humanized durations in stat provide --- core/commands/stat_provide.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/commands/stat_provide.go b/core/commands/stat_provide.go index 7d9c44cd0df..86fe96efb99 100644 --- a/core/commands/stat_provide.go +++ b/core/commands/stat_provide.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "text/tabwriter" + "time" humanize "github.com/dustin/go-humanize" cmds "github.com/ipfs/go-ipfs-cmds" @@ -56,8 +57,8 @@ This interface is not stable and may change from release to release. 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) + fmt.Fprintf(wtr, "AvgProvideDuration:\t%v\n", humanDuration(s.AvgProvideDuration)) + fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", humanDuration(s.LastReprovideDuration)) lrbs := float64(s.LastReprovideBatchSize) fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0)) return nil @@ -66,6 +67,10 @@ This interface is not stable and may change from release to release. Type: batched.BatchedProviderStats{}, } +func humanDuration(val time.Duration) string { + return val.Truncate(time.Microsecond).String() +} + func humanSI(val float64, decimals int) string { v, unit := humanize.ComputeSI(val) return humanize.SIWithDigits(v, decimals, unit) From 12ebbe5553dec4414fec5725444f1e717af2ed4e Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 27 May 2021 22:03:11 +0200 Subject: [PATCH 4/4] refactor: improved humanNumber and humanSI This hides "(n)" when value is same as SI, and makes SI "3k" (instead of "3 k") --- core/commands/stat_provide.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/commands/stat_provide.go b/core/commands/stat_provide.go index 86fe96efb99..4224efcafc0 100644 --- a/core/commands/stat_provide.go +++ b/core/commands/stat_provide.go @@ -55,12 +55,10 @@ This interface is not stable and may change from release to release. wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) defer wtr.Flush() - 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", humanDuration(s.AvgProvideDuration)) - fmt.Fprintf(wtr, "LastReprovideDuration:\t%v\n", humanDuration(s.LastReprovideDuration)) - lrbs := float64(s.LastReprovideBatchSize) - fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\t(%s)\n", humanSI(lrbs, 0), humanFull(lrbs, 0)) + fmt.Fprintf(wtr, "TotalProvides:\t%s\n", humanNumber(s.TotalProvides)) + fmt.Fprintf(wtr, "AvgProvideDuration:\t%s\n", humanDuration(s.AvgProvideDuration)) + fmt.Fprintf(wtr, "LastReprovideDuration:\t%s\n", humanDuration(s.LastReprovideDuration)) + fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\n", humanNumber(s.LastReprovideBatchSize)) return nil }), }, @@ -71,9 +69,19 @@ func humanDuration(val time.Duration) string { return val.Truncate(time.Microsecond).String() } +func humanNumber(n int) string { + nf := float64(n) + str := humanSI(nf, 0) + fullStr := humanFull(nf, 0) + if str != fullStr { + return fmt.Sprintf("%s\t(%s)", str, fullStr) + } + return str +} + func humanSI(val float64, decimals int) string { v, unit := humanize.ComputeSI(val) - return humanize.SIWithDigits(v, decimals, unit) + return fmt.Sprintf("%s%s", humanFull(v, decimals), unit) } func humanFull(val float64, decimals int) string {