From 8aec84311ebfcf08bcf551c099d6f6205ac969c5 Mon Sep 17 00:00:00 2001 From: jbenet Date: Sat, 7 Jul 2018 01:18:09 -0700 Subject: [PATCH 1/4] added Request and RequestDecode related to #92 --- shell.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/shell.go b/shell.go index 2a46737db..2a3a6631b 100644 --- a/shell.go +++ b/shell.go @@ -92,6 +92,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 From 0a76d867e89c148ab9be0837846a6440c8e578e5 Mon Sep 17 00:00:00 2001 From: jbenet Date: Sat, 7 Jul 2018 01:19:09 -0700 Subject: [PATCH 2/4] added stats bw and swarm peers --- shell.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/shell.go b/shell.go index 2a3a6631b..09bbd234d 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 "gx/ipfs/QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy/go-libp2p-metrics" ) const ( @@ -850,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 +} From 7173b3cc953726ad00eb7e55a559ab313922758d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 11 Jul 2018 16:04:35 +0200 Subject: [PATCH 3/4] gx import go libp2p metrics (and unrewrite) --- package.json | 6 ++++++ shell.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) 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 09bbd234d..5bdb573a0 100644 --- a/shell.go +++ b/shell.go @@ -21,7 +21,7 @@ import ( manet "github.com/multiformats/go-multiaddr-net" tar "github.com/whyrusleeping/tar-utils" - p2pmetrics "gx/ipfs/QmdeBtQGXjSt7cb97nx9JyLHHv5va2LyEAue7Q5tDFzpLy/go-libp2p-metrics" + p2pmetrics "github.com/libp2p/go-libp2p-metrics" ) const ( From 2960bf935289bb0e1eb5c0914489d962f1095ce3 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 11 Jul 2018 16:08:41 +0200 Subject: [PATCH 4/4] add basic tests for stats and swarm peers --- shell_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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) +}