pretty print kad inspector (#743)

* pretty print kad inspector

* fix format string

* cleanup

* cleanup

* usage, args, print improvements
This commit is contained in:
Bryan White 2018-11-30 18:36:05 +01:00 committed by GitHub
parent 6b68b0c368
commit 71ef93feed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 19 deletions

View File

@ -13,8 +13,10 @@ import (
"os" "os"
"strconv" "strconv"
"github.com/gogo/protobuf/jsonpb"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/zeebo/errs" "github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/pkg/pb" "storj.io/storj/pkg/pb"
"storj.io/storj/pkg/process" "storj.io/storj/pkg/process"
@ -65,32 +67,36 @@ var (
getBucketCmd = &cobra.Command{ getBucketCmd = &cobra.Command{
Use: "ls <bucket_id>", Use: "ls <bucket_id>",
Short: "get all nodes in bucket", Short: "get all nodes in bucket",
Args: cobra.MinimumNArgs(1),
RunE: GetBucket, RunE: GetBucket,
} }
pingNodeCmd = &cobra.Command{ pingNodeCmd = &cobra.Command{
Use: "ping <node_id>", Use: "ping <node_id> <ip:port>",
Short: "ping node at provided ID", Short: "ping node at provided ID",
Args: cobra.MinimumNArgs(2),
RunE: PingNode, RunE: PingNode,
} }
getStatsCmd = &cobra.Command{ getStatsCmd = &cobra.Command{
Use: "getstats", Use: "getstats <node_id>",
Short: "Get node stats", Short: "Get node stats",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: GetStats, RunE: GetStats,
} }
getCSVStatsCmd = &cobra.Command{ getCSVStatsCmd = &cobra.Command{
Use: "getcsvstats", Use: "getcsvstats <path to node ID csv file>",
Short: "Get node stats from csv", Short: "Get node stats from csv",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
RunE: GetCSVStats, RunE: GetCSVStats,
} }
createStatsCmd = &cobra.Command{ createStatsCmd = &cobra.Command{
// TODO: add args to usage
Use: "createstats", Use: "createstats",
Short: "Create node with stats", Short: "Create node with stats",
Args: cobra.MinimumNArgs(5), // id, auditct, auditsuccessct, uptimect, uptimesuccessct Args: cobra.MinimumNArgs(5), // id, auditct, auditsuccessct, uptimect, uptimesuccessct
RunE: CreateStats, RunE: CreateStats,
} }
createCSVStatsCmd = &cobra.Command{ createCSVStatsCmd = &cobra.Command{
// TODO: add args to usage
Use: "createcsvstats", Use: "createcsvstats",
Short: "Create node stats from csv", Short: "Create node stats from csv",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
@ -165,10 +171,6 @@ func GetBuckets(cmd *cobra.Command, args []string) (err error) {
// GetBucket returns a bucket with given `id` // GetBucket returns a bucket with given `id`
func GetBucket(cmd *cobra.Command, args []string) (err error) { func GetBucket(cmd *cobra.Command, args []string) (err error) {
if len(args) < 1 {
return errs.New("Must provide at least one bucket ID")
}
i, err := NewInspector(*Addr) i, err := NewInspector(*Addr)
if err != nil { if err != nil {
return ErrInspectorDial.Wrap(err) return ErrInspectorDial.Wrap(err)
@ -186,15 +188,21 @@ func GetBucket(cmd *cobra.Command, args []string) (err error) {
return ErrRequest.Wrap(err) return ErrRequest.Wrap(err)
} }
fmt.Printf("Bucket ----------- \n %+v\n", bucket) fmt.Println(prettyPrintBucket(bucket))
return nil return nil
} }
func prettyPrintBucket(b *pb.GetBucketResponse) string {
m := jsonpb.Marshaler{Indent: " ", EmitDefaults: false}
s, err := m.MarshalToString(b)
if err != nil {
zap.S().Error("error marshaling bucket: %s", b.Id)
}
return s
}
// PingNode sends a PING RPC across the Kad network to check node availability // PingNode sends a PING RPC across the Kad network to check node availability
func PingNode(cmd *cobra.Command, args []string) (err error) { func PingNode(cmd *cobra.Command, args []string) (err error) {
if len(args) < 2 {
return errs.New("Must provide a node ID and address to ping")
}
nodeID, err := storj.NodeIDFromString(args[0]) nodeID, err := storj.NodeIDFromString(args[0])
if err != nil { if err != nil {
return err return err
@ -212,7 +220,16 @@ func PingNode(cmd *cobra.Command, args []string) (err error) {
Address: args[1], Address: args[1],
}) })
fmt.Printf("\n -- Ping response: %+v\n, -- Error: %+v\n", p, err) var okayString string
if p.Ok {
okayString = "OK"
} else {
okayString = "Error"
}
fmt.Printf("\n -- Ping response: %s\n", okayString)
if err != nil {
fmt.Printf(" -- Error: %s", err)
}
return nil return nil
} }

View File

@ -5,7 +5,6 @@ package inspector
import ( import (
"context" "context"
"fmt"
"github.com/zeebo/errs" "github.com/zeebo/errs"
"go.uber.org/zap" "go.uber.org/zap"
@ -42,9 +41,15 @@ type Server struct {
// CountNodes returns the number of nodes in the cache and in kademlia // CountNodes returns the number of nodes in the cache and in kademlia
func (srv *Server) CountNodes(ctx context.Context, req *pb.CountNodesRequest) (*pb.CountNodesResponse, error) { func (srv *Server) CountNodes(ctx context.Context, req *pb.CountNodesRequest) (*pb.CountNodesResponse, error) {
overlayKeys, err := srv.cache.DB.List(nil, 0)
if err != nil {
return nil, err
}
kadNodes := srv.dht.Seen()
return &pb.CountNodesResponse{ return &pb.CountNodesResponse{
Kademlia: 0, Kademlia: int64(len(kadNodes)),
Overlay: 0, Overlay: int64(len(overlayKeys)),
}, nil }, nil
} }
@ -106,14 +111,13 @@ func (srv *Server) PingNode(ctx context.Context, req *pb.PingNodeRequest) (*pb.P
Address: req.Address, Address: req.Address,
}, },
}) })
res := &pb.PingNodeResponse{Ok: p}
if err != nil { if err != nil {
return &pb.PingNodeResponse{}, ServerError.Wrap(err) return res, ServerError.Wrap(err)
} }
fmt.Printf("---- Pinged Node: %+v\n", p) return res, nil
return &pb.PingNodeResponse{}, nil
} }
// --------------------- // ---------------------