-
Notifications
You must be signed in to change notification settings - Fork 2k
/
helper_devices.go
123 lines (94 loc) · 2.82 KB
/
helper_devices.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package command
import (
"fmt"
"sort"
"github.com/hashicorp/nomad/api"
"github.com/mitchellh/cli"
)
func deviceQualifiedID(vendor, typ, name, id string) string {
p := vendor
if typ != "" {
p += "/" + typ
}
if name != "" {
p += "/" + name
}
return p + "[" + id + "]"
}
func buildDeviceStatsSummaryMap(deviceGroupStats []*api.DeviceGroupStats) map[string]*api.StatValue {
r := map[string]*api.StatValue{}
for _, dg := range deviceGroupStats {
for id, stats := range dg.InstanceStats {
k := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, id)
r[k] = stats.Summary
}
}
return r
}
func formatDeviceStats(qid string, stat *api.StatObject) []string {
attrs := []string{fmt.Sprintf("Device|%s", qid)}
formatDeviceStatsImpl(stat, "", &attrs)
sort.Strings(attrs[1:])
return attrs
}
func formatDeviceStatsImpl(stat *api.StatObject, keyPrefix string, result *[]string) {
if keyPrefix != "" {
keyPrefix = keyPrefix + "."
}
for n, stat := range stat.Attributes {
*result = append(*result, fmt.Sprintf("%s%s|%s", keyPrefix, n, stat))
}
for k, o := range stat.Nested {
formatDeviceStatsImpl(o, keyPrefix+k, result)
}
}
// getDeviceResourcesForNode returns a list of devices and their statistics summary
// and tracks devices without statistics
func getDeviceResourcesForNode(deviceGroupStats []*api.DeviceGroupStats, node *api.Node) []string {
statsSummaryMap := buildDeviceStatsSummaryMap(deviceGroupStats)
devices := []string{}
for _, dg := range node.NodeResources.Devices {
for _, inst := range dg.Instances {
id := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, inst.ID)
statStr := ""
if stats, ok := statsSummaryMap[id]; ok && stats != nil {
statStr = stats.String()
}
devices = append(devices, fmt.Sprintf("%v|%v", id, statStr))
}
}
sort.Strings(devices)
return devices
}
// getDeviceResources returns alist of devices and their statistics summary
func getDeviceResources(deviceGroupStats []*api.DeviceGroupStats) []string {
statsSummaryMap := buildDeviceStatsSummaryMap(deviceGroupStats)
result := make([]string, 0, len(statsSummaryMap))
for id, stats := range statsSummaryMap {
result = append(result, id+"|"+stats.String())
}
sort.Strings(result)
return result
}
func printDeviceStats(ui cli.Ui, deviceGroupStats []*api.DeviceGroupStats) {
isFirst := true
for _, dg := range deviceGroupStats {
for id, dinst := range dg.InstanceStats {
if !isFirst {
ui.Output("")
}
isFirst = false
qid := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, id)
attrs := formatDeviceStats(qid, dinst.Stats)
ui.Output(formatKV(attrs))
}
}
}
func getDeviceAttributes(d *api.NodeDeviceResource) []string {
attrs := []string{fmt.Sprintf("Device Group|%s", d.ID())}
for k, v := range d.Attributes {
attrs = append(attrs, k+"|"+v.String())
}
sort.Strings(attrs[1:])
return attrs
}