-
Notifications
You must be signed in to change notification settings - Fork 8
/
process.go
158 lines (133 loc) · 4.56 KB
/
process.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
package types
import (
"fmt"
"github.com/turbot/pipe-fittings/printers"
"github.com/turbot/pipe-fittings/sanitize"
"time"
"github.com/logrusorgru/aurora"
flowpipeapiclient "github.com/turbot/flowpipe-sdk-go"
typehelpers "github.com/turbot/go-kit/types"
)
// The definition of a single Flowpipe Process
type Process struct {
ID string `json:"execution_id"`
Pipeline string `json:"pipeline"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
}
func (p Process) String(sanitizer *sanitize.Sanitizer, opts sanitize.RenderOptions) string {
au := aurora.NewAurora(opts.ColorEnabled)
keyWidth := 14
output := ""
// deliberately shadow the receiver with a sanitized version of the struct
var err error
if p, err = sanitize.SanitizeStruct(sanitizer, p); err != nil {
return ""
}
output += fmt.Sprintf("%-*s%s\n", keyWidth, au.Blue("Execution ID:"), p.ID)
output += fmt.Sprintf("%-*s%s\n", keyWidth, au.Blue("Pipeline:"), p.Pipeline)
output += fmt.Sprintf("%-*s%s\n", keyWidth, au.Blue("Status:"), p.Status)
output += fmt.Sprintf("%-*s%s\n", keyWidth, au.Blue("Created:"), p.CreatedAt.Local().Format(time.DateTime))
return output
}
// Process log payload definition
type ProcessPayload struct {
PipelineName string `json:"name"`
PipelineExecutionID string `json:"pipeline_execution_id"`
Event ProcessPayloadEvent `json:"event"`
}
type ProcessPayloadEvent struct {
CreatedAt time.Time `json:"created_at"`
}
type ProcessOutputData struct {
ID string `json:"process_id"`
Output map[string]interface{} `json:"output"`
}
// Identical to the EventLogEntry struct in internal/types/execution.go
// Using the EventLogEntry returned an error at the time of openapi generation:
// cannot find type definition: json.RawMessage
// TODO - Recheck to use the EventLogEntry struct
type ProcessEventLog struct {
EventType string `json:"event_type"`
Timestamp *time.Time `json:"ts"`
// Setting the type as string for now, as the CLI need to print the payload
Payload string `json:"payload"`
}
type PrintableProcess struct {
Items []Process
}
func NewPrintableProcess(resp *ListProcessResponse) *PrintableProcess {
return &PrintableProcess{
Items: resp.Items,
}
}
func NewPrintableProcessFromSingle(input *Process) *PrintableProcess {
return &PrintableProcess{
Items: []Process{*input},
}
}
func (p PrintableProcess) GetItems() []Process {
return p.Items
}
func (p PrintableProcess) GetTable() (*printers.Table, error) {
var tableRows []printers.TableRow
for _, item := range p.Items {
cells := []any{
item.ID,
item.Pipeline,
item.CreatedAt.Local().Format(time.DateTime),
item.Status,
}
tableRows = append(tableRows, printers.TableRow{Cells: cells})
}
return printers.NewTable().WithData(tableRows, p.getColumns()), nil
}
func (PrintableProcess) getColumns() (columns []string) {
return []string{"EXECUTION_ID", "PIPELINE", "CREATED_AT", "STATUS"}
}
// This type is used by the API to return a list of processs.
type ListProcessResponse struct {
Items []Process `json:"items"`
NextToken *string `json:"next_token,omitempty"`
}
func ListProcessResponseFromAPIResponse(apiResp *flowpipeapiclient.ListProcessResponse) (*ListProcessResponse, error) {
if apiResp == nil {
return nil, nil
}
var res = &ListProcessResponse{
Items: make([]Process, len(apiResp.Items)),
NextToken: apiResp.NextToken,
}
for i, apiItem := range apiResp.Items {
item, err := ProcessFromAPIResponse(apiItem)
if err != nil {
return nil, err
}
res.Items[i] = *item
}
return res, nil
}
func ProcessFromAPIResponse(apiResp flowpipeapiclient.Process) (*Process, error) {
createdAt, _ := time.Parse(time.RFC3339Nano, *apiResp.CreatedAt)
res := &Process{
ID: typehelpers.SafeString(apiResp.ExecutionId),
Pipeline: typehelpers.SafeString(apiResp.Pipeline),
Status: typehelpers.SafeString(apiResp.Status),
CreatedAt: createdAt,
}
return res, nil
}
type ListProcessLogJSONResponse struct {
Items []ProcessEventLog `json:"items"`
NextToken *string `json:"next_token,omitempty"`
}
// This type is used by the API to return a list of proces logs.
type ListProcessLogResponse struct {
Items []EventLogEntry `json:"items"`
NextToken *string `json:"next_token,omitempty"`
}
type CmdProcess struct {
Command string `json:"command" binding:"required,oneof=run cancel pause resume"`
PipelineExecutionID string `json:"pipeline_execution_id,omitempty" format:"^(pexec|exec)_[0-9a-v]{20}$"`
Reason string `json:"reason,omitempty"`
}