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

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

Steven Allen's avatar
Steven Allen committed
6
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
7 8 9
)

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

func (bs *Bitswap) Stat() (*Stat, error) {
	st := new(Stat)
	st.ProvideBufLen = len(bs.newBlocks)
	st.Wantlist = bs.GetWantlist()
Jeromy's avatar
Jeromy committed
25
	bs.counterLk.Lock()
26 27 28 29 30 31 32
	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
Jeromy's avatar
Jeromy committed
33
	bs.counterLk.Unlock()
34

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

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

	return st, nil
}