2018-11-14 16:51:50 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2018-11-15 03:11:59 +00:00
|
|
|
"fmt"
|
2018-11-16 22:28:17 +00:00
|
|
|
"sort"
|
2018-11-15 03:11:59 +00:00
|
|
|
|
2018-11-14 16:51:50 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2018-11-15 19:44:13 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2018-11-14 16:51:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func deviceQualifiedID(vendor, typ, name, id string) string {
|
|
|
|
p := vendor
|
|
|
|
if typ != "" {
|
|
|
|
p += "/" + typ
|
|
|
|
}
|
|
|
|
if name != "" {
|
|
|
|
p += "/" + name
|
|
|
|
}
|
|
|
|
|
|
|
|
return p + "[" + id + "]"
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:44:13 +00:00
|
|
|
func buildDeviceStatsSummaryMap(deviceGroupStats []*api.DeviceGroupStats) map[string]*api.StatValue {
|
2018-11-14 16:51:50 +00:00
|
|
|
r := map[string]*api.StatValue{}
|
|
|
|
|
2018-11-15 19:44:13 +00:00
|
|
|
for _, dg := range deviceGroupStats {
|
2018-11-14 16:51:50 +00:00
|
|
|
for id, stats := range dg.InstanceStats {
|
|
|
|
k := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, id)
|
|
|
|
r[k] = stats.Summary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2018-11-15 03:11:59 +00:00
|
|
|
|
2018-11-16 22:28:17 +00:00
|
|
|
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) {
|
2018-11-15 03:11:59 +00:00
|
|
|
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 {
|
2018-11-16 22:28:17 +00:00
|
|
|
formatDeviceStatsImpl(o, keyPrefix+k, result)
|
2018-11-15 03:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 19:44:13 +00:00
|
|
|
|
2018-11-15 19:57:15 +00:00
|
|
|
// getDeviceResourcesForNode returns a list of devices and their statistics summary
|
|
|
|
// and tracks devices without statistics
|
2018-11-15 19:44:13 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 22:28:17 +00:00
|
|
|
sort.Strings(devices)
|
|
|
|
|
2018-11-15 19:44:13 +00:00
|
|
|
return devices
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:57:15 +00:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2018-11-16 22:28:17 +00:00
|
|
|
sort.Strings(result)
|
|
|
|
|
2018-11-15 19:57:15 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:44:13 +00:00
|
|
|
func printDeviceStats(ui cli.Ui, deviceGroupStats []*api.DeviceGroupStats) {
|
|
|
|
isFirst := true
|
|
|
|
for _, dg := range deviceGroupStats {
|
|
|
|
for id, dinst := range dg.InstanceStats {
|
|
|
|
if !isFirst {
|
2018-11-16 15:37:39 +00:00
|
|
|
ui.Output("")
|
2018-11-15 19:44:13 +00:00
|
|
|
}
|
|
|
|
isFirst = false
|
|
|
|
|
|
|
|
qid := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, id)
|
2018-11-16 22:28:17 +00:00
|
|
|
attrs := formatDeviceStats(qid, dinst.Stats)
|
2018-11-15 19:44:13 +00:00
|
|
|
|
|
|
|
ui.Output(formatKV(attrs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|