stat.go 958 Bytes
Newer Older
1 2 3
package bitswap

import (
Jeromy's avatar
Jeromy committed
4
	"sort"
5

Jeromy's avatar
Jeromy committed
6
	cid "github.com/ipfs/go-cid"
7 8 9
)

type Stat struct {
10 11 12 13 14 15 16 17 18 19
	ProvideBufLen    int
	Wantlist         []cid.Cid
	Peers            []string
	BlocksReceived   uint64
	DataReceived     uint64
	BlocksSent       uint64
	DataSent         uint64
	DupBlksReceived  uint64
	DupDataReceived  uint64
	MessagesReceived uint64
20 21 22 23 24 25
}

func (bs *Bitswap) Stat() (*Stat, error) {
	st := new(Stat)
	st.ProvideBufLen = len(bs.newBlocks)
	st.Wantlist = bs.GetWantlist()
Jeromy's avatar
Jeromy committed
26
	bs.counterLk.Lock()
27 28 29 30 31 32 33
	c := bs.counters
	st.BlocksReceived = c.blocksRecvd
	st.DupBlksReceived = c.dupBlocksRecvd
	st.DupDataReceived = c.dupDataRecvd
	st.BlocksSent = c.blocksSent
	st.DataSent = c.dataSent
	st.DataReceived = c.dataRecvd
34
	st.MessagesReceived = c.messagesRecvd
Jeromy's avatar
Jeromy committed
35
	bs.counterLk.Unlock()
36

37 38 39 40
	peers := bs.engine.Peers()
	st.Peers = make([]string, 0, len(peers))

	for _, p := range peers {
Jeromy's avatar
Jeromy committed
41 42 43
		st.Peers = append(st.Peers, p.Pretty())
	}
	sort.Strings(st.Peers)
44 45 46

	return st, nil
}