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

fix(commands): support --enc=json in stat #5620

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 25 additions & 25 deletions core/commands/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"sync"
"time"

cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Expand Down Expand Up @@ -40,6 +41,9 @@ const (
statIntervalOptionName = "interval"
)

// for `--poll` flag in `ipfs stat bw`
var once sync.Once

var statBwCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Print ipfs bandwidth information.",
Expand Down Expand Up @@ -147,35 +151,31 @@ Example:
}
},
Type: metrics.Stats{},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
polling, _ := res.Request().Options[statPollOptionName].(bool)
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
polling, _ := req.Options[statPollOptionName].(bool)

if polling {
fmt.Fprintln(os.Stdout, "Total Up Total Down Rate Up Rate Down")
// print only once in `--poll` flag
once.Do(func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for multiple invocations of the command, not sure what's the best way to do that. cc @keks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proper way to do this is using PostRun. Encoders are for processing individual elements, while PostRun is passed the stream that is emitted by Run and can do transport (e.g. cli or http) specific presentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: I think we need a second PostRunFunc in the PostRun map with index cmds.JSON.

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
}

bs := v.(*metrics.Stats)

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

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)))

bs := v.(*metrics.Stats)

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

fmt.Fprintf(os.Stdout, "%8s ", humanize.Bytes(uint64(bs.TotalOut)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be also probably be printing into the writer instead of directly to Stdout.

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 nil
}),
},
}

Expand Down