github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/diag.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"time"
     7  
     8  	cmds "github.com/jbenet/go-ipfs/commands"
     9  	diagn "github.com/jbenet/go-ipfs/diagnostics"
    10  )
    11  
    12  type DiagnosticConnection struct {
    13  	ID      string
    14  	Latency int64
    15  }
    16  
    17  type DiagnosticPeer struct {
    18  	ID           string
    19  	LifeSpan     float64
    20  	BandwidthIn  uint64
    21  	BandwidthOut uint64
    22  	Connections  []DiagnosticConnection
    23  }
    24  
    25  type DiagnosticOutput struct {
    26  	Peers []DiagnosticPeer
    27  }
    28  
    29  var diagCmd = &cmds.Command{
    30  	Description: "Generates diagnostic reports",
    31  
    32  	Subcommands: map[string]*cmds.Command{
    33  		"net": diagNetCmd,
    34  	},
    35  }
    36  
    37  var diagNetCmd = &cmds.Command{
    38  	Description: "Generates a network diagnostics report",
    39  	Help: `Sends out a message to each node in the network recursively
    40  requesting a listing of data about them including number of
    41  connected peers and latencies between them.
    42  `,
    43  
    44  	Run: func(req cmds.Request) (interface{}, error) {
    45  		n := req.Context().Node
    46  
    47  		if !n.OnlineMode() {
    48  			return nil, errNotOnline
    49  		}
    50  
    51  		info, err := n.Diagnostics.GetDiagnostic(time.Second * 20)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  
    56  		output := make([]DiagnosticPeer, len(info))
    57  		for i, peer := range info {
    58  			connections := make([]DiagnosticConnection, len(peer.Connections))
    59  			for j, conn := range peer.Connections {
    60  				connections[j] = DiagnosticConnection{
    61  					ID:      conn.ID,
    62  					Latency: conn.Latency.Nanoseconds(),
    63  				}
    64  			}
    65  
    66  			output[i] = DiagnosticPeer{
    67  				ID:           peer.ID,
    68  				LifeSpan:     peer.LifeSpan.Minutes(),
    69  				BandwidthIn:  peer.BwIn,
    70  				BandwidthOut: peer.BwOut,
    71  				Connections:  connections,
    72  			}
    73  		}
    74  
    75  		return &DiagnosticOutput{output}, nil
    76  	},
    77  	Type: &DiagnosticOutput{},
    78  }
    79  
    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)
    89  	}
    90  }