-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"io" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" | ||
|
@@ -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.", | ||
|
@@ -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() { | ||
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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
}), | ||
}, | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, whilePostRun
is passed the stream that is emitted byRun
and can do transport (e.g. cli or http) specific presentation.There was a problem hiding this comment.
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 thePostRun
map with indexcmds.JSON
.