Skip to content

Commit 56ede87

Browse files
lgfa29schmichael
authored andcommitted
cli: include all possible scores in alloc status metric table (#11128)
1 parent e46a0e9 commit 56ede87

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
@@ -355,33 +355,32 @@ func formatAllocMetrics(metrics *api.AllocationMetric, scores bool, prefix strin
355355
if scores {
356356
if len(metrics.ScoreMetaData) > 0 {
357357
scoreOutput := make([]string, len(metrics.ScoreMetaData)+1)
358-
var scorerNames []string
359-
for i, scoreMeta := range metrics.ScoreMetaData {
360-
// Add header as first row
361-
if i == 0 {
362-
scoreOutput[0] = "Node|"
363-
364-
// sort scores alphabetically
365-
scores := make([]string, 0, len(scoreMeta.Scores))
366-
for score := range scoreMeta.Scores {
367-
scores = append(scores, score)
368-
}
369-
sort.Strings(scores)
370358

371-
// build score header output
372-
for _, scorerName := range scores {
373-
scoreOutput[0] += fmt.Sprintf("%v|", scorerName)
374-
scorerNames = append(scorerNames, scorerName)
375-
}
376-
scoreOutput[0] += "final score"
359+
// Find all possible scores and build header row.
360+
allScores := make(map[string]struct{})
361+
for _, scoreMeta := range metrics.ScoreMetaData {
362+
for score := range scoreMeta.Scores {
363+
allScores[score] = struct{}{}
377364
}
365+
}
366+
// Sort scores alphabetically.
367+
scores := make([]string, 0, len(allScores))
368+
for score := range allScores {
369+
scores = append(scores, score)
370+
}
371+
sort.Strings(scores)
372+
scoreOutput[0] = fmt.Sprintf("Node|%s|final score", strings.Join(scores, "|"))
373+
374+
// Build row for each score.
375+
for i, scoreMeta := range metrics.ScoreMetaData {
378376
scoreOutput[i+1] = fmt.Sprintf("%v|", scoreMeta.NodeID)
379-
for _, scorerName := range scorerNames {
377+
for _, scorerName := range scores {
380378
scoreVal := scoreMeta.Scores[scorerName]
381379
scoreOutput[i+1] += fmt.Sprintf("%.3g|", scoreVal)
382380
}
383381
scoreOutput[i+1] += fmt.Sprintf("%.3g", scoreMeta.NormScore)
384382
}
383+
385384
out += formatList(scoreOutput)
386385
} else {
387386
// 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)