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"
|
|
|
|
|
2018-02-20 07:02:01 +08:00
|
|
|
"github.com/cjbassi/gotop/utils"
|
2018-03-30 06:48:43 +08:00
|
|
|
ui "github.com/cjbassi/termui"
|
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
|
2018-04-11 12:34:11 +08:00
|
|
|
prevRecvTotal uint64
|
|
|
|
prevSentTotal 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)
|
2018-04-11 12:34:11 +08:00
|
|
|
curRecvTotal := interfaces[0].BytesRecv
|
|
|
|
curSentTotal := interfaces[0].BytesSent
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-04-11 12:34:11 +08:00
|
|
|
if self.prevRecvTotal != 0 { // if this isn't the first update
|
|
|
|
recvRecent := curRecvTotal - self.prevRecvTotal
|
|
|
|
sentRecent := curSentTotal - self.prevSentTotal
|
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-04-11 12:34:11 +08:00
|
|
|
|
|
|
|
if recvRecent < 0 || sentRecent < 0 {
|
|
|
|
utils.Error("net data",
|
|
|
|
fmt.Sprint(
|
|
|
|
"curRecvTotal: ", curRecvTotal, "\n",
|
|
|
|
"curSentTotal: ", curSentTotal, "\n",
|
|
|
|
"self.prevRecvTotal: ", self.prevRecvTotal, "\n",
|
|
|
|
"self.prevSentTotal: ", self.prevSentTotal, "\n",
|
|
|
|
"recvRecent: ", recvRecent, "\n",
|
|
|
|
"sentRecent: ", sentRecent,
|
|
|
|
))
|
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
// used in later calls to update
|
2018-04-11 12:34:11 +08:00
|
|
|
self.prevRecvTotal = curRecvTotal
|
|
|
|
self.prevSentTotal = curSentTotal
|
2018-02-20 07:02:01 +08:00
|
|
|
|
2018-04-11 12:34:11 +08:00
|
|
|
// net widget titles
|
2018-02-20 07:02:01 +08:00
|
|
|
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"
|
2018-02-20 07:02:01 +08:00
|
|
|
|
|
|
|
if i == 0 {
|
2018-04-11 12:34:11 +08:00
|
|
|
total = float64(curRecvTotal)
|
2018-02-20 07:02:01 +08:00
|
|
|
method = "Rx"
|
|
|
|
} else {
|
2018-04-11 12:34:11 +08:00
|
|
|
total = float64(curSentTotal)
|
2018-02-20 07:02:01 +08:00
|
|
|
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"
|
2018-02-20 07:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if total >= 1000000000 {
|
2018-03-04 09:05:52 +08:00
|
|
|
total = utils.BytesToGB(uint64(total))
|
|
|
|
unitTotal = "GB"
|
2018-02-20 07:02:01 +08:00
|
|
|
} else if total >= 1000000 {
|
2018-03-04 09:05:52 +08:00
|
|
|
total = utils.BytesToMB(uint64(total))
|
|
|
|
unitTotal = "MB"
|
2018-02-20 07:02:01 +08:00
|
|
|
}
|
|
|
|
|
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-20 07:02:01 +08:00
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|