Display StatsObject nested objects as well

This commit is contained in:
Mahmood Ali
2018-11-14 22:11:59 -05:00
parent 8274555d1c
commit 5e2cdb48d4
3 changed files with 74 additions and 3 deletions

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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))
}
}