-
Notifications
You must be signed in to change notification settings - Fork 85
/
history.go
147 lines (132 loc) · 4.55 KB
/
history.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
package job
import (
"fmt"
"strconv"
"time"
"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/models"
"github.com/bacalhau-project/bacalhau/pkg/publicapi/apimodels"
"github.com/bacalhau-project/bacalhau/pkg/util/idgen"
"github.com/bacalhau-project/bacalhau/pkg/util/templates"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n"
)
var (
historyShort = `List history events for a job by id.`
historyLong = templates.LongDesc(i18n.T(`
List job history events for a job by id.
`))
historyExample = templates.Examples(i18n.T(`
# All events for a given job.
bacalhau job history e3f8c209-d683-4a41-b840-f09b88d087b9
# Job level events
bacalhau job history --type job e3f8c209
# Execution level events
bacalhau job history --type execution e3f8c209
`))
)
// HistoryOptions is a struct to support node command
type HistoryOptions struct {
output.OutputOptions
cliflags.ListOptions
EventType string
ExecutionID string
NodeID string
}
// NewHistoryOptions returns initialized Options
func NewHistoryOptions() *HistoryOptions {
return &HistoryOptions{
OutputOptions: output.OutputOptions{Format: output.TableFormat},
EventType: "all",
}
}
func NewHistoryCmd() *cobra.Command {
o := NewHistoryOptions()
nodeCmd := &cobra.Command{
Use: "history [id]",
Short: historyShort,
Long: historyLong,
Example: historyExample,
Args: cobra.ExactArgs(1),
Run: o.run,
}
nodeCmd.Flags().StringVar(&o.EventType, "event-type", o.EventType,
"The type of history events to return. One of: all, job, execution")
nodeCmd.Flags().StringVar(&o.ExecutionID, "execution-id", o.ExecutionID,
"The execution id to filter by.")
nodeCmd.Flags().StringVar(&o.NodeID, "node-id", o.NodeID,
"The node id to filter by.")
nodeCmd.Flags().AddFlagSet(cliflags.ListFlags(&o.ListOptions))
nodeCmd.Flags().AddFlagSet(cliflags.OutputFormatFlags(&o.OutputOptions))
return nodeCmd
}
var historyColumns = []output.TableColumn[*models.JobHistory]{
{
ColumnConfig: table.ColumnConfig{Name: "Time", WidthMax: 8, WidthMaxEnforcer: output.ShortenTime},
Value: func(j *models.JobHistory) string { return j.Time.Format(time.DateTime) },
},
{
ColumnConfig: table.ColumnConfig{Name: "Level", WidthMax: 15, WidthMaxEnforcer: text.WrapText},
Value: func(jwi *models.JobHistory) string { return jwi.Type.String() },
},
{
ColumnConfig: table.ColumnConfig{Name: "Exec. ID", WidthMax: 10, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string { return idgen.ShortID(j.ExecutionID) },
},
{
ColumnConfig: table.ColumnConfig{Name: "Node ID", WidthMax: 10, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string { return idgen.ShortID(j.NodeID) },
},
{
ColumnConfig: table.ColumnConfig{Name: "Rev.", WidthMax: 4, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string { return strconv.FormatUint(j.NewRevision, 10) },
},
{
ColumnConfig: table.ColumnConfig{Name: "Previous State", WidthMax: 20, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string {
if j.Type == models.JobHistoryTypeJobLevel {
return j.JobState.Previous.String()
}
return j.ExecutionState.Previous.String()
},
},
{
ColumnConfig: table.ColumnConfig{Name: "New State", WidthMax: 20, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string {
if j.Type == models.JobHistoryTypeJobLevel {
return j.JobState.New.String()
}
return j.ExecutionState.New.String()
},
},
{
ColumnConfig: table.ColumnConfig{Name: "Comment", WidthMax: 40, WidthMaxEnforcer: text.WrapText},
Value: func(j *models.JobHistory) string { return j.Comment },
},
}
func (o *HistoryOptions) run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
jobID := args[0]
response, err := util.GetAPIClientV2(ctx).Jobs().History(&apimodels.ListJobHistoryRequest{
JobID: jobID,
EventType: o.EventType,
ExecutionID: o.ExecutionID,
NodeID: o.NodeID,
BaseListRequest: apimodels.BaseListRequest{
Limit: o.Limit,
NextToken: o.NextToken,
OrderBy: o.OrderBy,
Reverse: o.Reverse,
},
})
if err != nil {
util.Fatal(cmd, err, 1)
}
if err = output.Output(cmd, historyColumns, o.OutputOptions, response.History); err != nil {
util.Fatal(cmd, fmt.Errorf("failed to output: %w", err), 1)
}
}