Skip to content

Commit 133d3cb

Browse files
lgfa29schmichael
authored andcommitted
cli: include all possible scores in alloc status metric table (#11128)
1 parent 7d05e34 commit 133d3cb

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

.changelog/11128.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
cli: Display all possible scores in the allocation status table
3+
```

command/monitor.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -330,33 +330,32 @@ func formatAllocMetrics(metrics *api.AllocationMetric, scores bool, prefix strin
330330
if scores {
331331
if len(metrics.ScoreMetaData) > 0 {
332332
scoreOutput := make([]string, len(metrics.ScoreMetaData)+1)
333-
var scorerNames []string
334-
for i, scoreMeta := range metrics.ScoreMetaData {
335-
// Add header as first row
336-
if i == 0 {
337-
scoreOutput[0] = "Node|"
338-
339-
// sort scores alphabetically
340-
scores := make([]string, 0, len(scoreMeta.Scores))
341-
for score := range scoreMeta.Scores {
342-
scores = append(scores, score)
343-
}
344-
sort.Strings(scores)
345333

346-
// build score header output
347-
for _, scorerName := range scores {
348-
scoreOutput[0] += fmt.Sprintf("%v|", scorerName)
349-
scorerNames = append(scorerNames, scorerName)
350-
}
351-
scoreOutput[0] += "final score"
334+
// Find all possible scores and build header row.
335+
allScores := make(map[string]struct{})
336+
for _, scoreMeta := range metrics.ScoreMetaData {
337+
for score := range scoreMeta.Scores {
338+
allScores[score] = struct{}{}
352339
}
340+
}
341+
// Sort scores alphabetically.
342+
scores := make([]string, 0, len(allScores))
343+
for score := range allScores {
344+
scores = append(scores, score)
345+
}
346+
sort.Strings(scores)
347+
scoreOutput[0] = fmt.Sprintf("Node|%s|final score", strings.Join(scores, "|"))
348+
349+
// Build row for each score.
350+
for i, scoreMeta := range metrics.ScoreMetaData {
353351
scoreOutput[i+1] = fmt.Sprintf("%v|", scoreMeta.NodeID)
354-
for _, scorerName := range scorerNames {
352+
for _, scorerName := range scores {
355353
scoreVal := scoreMeta.Scores[scorerName]
356354
scoreOutput[i+1] += fmt.Sprintf("%.3g|", scoreVal)
357355
}
358356
scoreOutput[i+1] += fmt.Sprintf("%.3g", scoreMeta.NormScore)
359357
}
358+
360359
out += formatList(scoreOutput)
361360
} else {
362361
// Backwards compatibility for old allocs

command/monitor_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/hashicorp/nomad/api"
89
"github.com/hashicorp/nomad/nomad/structs"
910
"github.com/mitchellh/cli"
11+
"github.com/stretchr/testify/require"
1012
)
1113

1214
func TestMonitor_Update_Eval(t *testing.T) {
@@ -216,3 +218,56 @@ func TestMonitor_Monitor(t *testing.T) {
216218
t.Fatalf("missing final status\n\n%s", out)
217219
}
218220
}
221+
222+
func TestMonitor_formatAllocMetric(t *testing.T) {
223+
tests := []struct {
224+
Name string
225+
Metrics *api.AllocationMetric
226+
Expected string
227+
}{
228+
{
229+
Name: "display all possible scores",
230+
Metrics: &api.AllocationMetric{
231+
NodesEvaluated: 3,
232+
ScoreMetaData: []*api.NodeScoreMeta{
233+
{
234+
NodeID: "node-1",
235+
Scores: map[string]float64{
236+
"score-1": 1,
237+
"score-2": 2,
238+
},
239+
NormScore: 1,
240+
},
241+
{
242+
NodeID: "node-2",
243+
Scores: map[string]float64{
244+
"score-1": 1,
245+
"score-3": 3,
246+
},
247+
NormScore: 2,
248+
},
249+
{
250+
NodeID: "node-3",
251+
Scores: map[string]float64{
252+
"score-4": 4,
253+
},
254+
NormScore: 3,
255+
},
256+
},
257+
},
258+
Expected: `
259+
Node score-1 score-2 score-3 score-4 final score
260+
node-1 1 2 0 0 1
261+
node-2 1 0 3 0 2
262+
node-3 0 0 0 4 3
263+
`,
264+
},
265+
}
266+
267+
for _, tc := range tests {
268+
t.Run(tc.Name, func(t *testing.T) {
269+
got := formatAllocMetrics(tc.Metrics, true, "")
270+
require.Equal(t, strings.TrimSpace(tc.Expected), got)
271+
})
272+
}
273+
}

0 commit comments

Comments
 (0)