Skip to content

Commit 99c1b43

Browse files
Add STATUS column to batch job list table output (minio#5202)
1 parent 18c1c94 commit 99c1b43

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

cmd/batch-list.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ EXAMPLES:
6666
type batchListMessage struct {
6767
Status string `json:"status"`
6868
Jobs []madmin.BatchJobResult `json:"jobs"`
69+
Admin *madmin.AdminClient `json:"-"` // Add AdminClient to fetch job status
6970
}
7071

7172
// String colorized batchList message
@@ -90,15 +91,35 @@ func (c batchListMessage) String() string {
9091
table.SetTablePadding("\t") // pad with tabs
9192
table.SetNoWhiteSpace(true)
9293

93-
table.SetHeader([]string{"ID", "TYPE", "USER", "STARTED"})
94-
data := make([][]string, 0, 4)
94+
// Add a new "STATUS" column to the table header
95+
table.SetHeader([]string{"ID", "TYPE", "USER", "STARTED", "STATUS"})
96+
data := make([][]string, 0, 5)
9597

98+
// Fetch the status for the batch job using BatchJobStatus API
9699
for _, job := range c.Jobs {
100+
jobStatus := "unknown"
101+
jobMetric, err := c.Admin.BatchJobStatus(context.Background(), job.ID)
102+
103+
// Output error if the API call fails, but continue processing remaining jobs
104+
if err != nil {
105+
println("Failed to fetch job status for Job ID:", job.ID, "Error:", err.Error())
106+
} else {
107+
if jobMetric.LastMetric.Complete && !jobMetric.LastMetric.Failed {
108+
jobStatus = "completed"
109+
} else if !jobMetric.LastMetric.Complete && !jobMetric.LastMetric.Failed {
110+
jobStatus = "in-progress"
111+
} else if jobMetric.LastMetric.Failed {
112+
jobStatus = "failed"
113+
}
114+
}
115+
116+
// Add jobStatus details to the data table
97117
data = append(data, []string{
98118
job.ID,
99119
string(job.Type),
100120
job.User,
101121
humanize.Time(job.Started),
122+
jobStatus,
102123
})
103124
}
104125

@@ -111,7 +132,43 @@ func (c batchListMessage) String() string {
111132
// JSON jsonified batchList message
112133
func (c batchListMessage) JSON() string {
113134
c.Status = "success"
114-
batchListMessageBytes, e := json.MarshalIndent(c, "", " ")
135+
136+
// Create a temporary slice to hold jobs with derived statuses
137+
jobsWithStatus := make([]map[string]interface{}, len(c.Jobs))
138+
139+
// Fetch the status for the batch job using BatchJobStatus API
140+
for i, job := range c.Jobs {
141+
jobStatus := "unknown"
142+
jobMetric, err := c.Admin.BatchJobStatus(context.Background(), job.ID)
143+
144+
// Output error if the API call fails, but continue processing remaining jobs
145+
if err != nil {
146+
println("Failed to fetch job status for Job ID:", job.ID, "Error:", err.Error())
147+
} else {
148+
if jobMetric.LastMetric.Complete && !jobMetric.LastMetric.Failed {
149+
jobStatus = "completed"
150+
} else if !jobMetric.LastMetric.Complete && !jobMetric.LastMetric.Failed {
151+
jobStatus = "in-progress"
152+
} else if jobMetric.LastMetric.Failed {
153+
jobStatus = "failed"
154+
}
155+
}
156+
157+
// Add the job details along with the derived status
158+
jobsWithStatus[i] = map[string]interface{}{
159+
"id": job.ID,
160+
"type": job.Type,
161+
"user": job.User,
162+
"started": job.Started,
163+
"status": jobStatus,
164+
}
165+
}
166+
167+
// Marshal the updated jobs into JSON
168+
batchListMessageBytes, e := json.MarshalIndent(map[string]interface{}{
169+
"status": c.Status,
170+
"jobs": jobsWithStatus,
171+
}, "", " ")
115172
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
116173

117174
return string(batchListMessageBytes)
@@ -147,6 +204,7 @@ func mainBatchList(ctx *cli.Context) error {
147204
printMsg(batchListMessage{
148205
Status: "success",
149206
Jobs: res.Jobs,
207+
Admin: adminClient, // Pass the adminClient for status lookups
150208
})
151209
return nil
152210
}

0 commit comments

Comments
 (0)