@cryptotaxi247 / kubo / commits / 61e8ad999

core/commands2: Added 'diag' command

Matt Bell committed Nov 4, 2014 at 23:45 UTC 61e8ad999021a7a7d4640bf06541fca4901a9371
2 files changed +80
core/commands2/diag.go new
+79
@@ -0,0 +1,79 @@
1 +package commands
2 +
3 +import (
4 + "errors"
5 + "fmt"
6 + "io"
7 + "time"
8 +
9 + cmds "github.com/jbenet/go-ipfs/commands"
10 + diagn "github.com/jbenet/go-ipfs/diagnostics"
11 +)
12 +
13 +type DiagnosticConnection struct {
14 + ID string
15 + Latency int64
16 +}
17 +
18 +type DiagnosticPeer struct {
19 + ID string
20 + LifeSpan float64
21 + BandwidthIn uint64
22 + BandwidthOut uint64
23 + Connections []DiagnosticConnection
24 +}
25 +
26 +type DiagnosticOutput struct {
27 + Peers []DiagnosticPeer
28 +}
29 +
30 +var diagCmd = &cmds.Command{
31 + Run: func(res cmds.Response, req cmds.Request) {
32 + n := req.Context().Node
33 +
34 + if n.Diagnostics == nil {
35 + res.SetError(errors.New("Cannot run diagnostic in offline mode!"), cmds.ErrNormal)
36 + return
37 + }
38 +
39 + info, err := n.Diagnostics.GetDiagnostic(time.Second * 20)
40 + if err != nil {
41 + res.SetError(err, cmds.ErrNormal)
42 + return
43 + }
44 +
45 + output := make([]DiagnosticPeer, len(info))
46 + for i, peer := range info {
47 + connections := make([]DiagnosticConnection, len(peer.Connections))
48 + for j, conn := range peer.Connections {
49 + connections[j] = DiagnosticConnection{
50 + ID: conn.ID,
51 + Latency: conn.Latency.Nanoseconds(),
52 + }
53 + }
54 +
55 + output[i] = DiagnosticPeer{
56 + ID: peer.ID,
57 + LifeSpan: peer.LifeSpan.Minutes(),
58 + BandwidthIn: peer.BwIn,
59 + BandwidthOut: peer.BwOut,
60 + Connections: connections,
61 + }
62 + }
63 +
64 + res.SetOutput(&DiagnosticOutput{output})
65 + },
66 + Type: &DiagnosticOutput{},
67 +}
68 +
69 +func PrintDiagnostics(info []*diagn.DiagInfo, out io.Writer) {
70 + for _, i := range info {
71 + fmt.Fprintf(out, "Peer: %s\n", i.ID)
72 + fmt.Fprintf(out, "\tUp for: %s\n", i.LifeSpan.String())
73 + fmt.Fprintf(out, "\tConnected To:\n")
74 + for _, c := range i.Connections {
75 + fmt.Fprintf(out, "\t%s\n\t\tLatency = %s\n", c.ID, c.Latency.String())
76 + }
77 + fmt.Fprintln(out)
78 + }
79 +}
core/commands2/root.go
+1
@@ -62,6 +62,7 @@ var rootSubcommands = map[string]*cmds.Command{
62 "publish": publishCmd,
63 "add": addCmd,
64 "log": logCmd,
65 + "diag": diagCmd,
66
67 // test subcommands
68 // TODO: remove these when we don't need them anymore