fix(2/diag) match ipfs output
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 12, 2014 at 19:20 UTC
9eccce1f93906cc2a75ffcd9f7cdb112874a26ab
2 files changed
+57
-25
core/commands2/diag.go
+56
-25
@@ -1,25 +1,27 @@
1
package commands
2
3
import (
4
- "fmt"
4
+ "bytes"
5
"io"
6
+ "text/template"
7
"time"
8
9
cmds "github.com/jbenet/go-ipfs/commands"
9
- diagn "github.com/jbenet/go-ipfs/diagnostics"
10
+ util "github.com/jbenet/go-ipfs/util"
11
)
12
13
type DiagnosticConnection struct {
13
- ID string
14
- Latency int64
14
+ ID string
15
+ // TODO use milliseconds or microseconds for human readability
16
+ NanosecondsLatency uint64
17
}
18
19
type DiagnosticPeer struct {
18
- ID string
19
- LifeSpan float64
20
- BandwidthIn uint64
21
- BandwidthOut uint64
22
- Connections []DiagnosticConnection
20
+ ID string
21
+ UptimeSeconds uint64
22
+ BandwidthBytesIn uint64
23
+ BandwidthBytesOut uint64
24
+ Connections []DiagnosticConnection
25
}
26
27
type DiagnosticOutput struct {
@@ -58,33 +60,62 @@ connected peers and latencies between them.
60
connections := make([]DiagnosticConnection, len(peer.Connections))
61
for j, conn := range peer.Connections {
62
connections[j] = DiagnosticConnection{
61
- ID: conn.ID,
62
- Latency: conn.Latency.Nanoseconds(),
63
+ ID: conn.ID,
64
+ NanosecondsLatency: uint64(conn.Latency.Nanoseconds()),
65
}
66
}
67
68
output[i] = DiagnosticPeer{
67
- ID: peer.ID,
68
- LifeSpan: peer.LifeSpan.Minutes(),
69
- BandwidthIn: peer.BwIn,
70
- BandwidthOut: peer.BwOut,
71
- Connections: connections,
69
+ ID: peer.ID,
70
+ UptimeSeconds: uint64(peer.LifeSpan.Seconds()),
71
+ BandwidthBytesIn: peer.BwIn,
72
+ BandwidthBytesOut: peer.BwOut,
73
+ Connections: connections,
74
}
75
}
76
77
return &DiagnosticOutput{output}, nil
78
},
79
Type: &DiagnosticOutput{},
80
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
81
+ cmds.Text: func(r cmds.Response) ([]byte, error) {
82
+ output, ok := r.Output().(*DiagnosticOutput)
83
+ if !ok {
84
+ return nil, util.ErrCast()
85
+ }
86
+ var buf bytes.Buffer
87
+ err := printDiagnostics(&buf, output)
88
+ if err != nil {
89
+ return nil, err
90
+ }
91
+ return buf.Bytes(), nil
92
+ },
93
+ },
94
}
95
80
-func PrintDiagnostics(info []*diagn.DiagInfo, out io.Writer) {
81
- for _, i := range info {
82
- fmt.Fprintf(out, "Peer: %s\n", i.ID)
83
- fmt.Fprintf(out, "\tUp for: %s\n", i.LifeSpan.String())
84
- fmt.Fprintf(out, "\tConnected To:\n")
85
- for _, c := range i.Connections {
86
- fmt.Fprintf(out, "\t%s\n\t\tLatency = %s\n", c.ID, c.Latency.String())
87
- }
88
- fmt.Fprintln(out)
96
+func printDiagnostics(out io.Writer, info *DiagnosticOutput) error {
97
+
98
+ diagTmpl := `
99
+{{ range $peer := .Peers }}
100
+ID {{ $peer.ID }}
101
+ up {{ $peer.UptimeSeconds }} seconds
102
+ connected to {{ len .Connections }}...
103
+ {{ range $connection := .Connections }}
104
+ ID {{ $connection.ID }}
105
+ latency: {{ $connection.NanosecondsLatency }} ns
106
+ {{ end }}
107
+{{end}}
108
+`
109
+
110
+ templ, err := template.New("DiagnosticOutput").Parse(diagTmpl)
111
+ if err != nil {
112
+ return err
113
+ }
114
+
115
+ err = templ.Execute(out, info)
116
+ if err != nil {
117
+ return err
118
}
119
+
120
+ return nil
121
}
diagnostics/diag.go
+1
@@ -67,6 +67,7 @@ type DiagInfo struct {
67
Keys []string
68
69
// How long this node has been running for
70
+ // TODO rename Uptime
71
LifeSpan time.Duration
72
73
// Incoming Bandwidth Usage