-
Notifications
You must be signed in to change notification settings - Fork 5
/
peerStats.go
executable file
·87 lines (76 loc) · 2.53 KB
/
peerStats.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
package main
import (
"net"
"time"
)
func peerStatLoop() {
for {
// Sleep until its time to update
time.Sleep(time.Duration(SystemConfig.DataSources.CjdnsPeers.Interval))
// Get peer stats from cjdns
results, err := Data.CjdnsConn.InterfaceController_peerStats()
if err != nil {
l.Noticeln(err)
return
}
Data.Node.RateIn, Data.Node.RateOut = 0, 0
Data.Node.BytesIn, Data.Node.BytesOut = int64(0), int64(0)
peerUpdate := make(map[string]Peer)
// loop through the results and update peer statistics while checking
// for any peers that are no longer there.
for _, peer := range results {
// Create the peer in the map if it's not there yet and calculate
// the IPv6 address. If it is there, just get the IPv6 address.
var IPv6 net.IP
if _, ok := Data.Peers[peer.PublicKey.String()]; !ok {
IPv6 = peer.PublicKey.IP()
Data.Peers[peer.PublicKey.String()] = Peer{
IPv6: IPv6,
}
} else {
IPv6 = Data.Peers[peer.PublicKey.String()].IPv6
}
// Calculate upload and download rate
newRateIn := float64(peer.BytesIn-Data.Peers[peer.PublicKey.String()].BytesIn) / (time.Since(Data.Peers[peer.PublicKey.String()].LastUpdate).Seconds())
newRateOut := float64(peer.BytesOut-Data.Peers[peer.PublicKey.String()].BytesOut) / (time.Since(Data.Peers[peer.PublicKey.String()].LastUpdate).Seconds())
// Update the peer statistics
peerUpdate[peer.PublicKey.String()] = Peer{
PublicKey: peer.PublicKey,
IPv6: Data.Peers[peer.PublicKey.String()].IPv6, // save the same IP
LastUpdate: time.Now(),
Last: peer.Last,
SwitchLabel: peer.SwitchLabel,
IsIncoming: peer.IsIncoming,
State: peer.State,
BytesIn: peer.BytesIn,
RateIn: newRateIn,
BytesOut: peer.BytesOut,
RateOut: newRateOut,
}
Data.Node.RateIn += newRateIn
Data.Node.RateOut += newRateOut
Data.Node.BytesIn += peer.BytesIn
Data.Node.BytesOut += peer.BytesOut
}
// Copy the new peer data to the persistant Data.Peers, this should drop
// any non existant peers.
Data.Peers = peerUpdate
/* for _, p := range Data.Peers {
//fmt.Println(
//p.IPv6.String(),
//p.RateIn.String()+"/s", "/", p.RateOut.String()+"/s", "In/Out", "Status:", p.State.String())
}
*/
/*
output, err := json.MarshalIndent(Data.Peers, "", "\t")
if err != nil {
fmt.Println(err)
//Pretty.Print(Data.Peers)
return
}
fmt.Println(string(output))
*/
//fmt.Println("Totals:", totalIn.String()+"/s", "/", totalOut.String()+"/s", "In/Out")
//fmt.Println()
}
}