diff --git a/package.json b/package.json index bd0e456df..15d1d0df8 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,12 @@ "hash": "QmQine7gvHncNevKtG9QXxf3nXcwSj6aDDmMm52mHofEEp", "name": "tar-utils", "version": "0.0.3" + }, + { + "author": "whyrusleeping", + "hash": "QmcoBbyTiL9PFjo1GFixJwqQ8mZLJ36CribuqyKmS1okPu", + "name": "go-libp2p-metrics", + "version": "2.1.3" } ], "gxVersion": "0.7.0", diff --git a/shell.go b/shell.go index 2a46737db..5bdb573a0 100644 --- a/shell.go +++ b/shell.go @@ -20,6 +20,8 @@ import ( ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" tar "github.com/whyrusleeping/tar-utils" + + p2pmetrics "github.com/libp2p/go-libp2p-metrics" ) const ( @@ -92,6 +94,25 @@ func (s *Shell) newRequest(ctx context.Context, command string, args ...string) return NewRequest(ctx, s.url, command, args...) } +func (s *Shell) Request(ctx context.Context, command string, args ...string) (*Request, *Response, error) { + req := s.newRequest(ctx, command, args...) + res, err := req.Send(s.httpcli) + return req, res, err +} + +func (s *Shell) RequestDecode(ctx context.Context, dec interface{}, command string, args ...string) error { + _, res, err := s.Request(ctx, command, args...) + if err != nil { + return err + } + defer res.Close() + if res.Error != nil { + return res.Error + } + + return json.NewDecoder(res.Output).Decode(dec) +} + type IdOutput struct { ID string PublicKey string @@ -831,3 +852,34 @@ func (s *Shell) ObjectStat(key string) (*ObjectStats, error) { return stat, nil } + +// ObjectStat gets stats for the DAG object named by key. It returns +// the stats of the requested Object or an error. +func (s *Shell) StatsBW() (*p2pmetrics.Stats, error) { + v := &p2pmetrics.Stats{} + err := s.RequestDecode(context.TODO(), &v, "stats/bw") + return v, err +} + +type SwarmStreamInfo struct { + Protocol string +} + +type SwarmConnInfo struct { + Addr string + Peer string + Latency string + Muxer string + Streams []SwarmStreamInfo +} + +type SwarmConnInfos struct { + Peers []SwarmConnInfo +} + +// SwarmPeers gets all the swarm peers +func (s *Shell) SwarmPeers() (*SwarmConnInfos, error) { + v := &SwarmConnInfos{} + err := s.RequestDecode(context.TODO(), &v, "swarm/peers") + return v, err +} diff --git a/shell_test.go b/shell_test.go index 26b43eaa1..376ea7b8f 100644 --- a/shell_test.go +++ b/shell_test.go @@ -242,3 +242,17 @@ func TestDagPut(t *testing.T) { is.Nil(err) is.Equal(c, "zdpuAt47YjE9XTgSxUBkiYCbmnktKajQNheQBGASHj3FfYf8M") } + +func TestStatsBW(t *testing.T) { + is := is.New(t) + s := NewShell(shellUrl) + _, err := s.StatsBW() + is.Nil(err) +} + +func TestSwarmPeers(t *testing.T) { + is := is.New(t) + s := NewShell(shellUrl) + _, err := s.SwarmPeers() + is.Nil(err) +}