Commit 9eccce1f authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

fix(2/diag) match ipfs output

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 3352aeee
package commands package commands
import ( import (
"fmt" "bytes"
"io" "io"
"text/template"
"time" "time"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
diagn "github.com/jbenet/go-ipfs/diagnostics" util "github.com/jbenet/go-ipfs/util"
) )
type DiagnosticConnection struct { type DiagnosticConnection struct {
ID string ID string
Latency int64 // TODO use milliseconds or microseconds for human readability
NanosecondsLatency uint64
} }
type DiagnosticPeer struct { type DiagnosticPeer struct {
ID string ID string
LifeSpan float64 UptimeSeconds uint64
BandwidthIn uint64 BandwidthBytesIn uint64
BandwidthOut uint64 BandwidthBytesOut uint64
Connections []DiagnosticConnection Connections []DiagnosticConnection
} }
type DiagnosticOutput struct { type DiagnosticOutput struct {
...@@ -58,33 +60,62 @@ connected peers and latencies between them. ...@@ -58,33 +60,62 @@ connected peers and latencies between them.
connections := make([]DiagnosticConnection, len(peer.Connections)) connections := make([]DiagnosticConnection, len(peer.Connections))
for j, conn := range peer.Connections { for j, conn := range peer.Connections {
connections[j] = DiagnosticConnection{ connections[j] = DiagnosticConnection{
ID: conn.ID, ID: conn.ID,
Latency: conn.Latency.Nanoseconds(), NanosecondsLatency: uint64(conn.Latency.Nanoseconds()),
} }
} }
output[i] = DiagnosticPeer{ output[i] = DiagnosticPeer{
ID: peer.ID, ID: peer.ID,
LifeSpan: peer.LifeSpan.Minutes(), UptimeSeconds: uint64(peer.LifeSpan.Seconds()),
BandwidthIn: peer.BwIn, BandwidthBytesIn: peer.BwIn,
BandwidthOut: peer.BwOut, BandwidthBytesOut: peer.BwOut,
Connections: connections, Connections: connections,
} }
} }
return &DiagnosticOutput{output}, nil return &DiagnosticOutput{output}, nil
}, },
Type: &DiagnosticOutput{}, Type: &DiagnosticOutput{},
Marshallers: map[cmds.EncodingType]cmds.Marshaller{
cmds.Text: func(r cmds.Response) ([]byte, error) {
output, ok := r.Output().(*DiagnosticOutput)
if !ok {
return nil, util.ErrCast()
}
var buf bytes.Buffer
err := printDiagnostics(&buf, output)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
},
},
} }
func PrintDiagnostics(info []*diagn.DiagInfo, out io.Writer) { func printDiagnostics(out io.Writer, info *DiagnosticOutput) error {
for _, i := range info {
fmt.Fprintf(out, "Peer: %s\n", i.ID) diagTmpl := `
fmt.Fprintf(out, "\tUp for: %s\n", i.LifeSpan.String()) {{ range $peer := .Peers }}
fmt.Fprintf(out, "\tConnected To:\n") ID {{ $peer.ID }}
for _, c := range i.Connections { up {{ $peer.UptimeSeconds }} seconds
fmt.Fprintf(out, "\t%s\n\t\tLatency = %s\n", c.ID, c.Latency.String()) connected to {{ len .Connections }}...
} {{ range $connection := .Connections }}
fmt.Fprintln(out) ID {{ $connection.ID }}
latency: {{ $connection.NanosecondsLatency }} ns
{{ end }}
{{end}}
`
templ, err := template.New("DiagnosticOutput").Parse(diagTmpl)
if err != nil {
return err
}
err = templ.Execute(out, info)
if err != nil {
return err
} }
return nil
} }
...@@ -67,6 +67,7 @@ type DiagInfo struct { ...@@ -67,6 +67,7 @@ type DiagInfo struct {
Keys []string Keys []string
// How long this node has been running for // How long this node has been running for
// TODO rename Uptime
LifeSpan time.Duration LifeSpan time.Duration
// Incoming Bandwidth Usage // Incoming Bandwidth Usage
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment