-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
181 lines (161 loc) · 4.21 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package commands
import (
"bytes"
"errors"
"fmt"
"io"
"time"
humanize "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
cmds "github.com/ipfs/go-ipfs/commands"
metrics "github.com/ipfs/go-ipfs/metrics"
peer "github.com/ipfs/go-ipfs/p2p/peer"
protocol "github.com/ipfs/go-ipfs/p2p/protocol"
u "github.com/ipfs/go-ipfs/util"
)
var StatsCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Query IPFS statistics",
ShortDescription: ``,
},
Subcommands: map[string]*cmds.Command{
"bw": statBwCmd,
},
}
var statBwCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print ipfs bandwidth information",
ShortDescription: ``,
},
Options: []cmds.Option{
cmds.StringOption("peer", "p", "specify a peer to print bandwidth for"),
cmds.StringOption("proto", "t", "specify a protocol to print bandwidth for"),
cmds.BoolOption("poll", "print bandwidth at an interval"),
cmds.StringOption("interval", "i", "time interval to wait between updating output"),
},
Run: func(req cmds.Request, res cmds.Response) {
nd, err := req.Context().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
// Must be online!
if !nd.OnlineMode() {
res.SetError(errNotOnline, cmds.ErrClient)
return
}
pstr, pfound, err := req.Option("peer").String()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
tstr, tfound, err := req.Option("proto").String()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if pfound && tfound {
res.SetError(errors.New("please only specify peer OR protocol"), cmds.ErrClient)
return
}
var pid peer.ID
if pfound {
checkpid, err := peer.IDB58Decode(pstr)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pid = checkpid
}
interval := time.Second
timeS, found, err := req.Option("interval").String()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if found {
v, err := time.ParseDuration(timeS)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
interval = v
}
doPoll, _, err := req.Option("poll").Bool()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))
go func() {
defer close(out)
for {
if pfound {
stats := nd.Reporter.GetBandwidthForPeer(pid)
out <- &stats
} else if tfound {
protoId := protocol.ID(tstr)
stats := nd.Reporter.GetBandwidthForProtocol(protoId)
out <- &stats
} else {
totals := nd.Reporter.GetBandwidthTotals()
out <- &totals
}
if !doPoll {
return
}
select {
case <-time.After(interval):
case <-req.Context().Context.Done():
return
}
}
}()
},
Type: metrics.Stats{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
outCh, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}
polling, _, err := res.Request().Option("poll").Bool()
if err != nil {
return nil, err
}
first := true
marshal := func(v interface{}) (io.Reader, error) {
bs, ok := v.(*metrics.Stats)
if !ok {
return nil, u.ErrCast()
}
out := new(bytes.Buffer)
if !polling {
printStats(out, bs)
} else {
if first {
fmt.Fprintln(out, "Total Up\t Total Down\t Rate Up\t Rate Down")
first = false
}
fmt.Fprint(out, "\r")
fmt.Fprintf(out, "%s \t\t", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(out, " %s \t\t", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(out, " %s/s \t", humanize.Bytes(uint64(bs.RateOut)))
fmt.Fprintf(out, " %s/s ", humanize.Bytes(uint64(bs.RateIn)))
}
return out, nil
}
return &cmds.ChannelMarshaler{
Channel: outCh,
Marshaler: marshal,
}, nil
},
},
}
func printStats(out io.Writer, bs *metrics.Stats) {
fmt.Fprintln(out, "Bandwidth")
fmt.Fprintf(out, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn)))
fmt.Fprintf(out, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut)))
fmt.Fprintf(out, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn)))
fmt.Fprintf(out, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut)))
}