Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DisplayCLI for stat bw CLI command #8358

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 51 additions & 33 deletions core/commands/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"fmt"
"io"
"os"
"time"

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Expand Down Expand Up @@ -154,42 +153,61 @@ Example:
}
},
Type: metrics.Stats{},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
polling, _ := res.Request().Options[statPollOptionName].(bool)
DisplayCLI: func(res cmds.Response, stdout, stderr io.Writer) error {
polling, _ := res.Request().Options[statPollOptionName].(bool)

if polling {
fmt.Fprintln(os.Stdout, "Total Up Total Down Rate Up Rate Down")
}
for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
output := printStats

bs := v.(*metrics.Stats)

if !polling {
printStats(os.Stdout, bs)
return nil
}
if polling {
output = pollStats
}

fmt.Fprintf(os.Stdout, "%8s ", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(os.Stdout, "%8s ", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(os.Stdout, "%8s/s ", humanize.Bytes(uint64(bs.RateOut)))
fmt.Fprintf(os.Stdout, "%8s/s \r", humanize.Bytes(uint64(bs.RateIn)))
}
},
return output(res, stdout, stderr)
},
}

func printStats(out io.Writer, bs *metrics.Stats) {
fmt.Fprintln(out, "Bandwidth")
fmt.Fprintf(out, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(out, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(out, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn)))
fmt.Fprintf(out, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut)))
// Non-polled output

func printStats(res cmds.Response, stdout io.Writer, stderr io.Writer) error {

v, err := res.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}

bs := v.(*metrics.Stats)

fmt.Fprintln(stdout, "Bandwidth")
fmt.Fprintf(stdout, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(stdout, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(stdout, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn)))
fmt.Fprintf(stdout, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut)))

return nil
}

// Polled output

func pollStats(res cmds.Response, stdout io.Writer, stderr io.Writer) error {
fmt.Fprintln(stdout, "Total Up Total Down Rate Up Rate Down")

for {
v, err := res.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}

bs := v.(*metrics.Stats)

fmt.Fprintf(stdout, "%8s ", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(stdout, "%8s ", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(stdout, "%8s/s ", humanize.Bytes(uint64(bs.RateOut)))
fmt.Fprintf(stdout, "%8s/s \r", humanize.Bytes(uint64(bs.RateIn)))
}
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/ipfs/go-graphsync v0.8.0
github.com/ipfs/go-ipfs-blockstore v0.1.6
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-cmds v0.6.0
github.com/ipfs/go-ipfs-config v0.14.0
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
Expand Down Expand Up @@ -59,6 +58,7 @@ require (
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jbenet/go-temp-err-catcher v0.1.0
github.com/jbenet/goprocess v0.1.4
github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12 // indirect
github.com/libp2p/go-doh-resolver v0.3.1
github.com/libp2p/go-libp2p v0.14.4
github.com/libp2p/go-libp2p-circuit v0.4.0
Expand Down Expand Up @@ -108,3 +108,5 @@ require (
)

go 1.15

// replace github.com/ipfs/go-ipfs-cmds => ../go-ipfs-cmds
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsj
github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12 h1:Fpq4JqaVM4/7R+iHwT5vWdsxHGjZabzqBuWkAyqTcc8=
github.com/jbouwman/go-ipfs-cmds v0.6.1-0.20210819183735-7ee6142a5f12/go.mod h1:g6wLI6iRb7guDxPSbVzhAHdhsIhn7ggvyKc0by6SR+U=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down