-
Notifications
You must be signed in to change notification settings - Fork 929
/
app_instance_table.go
75 lines (63 loc) · 1.35 KB
/
app_instance_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
73
74
75
package helpers
import (
"regexp"
"strings"
)
type AppInstanceRow struct {
Index string
State string
Since string
CPU string
Memory string
Disk string
}
type AppProcessTable struct {
Title string
Instances []AppInstanceRow
}
type AppTable struct {
Processes []AppProcessTable
}
func ParseV3AppTable(input []byte) AppTable {
appTable := AppTable{}
rows := strings.Split(string(input), "\n")
for i, row := range rows {
if i < 10 {
// this is where the processes start
continue
}
if row == "" {
continue
}
if strings.HasPrefix(row, "#") {
// instance row
columns := splitColumns(row)
instanceRow := AppInstanceRow{
Index: columns[0],
State: columns[1],
Since: columns[2],
CPU: columns[3],
Memory: columns[4],
Disk: columns[5],
}
lastProcessIndex := len(appTable.Processes) - 1
appTable.Processes[lastProcessIndex].Instances = append(
appTable.Processes[lastProcessIndex].Instances,
instanceRow,
)
} else if !strings.HasPrefix(row, " ") {
// process title
appTable.Processes = append(appTable.Processes, AppProcessTable{
Title: row,
})
} else {
// column headers
continue
}
}
return appTable
}
func splitColumns(row string) []string {
// uses 3 spaces between columns
return regexp.MustCompile(`\s{3,}`).Split(strings.TrimSpace(row), -1)
}