diff --git a/command/helper_stats.go b/command/helper_stats.go index 916cd6118..c1eb4f74b 100644 --- a/command/helper_stats.go +++ b/command/helper_stats.go @@ -1,6 +1,8 @@ package command import ( + "fmt" + "github.com/hashicorp/nomad/api" ) @@ -28,3 +30,17 @@ func buildDeviceStatsSummaryMap(host *api.HostStats) map[string]*api.StatValue { return r } + +func formatDeviceStats(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 { + formatDeviceStats(o, keyPrefix+k, result) + } +} diff --git a/command/helper_stats_test.go b/command/helper_stats_test.go index f5e0316e8..cb984c83a 100644 --- a/command/helper_stats_test.go +++ b/command/helper_stats_test.go @@ -1,6 +1,7 @@ package command import ( + "sort" "testing" "github.com/hashicorp/nomad/api" @@ -73,3 +74,58 @@ func TestBuildDeviceStatsSummaryMap(t *testing.T) { require.EqualValues(t, expected, buildDeviceStatsSummaryMap(hostStats)) } + +func TestFormatDeviceStats(t *testing.T) { + statValue := func(v string) *api.StatValue { + return &api.StatValue{ + StringVal: helper.StringToPtr(v), + } + } + + stat := &api.StatObject{ + Attributes: map[string]*api.StatValue{ + "k0": statValue("v0"), + }, + Nested: map[string]*api.StatObject{ + "nested1": &api.StatObject{ + Attributes: map[string]*api.StatValue{ + "k1_0": statValue("v1_0"), + "k1_1": statValue("v1_1"), + }, + Nested: map[string]*api.StatObject{ + "nested1_1": &api.StatObject{ + Attributes: map[string]*api.StatValue{ + "k11_0": statValue("v11_0"), + "k11_1": statValue("v11_1"), + }, + }, + }, + }, + "nested2": &api.StatObject{ + Attributes: map[string]*api.StatValue{ + "k2": statValue("v2"), + }, + }, + }, + } + + result := []string{"preseedkey|pressededvalue"} + formatDeviceStats(stat, "", &result) + + // check array is appended only + require.Equal(t, "preseedkey|pressededvalue", result[0]) + + // check rest of values + sort.Strings(result) + expected := []string{ + "k0|v0", + "nested1.k1_0|v1_0", + "nested1.k1_1|v1_1", + "nested1.nested1_1.k11_0|v11_0", + "nested1.nested1_1.k11_1|v11_1", + "nested2.k2|v2", + "preseedkey|pressededvalue", + } + + require.Equal(t, expected, result) +} diff --git a/command/node_status.go b/command/node_status.go index 678cb86a8..79cfe1c56 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -602,9 +602,8 @@ func (c *NodeStatusCommand) printDeviceStats(hostStats *api.HostStats) { qid := deviceQualifiedID(dg.Vendor, dg.Type, dg.Name, id) attrs := make([]string, 1, len(dinst.Stats.Attributes)+1) attrs[0] = fmt.Sprintf("Device|%s", qid) - for n, stat := range dinst.Stats.Attributes { - attrs = append(attrs, fmt.Sprintf("%s|%s", n, stat)) - } + formatDeviceStats(dinst.Stats, "", &attrs) + c.Ui.Output(formatKV(attrs)) } }