-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
72 lines (60 loc) · 1.7 KB
/
table.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
package main
import (
"fmt"
"os"
"sort"
"time"
"github.com/canonical/lxd/shared/units"
"github.com/olekukonko/tablewriter"
)
func renderTable(traffic trafficCounter) {
for {
time.Sleep(3 * time.Second)
fmt.Print("\033[H\033[2J")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PEER", "IPV4 RX", "IPV4 TX", "IPV6 RX", "IPV6 TX", "TOTAL", "NOTE"})
table.SetBorder(false)
table.SetAutoWrapText(false)
data := traffic.toSlice()
sort.SliceStable(data, func(i int, j int) bool {
return data[i].total > data[j].total
})
for _, entry := range data {
// Ignore anything that's got less than 1MiB of traffic.
if entry.total < 1024*1024 {
continue
}
var name string
if overrides[entry.hwaddr] != nil && overrides[entry.hwaddr].Company != "" {
name = overrides[entry.hwaddr].Company
} else if members[entry.hwaddr] != nil {
name = members[entry.hwaddr].Company
} else {
name = fmt.Sprintf("UNKNOWN (%s)", entry.hwaddr)
}
var asn string
if overrides[entry.hwaddr] != nil && overrides[entry.hwaddr].ASN != "" {
asn = overrides[entry.hwaddr].ASN
} else if members[entry.hwaddr] != nil {
asn = members[entry.hwaddr].ASN
}
if asn != "" {
name = fmt.Sprintf("%s (%s)", name, asn)
}
var note string
if overrides[entry.hwaddr] != nil && overrides[entry.hwaddr].Note != "" {
note = overrides[entry.hwaddr].Note
}
table.Append([]string{
name,
units.GetByteSizeString(entry.v4rx, 2),
units.GetByteSizeString(entry.v4tx, 2),
units.GetByteSizeString(entry.v6rx, 2),
units.GetByteSizeString(entry.v6tx, 2),
units.GetByteSizeString(entry.total, 2),
note,
})
}
table.Render()
}
}