2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-02-20 07:02:01 +08:00
|
|
|
"fmt"
|
2018-02-19 15:25:02 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
ui "github.com/cjbassi/gotop/termui"
|
2018-02-20 07:02:01 +08:00
|
|
|
"github.com/cjbassi/gotop/utils"
|
2018-02-23 16:42:39 +08:00
|
|
|
net "github.com/shirou/gopsutil/net"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Net struct {
|
|
|
|
*ui.Sparklines
|
2018-02-23 16:42:39 +08:00
|
|
|
interval time.Duration
|
|
|
|
// used to calculate recent network activity
|
2018-02-20 12:00:18 +08:00
|
|
|
recvTotal uint64
|
|
|
|
sentTotal uint64
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNet() *Net {
|
|
|
|
recv := ui.NewSparkline()
|
|
|
|
recv.Data = []int{0}
|
|
|
|
|
|
|
|
sent := ui.NewSparkline()
|
|
|
|
sent.Data = []int{0}
|
|
|
|
|
|
|
|
spark := ui.NewSparklines(recv, sent)
|
2018-02-20 07:02:01 +08:00
|
|
|
n := &Net{spark, time.Second, 0, 0}
|
2018-02-19 15:25:02 +08:00
|
|
|
n.Label = "Network Usage"
|
|
|
|
|
|
|
|
go n.update()
|
|
|
|
ticker := time.NewTicker(n.interval)
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
|
|
|
n.update()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Net) update() {
|
2018-02-23 16:42:39 +08:00
|
|
|
// `false` causes psutil to group all network activity
|
|
|
|
interfaces, _ := net.IOCounters(false)
|
2018-02-20 12:00:18 +08:00
|
|
|
recv := interfaces[0].BytesRecv
|
|
|
|
sent := interfaces[0].BytesSent
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-02-20 07:02:01 +08:00
|
|
|
if n.recvTotal != 0 { // if this isn't the first update
|
|
|
|
curRecv := recv - n.recvTotal
|
|
|
|
curSent := sent - n.sentTotal
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-02-20 12:00:18 +08:00
|
|
|
n.Lines[0].Data = append(n.Lines[0].Data, int(curRecv))
|
|
|
|
n.Lines[1].Data = append(n.Lines[1].Data, int(curSent))
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 16:42:39 +08:00
|
|
|
// used for later calls to update
|
2018-02-20 07:02:01 +08:00
|
|
|
n.recvTotal = recv
|
|
|
|
n.sentTotal = sent
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
var method string
|
2018-02-20 12:00:18 +08:00
|
|
|
var total uint64
|
2018-02-20 07:02:01 +08:00
|
|
|
cur := n.Lines[i].Data[len(n.Lines[i].Data)-1]
|
|
|
|
totalUnit := "B"
|
|
|
|
curUnit := "B"
|
|
|
|
|
|
|
|
if i == 0 {
|
2018-02-23 16:42:39 +08:00
|
|
|
total = recv
|
2018-02-20 07:02:01 +08:00
|
|
|
method = "Rx"
|
|
|
|
} else {
|
2018-02-23 16:42:39 +08:00
|
|
|
total = sent
|
2018-02-20 07:02:01 +08:00
|
|
|
method = "Tx"
|
|
|
|
}
|
|
|
|
|
|
|
|
if cur >= 1000000 {
|
2018-02-20 12:00:18 +08:00
|
|
|
cur = int(utils.BytesToMB(uint64(cur)))
|
2018-02-20 07:02:01 +08:00
|
|
|
curUnit = "MB"
|
|
|
|
} else if cur >= 1000 {
|
2018-02-20 12:00:18 +08:00
|
|
|
cur = int(utils.BytesToKB(uint64(cur)))
|
2018-02-20 07:02:01 +08:00
|
|
|
curUnit = "kB"
|
|
|
|
}
|
|
|
|
|
2018-02-23 15:58:48 +08:00
|
|
|
var totalCvrt float64
|
2018-02-20 07:02:01 +08:00
|
|
|
if total >= 1000000000 {
|
2018-02-23 15:58:48 +08:00
|
|
|
totalCvrt = utils.BytesToGB(total)
|
2018-02-20 07:02:01 +08:00
|
|
|
totalUnit = "GB"
|
|
|
|
} else if total >= 1000000 {
|
2018-02-23 15:58:48 +08:00
|
|
|
totalCvrt = utils.BytesToMB(total)
|
2018-02-20 07:02:01 +08:00
|
|
|
totalUnit = "MB"
|
|
|
|
}
|
|
|
|
|
2018-02-23 15:58:48 +08:00
|
|
|
n.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", method, totalCvrt, totalUnit)
|
|
|
|
n.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9d %2s/s", method, cur, curUnit)
|
2018-02-20 07:02:01 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|