-
Notifications
You must be signed in to change notification settings - Fork 85
/
describe.go
186 lines (165 loc) · 5.46 KB
/
describe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package job
import (
"fmt"
"time"
"github.com/bacalhau-project/bacalhau/pkg/lib/collections"
"github.com/bacalhau-project/bacalhau/pkg/models"
"github.com/bacalhau-project/bacalhau/pkg/util/idgen"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"
"github.com/bacalhau-project/bacalhau/cmd/util"
"github.com/bacalhau-project/bacalhau/cmd/util/flags/cliflags"
"github.com/bacalhau-project/bacalhau/cmd/util/output"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels"
"github.com/bacalhau-project/bacalhau/pkg/util/templates"
)
var (
describeLong = templates.LongDesc(i18n.T(`
Full description of a job, in yaml format.
Use 'bacalhau job list' to get a list of jobs.
`))
describeExample = templates.Examples(i18n.T(`
# Describe a job with the full ID
bacalhau job describe j-e3f8c209-d683-4a41-b840-f09b88d087b9
# Describe a job with the a shortened ID
bacalhau job describe j-47805f5c
# Describe a job with json output
bacalhau job describe --output json --pretty j-b6ad164a
`))
)
// DescribeOptions is a struct to support job command
type DescribeOptions struct {
OutputOpts output.NonTabularOutputOptions
}
// NewDescribeOptions returns initialized Options
func NewDescribeOptions() *DescribeOptions {
return &DescribeOptions{
OutputOpts: output.NonTabularOutputOptions{},
}
}
func NewDescribeCmd() *cobra.Command {
o := NewDescribeOptions()
jobCmd := &cobra.Command{
Use: "describe [id]",
Short: "Get the info of a job by id.",
Long: describeLong,
Example: describeExample,
Args: cobra.ExactArgs(1),
Run: o.run,
}
jobCmd.Flags().AddFlagSet(cliflags.OutputNonTabularFormatFlags(&o.OutputOpts))
return jobCmd
}
func (o *DescribeOptions) run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
jobID := args[0]
response, err := util.GetAPIClientV2(cmd).Jobs().Get(ctx, &apimodels.GetJobRequest{
JobID: jobID,
Include: "executions",
})
if err != nil {
util.Fatal(cmd, fmt.Errorf("could not get job %s: %w", jobID, err), 1)
}
if o.OutputOpts.Format != "" {
if err = output.OutputOneNonTabular(cmd, o.OutputOpts, response); err != nil {
util.Fatal(cmd, fmt.Errorf("failed to write job %s: %w", jobID, err), 1)
}
return
}
job := response.Job
var executions []*models.Execution
if response.Executions != nil {
// TODO: #520 rename Executions.Executions to Executions.Items
executions = response.Executions.Executions
}
o.printHeaderData(cmd, job)
o.printExecutionsSummary(cmd, executions)
if err = o.printExecutions(cmd, executions); err != nil {
util.Fatal(cmd, fmt.Errorf("failed to write job executions %s: %w", jobID, err), 1)
}
o.printOutputs(cmd, executions)
}
func (o *DescribeOptions) printHeaderData(cmd *cobra.Command, job *models.Job) {
var headerData = []collections.Pair[string, any]{
{Left: "ID", Right: job.ID},
{Left: "Name", Right: job.Name},
{Left: "Namespace", Right: job.Namespace},
{Left: "Type", Right: job.Type},
{Left: "State", Right: job.State.StateType},
{Left: "Message", Right: job.State.Message},
} // Job type specific data
if job.Type == models.JobTypeBatch || job.Type == models.JobTypeService {
headerData = append(headerData, collections.NewPair[string, any]("Count", job.Count))
}
// Additional data
headerData = append(headerData, []collections.Pair[string, any]{
{Left: "Created Time", Right: job.GetCreateTime().Format(time.DateTime)},
{Left: "Modified Time", Right: job.GetModifyTime().Format(time.DateTime)},
{Left: "Version", Right: job.Version},
}...)
output.KeyValue(cmd, headerData)
}
func (o *DescribeOptions) printExecutionsSummary(cmd *cobra.Command, executions []*models.Execution) {
// Summary of executions
var summaryPairs []collections.Pair[string, any]
summaryMap := map[models.ExecutionStateType]uint{}
for _, e := range executions {
summaryMap[e.ComputeState.StateType]++
}
for typ := models.ExecutionStateNew; typ < models.ExecutionStateCancelled; typ++ {
if summaryMap[typ] > 0 {
summaryPairs = append(summaryPairs, collections.NewPair[string, any](typ.String(), summaryMap[typ]))
}
}
output.Bold(cmd, "\nSummary\n")
output.KeyValue(cmd, summaryPairs)
}
func (o *DescribeOptions) printExecutions(cmd *cobra.Command, executions []*models.Execution) error {
// Executions table
tableOptions := output.OutputOptions{
Format: output.TableFormat,
NoStyle: true,
}
executionCols := []output.TableColumn[*models.Execution]{
executionColumnID,
executionColumnNodeID,
executionColumnState,
executionColumnDesired,
executionColumnRev,
executionColumnCreatedSince,
executionColumnModifiedSince,
executionColumnComment,
}
output.Bold(cmd, "\nExecutions\n")
return output.Output(cmd, executionCols, tableOptions, executions)
}
func (o *DescribeOptions) printOutputs(cmd *cobra.Command, executions []*models.Execution) {
outputs := make(map[string]string)
for _, e := range executions {
if e.RunOutput != nil {
separator := ""
if e.RunOutput.STDOUT != "" {
outputs[e.ID] = e.RunOutput.STDOUT
separator = "\n"
}
if e.RunOutput.STDERR != "" {
outputs[e.ID] += separator + e.RunOutput.STDERR
}
if e.RunOutput.StdoutTruncated || e.RunOutput.StderrTruncated {
outputs[e.ID] += "\n...\nOutput truncated"
}
}
}
if len(outputs) > 0 {
output.Bold(cmd, "\nStandard Output\n")
separator := ""
for id, out := range outputs {
if len(outputs) == 1 {
cmd.Print(out)
} else {
cmd.Printf("%sExecution %s:\n%s", separator, idgen.ShortUUID(id), out)
}
separator = "\n"
}
}
}