xmtop/widgets/net.go

99 lines
2.1 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"fmt"
2018-02-19 15:25:02 +08:00
"time"
ui "github.com/cjbassi/gotop/termui"
"github.com/cjbassi/gotop/utils"
2018-03-04 09:05:52 +08:00
psNet "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
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-03-28 05:27:23 +08:00
self := &Net{
2018-03-04 09:05:52 +08:00
Sparklines: spark,
interval: time.Second,
}
2018-03-28 05:27:23 +08:00
self.Label = "Network Usage"
2018-02-19 15:25:02 +08:00
2018-03-28 05:27:23 +08:00
go self.update()
ticker := time.NewTicker(self.interval)
2018-02-19 15:25:02 +08:00
go func() {
for range ticker.C {
2018-03-28 05:27:23 +08:00
self.update()
2018-02-19 15:25:02 +08:00
}
}()
2018-03-28 05:27:23 +08:00
return self
2018-02-19 15:25:02 +08:00
}
2018-03-28 05:27:23 +08:00
func (self *Net) update() {
2018-02-23 16:42:39 +08:00
// `false` causes psutil to group all network activity
2018-03-04 09:05:52 +08:00
interfaces, _ := psNet.IOCounters(false)
recvTotal := interfaces[0].BytesRecv
sentTotal := interfaces[0].BytesSent
2018-02-19 15:25:02 +08:00
2018-03-28 05:27:23 +08:00
if self.recvTotal != 0 { // if this isn't the first update
recvRecent := recvTotal - self.recvTotal
sentRecent := sentTotal - self.sentTotal
2018-02-19 15:25:02 +08:00
2018-03-28 05:27:23 +08:00
self.Lines[0].Data = append(self.Lines[0].Data, int(recvRecent))
self.Lines[1].Data = append(self.Lines[1].Data, int(sentRecent))
2018-02-19 15:25:02 +08:00
}
2018-03-04 09:05:52 +08:00
// used in later calls to update
2018-03-28 05:27:23 +08:00
self.recvTotal = recvTotal
self.sentTotal = sentTotal
2018-02-24 13:15:38 +08:00
// renders net widget titles
for i := 0; i < 2; i++ {
2018-02-24 13:15:38 +08:00
var method string // either 'Rx' or 'Tx'
2018-03-04 09:05:52 +08:00
var total float64
2018-03-28 05:27:23 +08:00
recent := self.Lines[i].Data[len(self.Lines[i].Data)-1]
2018-03-04 09:05:52 +08:00
unitTotal := "B"
unitRecent := "B"
if i == 0 {
2018-03-04 09:05:52 +08:00
total = float64(recvTotal)
method = "Rx"
} else {
2018-03-04 09:05:52 +08:00
total = float64(sentTotal)
method = "Tx"
}
2018-03-04 09:05:52 +08:00
if recent >= 1000000 {
recent = int(utils.BytesToMB(uint64(recent)))
unitRecent = "MB"
} else if recent >= 1000 {
recent = int(utils.BytesToKB(uint64(recent)))
unitRecent = "kB"
}
if total >= 1000000000 {
2018-03-04 09:05:52 +08:00
total = utils.BytesToGB(uint64(total))
unitTotal = "GB"
} else if total >= 1000000 {
2018-03-04 09:05:52 +08:00
total = utils.BytesToMB(uint64(total))
unitTotal = "MB"
}
2018-03-28 05:27:23 +08:00
self.Lines[i].Title1 = fmt.Sprintf(" Total %s: %5.1f %s", method, total, unitTotal)
self.Lines[i].Title2 = fmt.Sprintf(" %s/s: %9d %2s/s", method, recent, unitRecent)
}
2018-02-19 15:25:02 +08:00
}