summaryrefslogtreecommitdiffstats
path: root/widgets/net.go
blob: a38be1a35cb7674971593db8d23a1a812c8bd001 (plain)
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
package widgets

import (
	"fmt"
	"time"

	ui "github.com/cjbassi/gotop/termui"
	"github.com/cjbassi/gotop/utils"
	ps "github.com/shirou/gopsutil/net"
)

type Net struct {
	*ui.Sparklines
	interval  time.Duration
	recvTotal uint64
	sentTotal uint64
}

func NewNet() *Net {
	recv := ui.NewSparkline()
	recv.Data = []int{0}

	sent := ui.NewSparkline()
	sent.Data = []int{0}

	spark := ui.NewSparklines(recv, sent)
	n := &Net{spark, time.Second, 0, 0}
	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() {
	interfaces, _ := ps.IOCounters(false)
	recv := interfaces[0].BytesRecv
	sent := interfaces[0].BytesSent

	if n.recvTotal != 0 { // if this isn't the first update
		curRecv := recv - n.recvTotal
		curSent := sent - n.sentTotal

		n.Lines[0].Data = append(n.Lines[0].Data, int(curRecv))
		n.Lines[1].Data = append(n.Lines[1].Data, int(curSent))
	}

	n.recvTotal = recv
	n.sentTotal = sent

	for i := 0; i < 2; i++ {
		var method string
		var total uint64
		cur := n.Lines[i].Data[len(n.Lines[i].Data)-1]
		totalUnit := "B"
		curUnit := "B"

		if i == 0 {
			total = n.recvTotal
			method = "Rx"
		} else {
			total = n.sentTotal
			method = "Tx"
		}

		if cur >= 1000000 {
			cur = int(utils.BytesToMB(uint64(cur)))
			curUnit = "MB"
		} else if cur >= 1000 {
			cur = int(utils.BytesToKB(uint64(cur)))
			curUnit = "kB"
		}

		if total >= 1000000000 {
			total = utils.BytesToGB(total)
			totalUnit = "GB"
		} else if total >= 1000000 {
			total = utils.BytesToMB(total)
			totalUnit = "MB"
		}

		n.Lines[i].Title1 = fmt.Sprintf(" Total %s: %3d %s", method, total, totalUnit)
		n.Lines[i].Title2 = fmt.Sprintf(" %s/s: %7d %2s/s", method, cur, curUnit)
	}
}