mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
Display StatsObject nested objects as well
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user