forked from pachyderm/pachyderm
/
pretty.go
416 lines (381 loc) · 13.8 KB
/
pretty.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package pretty
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"text/template"
"github.com/docker/go-units"
"github.com/fatih/color"
"github.com/gogo/protobuf/types"
"github.com/pachyderm/pachyderm/src/client"
pfsclient "github.com/pachyderm/pachyderm/src/client/pfs"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
"github.com/pachyderm/pachyderm/src/server/pkg/pretty"
)
// PrintJobHeader prints a job header.
func PrintJobHeader(w io.Writer) {
// because STATE is a colorful field it has to be at the end of the line,
// otherwise the terminal escape characters will trip up the tabwriter
fmt.Fprint(w, "ID\tOUTPUT COMMIT\tSTARTED\tDURATION\tRESTART\tPROGRESS\tDL\tUL\tSTATE\t\n")
}
// PrintJobInfo pretty-prints job info.
func PrintJobInfo(w io.Writer, jobInfo *ppsclient.JobInfo) {
fmt.Fprintf(w, "%s\t", jobInfo.Job.ID)
if jobInfo.OutputCommit != nil {
fmt.Fprintf(w, "%s/%s\t", jobInfo.OutputCommit.Repo.Name, jobInfo.OutputCommit.ID)
} else if jobInfo.Pipeline != nil {
fmt.Fprintf(w, "%s/-\t", jobInfo.Pipeline.Name)
} else {
fmt.Fprintf(w, "-\t")
}
fmt.Fprintf(w, "%s\t", pretty.Ago(jobInfo.Started))
if jobInfo.Finished != nil {
fmt.Fprintf(w, "%s\t", pretty.TimeDifference(jobInfo.Started, jobInfo.Finished))
} else {
fmt.Fprintf(w, "-\t")
}
fmt.Fprintf(w, "%d\t", jobInfo.Restart)
fmt.Fprintf(w, "%d + %d / %d\t", jobInfo.DataProcessed, jobInfo.DataSkipped, jobInfo.DataTotal)
fmt.Fprintf(w, "%s\t", pretty.Size(jobInfo.Stats.DownloadBytes))
fmt.Fprintf(w, "%s\t", pretty.Size(jobInfo.Stats.UploadBytes))
fmt.Fprintf(w, "%s\t\n", jobState(jobInfo.State))
}
// PrintPipelineHeader prints a pipeline header.
func PrintPipelineHeader(w io.Writer) {
// because STATE is a colorful field it has to be at the end of the line,
// otherwise the terminal escape characters will trip up the tabwriter
fmt.Fprint(w, "NAME\tINPUT\tOUTPUT\tCREATED\tSTATE\t\n")
}
// PrintPipelineInfo pretty-prints pipeline info.
func PrintPipelineInfo(w io.Writer, pipelineInfo *ppsclient.PipelineInfo) {
fmt.Fprintf(w, "%s\t", pipelineInfo.Pipeline.Name)
fmt.Fprintf(w, "%s\t", ShorthandInput(pipelineInfo.Input))
fmt.Fprintf(w, "%s/%s\t", pipelineInfo.Pipeline.Name, pipelineInfo.OutputBranch)
fmt.Fprintf(w, "%s\t", pretty.Ago(pipelineInfo.CreatedAt))
fmt.Fprintf(w, "%s\t\n", pipelineState(pipelineInfo.State))
}
// PrintJobInputHeader pretty prints a job input header.
func PrintJobInputHeader(w io.Writer) {
fmt.Fprint(w, "NAME\tREPO\tCOMMIT\tGLOB\tLAZY\t\n")
}
// PrintJobInput pretty-prints a job input.
func PrintJobInput(w io.Writer, jobInput *ppsclient.JobInput) {
fmt.Fprintf(w, "%s\t", jobInput.Name)
fmt.Fprintf(w, "%s\t", jobInput.Commit.Repo.Name)
fmt.Fprintf(w, "%s\t", jobInput.Commit.ID)
fmt.Fprintf(w, "%s\t", jobInput.Glob)
fmt.Fprintf(w, "%t\t\n", jobInput.Lazy)
}
// PrintWorkerStatusHeader pretty prints a worker status header.
func PrintWorkerStatusHeader(w io.Writer) {
fmt.Fprint(w, "WORKER\tJOB\tDATUM\tSTARTED\tQUEUE\t\n")
}
// PrintWorkerStatus pretty prints a worker status.
func PrintWorkerStatus(w io.Writer, workerStatus *ppsclient.WorkerStatus) {
fmt.Fprintf(w, "%s\t", workerStatus.WorkerID)
fmt.Fprintf(w, "%s\t", workerStatus.JobID)
for _, datum := range workerStatus.Data {
fmt.Fprintf(w, datum.Path)
}
fmt.Fprintf(w, "\t")
fmt.Fprintf(w, "%s\t", pretty.Ago(workerStatus.Started))
fmt.Fprintf(w, "%d\t\n", workerStatus.QueueSize)
}
// PrintPipelineInputHeader prints a pipeline input header.
func PrintPipelineInputHeader(w io.Writer) {
fmt.Fprint(w, "NAME\tREPO\tBRANCH\tGLOB\tLAZY\t\n")
}
// PrintPipelineInput pretty-prints a pipeline input.
func PrintPipelineInput(w io.Writer, pipelineInput *ppsclient.PipelineInput) {
fmt.Fprintf(w, "%s\t", pipelineInput.Name)
fmt.Fprintf(w, "%s\t", pipelineInput.Repo.Name)
fmt.Fprintf(w, "%s\t", pipelineInput.Branch)
fmt.Fprintf(w, "%s\t", pipelineInput.Glob)
fmt.Fprintf(w, "%t\t\n", pipelineInput.Lazy)
}
// PrintJobCountsHeader prints a job counts header.
func PrintJobCountsHeader(w io.Writer) {
fmt.Fprintf(w, strings.ToUpper(jobState(ppsclient.JobState_JOB_STARTING))+"\t")
fmt.Fprintf(w, strings.ToUpper(jobState(ppsclient.JobState_JOB_RUNNING))+"\t")
fmt.Fprintf(w, strings.ToUpper(jobState(ppsclient.JobState_JOB_FAILURE))+"\t")
fmt.Fprintf(w, strings.ToUpper(jobState(ppsclient.JobState_JOB_SUCCESS))+"\t\n")
}
// PrintDetailedJobInfo pretty-prints detailed job info.
func PrintDetailedJobInfo(jobInfo *ppsclient.JobInfo) error {
template, err := template.New("JobInfo").Funcs(funcMap).Parse(
`ID: {{.Job.ID}} {{if .Pipeline}}
Pipeline: {{.Pipeline.Name}} {{end}} {{if .ParentJob}}
Parent: {{.ParentJob.ID}} {{end}}
Started: {{prettyAgo .Started}} {{if .Finished}}
Duration: {{prettyTimeDifference .Started .Finished}} {{end}}
State: {{jobState .State}}
Reason: {{.Reason}}
Processed: {{.DataProcessed}}
Failed: {{.DataFailed}}
Skipped: {{.DataSkipped}}
Total: {{.DataTotal}}
Data Downloaded: {{prettySize .Stats.DownloadBytes}}
Data Uploaded: {{prettySize .Stats.UploadBytes}}
Download Time: {{prettyDuration .Stats.DownloadTime}}
Process Time: {{prettyDuration .Stats.ProcessTime}}
Upload Time: {{prettyDuration .Stats.UploadTime}}
Datum Timeout: {{.DatumTimeout}}
Job Timeout: {{.JobTimeout}}
Worker Status:
{{workerStatus .}}Restarts: {{.Restart}}
ParallelismSpec: {{.ParallelismSpec}}
{{ if .ResourceRequests }}ResourceRequests:
CPU: {{ .ResourceRequests.Cpu }}
Memory: {{ .ResourceRequests.Memory }} {{end}}
{{ if .ResourceLimits }}ResourceLimits:
CPU: {{ .ResourceLimits.Cpu }}
Memory: {{ .ResourceLimits.Memory }}
GPU: {{ .ResourceLimits.Gpu }} {{end}}
{{ if .Service }}Service:
{{ if .Service.InternalPort }}InternalPort: {{ .Service.InternalPort }} {{end}}
{{ if .Service.ExternalPort }}ExternalPort: {{ .Service.ExternalPort }} {{end}} {{end}}Input:
{{jobInput .}}
Transform:
{{prettyTransform .Transform}} {{if .OutputCommit}}
Output Commit: {{.OutputCommit.ID}} {{end}} {{ if .StatsCommit }}
Stats Commit: {{.StatsCommit.ID}} {{end}} {{ if .Egress }}
Egress: {{.Egress.URL}} {{end}}
`)
if err != nil {
return err
}
err = template.Execute(os.Stdout, jobInfo)
if err != nil {
return err
}
return nil
}
// PrintDetailedPipelineInfo pretty-prints detailed pipeline info.
func PrintDetailedPipelineInfo(pipelineInfo *ppsclient.PipelineInfo) error {
template, err := template.New("PipelineInfo").Funcs(funcMap).Parse(
`Name: {{.Pipeline.Name}}{{if .Description}}
Description: {{.Description}}{{end}}
Created: {{prettyAgo .CreatedAt}}
State: {{pipelineState .State}}
Reason: {{.Reason}}
Parallelism Spec: {{.ParallelismSpec}}
{{ if .ResourceRequests }}ResourceRequests:
CPU: {{ .ResourceRequests.Cpu }}
Memory: {{ .ResourceRequests.Memory }} {{end}}
{{ if .ResourceLimits }}ResourceLimits:
CPU: {{ .ResourceLimits.Cpu }}
Memory: {{ .ResourceLimits.Memory }}
GPU: {{ .ResourceLimits.Gpu }} {{end}}
Datum Timeout: {{.DatumTimeout}}
Job Timeout: {{.JobTimeout}}
Input:
{{pipelineInput .}}
{{ if .GithookURL }}Githook URL: {{.GithookURL}} {{end}}
Output Branch: {{.OutputBranch}}
Transform:
{{prettyTransform .Transform}}
{{ if .Egress }}Egress: {{.Egress.URL}} {{end}}
{{if .RecentError}} Recent Error: {{.RecentError}} {{end}}
Job Counts:
{{jobCounts .JobCounts}}
`)
if err != nil {
return err
}
err = template.Execute(os.Stdout, pipelineInfo)
if err != nil {
return err
}
return nil
}
// PrintDatumInfoHeader prints a file info header.
func PrintDatumInfoHeader(w io.Writer) {
fmt.Fprint(w, "ID\tSTATUS\tTIME\t\n")
}
// PrintDatumInfo pretty-prints file info.
// If recurse is false and directory size is 0, display "-" instead
// If fast is true and file size is 0, display "-" instead
func PrintDatumInfo(w io.Writer, datumInfo *ppsclient.DatumInfo) {
totalTime := "-"
if datumInfo.Stats != nil {
totalTime = units.HumanDuration(client.GetDatumTotalTime(datumInfo.Stats))
}
fmt.Fprintf(w, "%s\t%s\t%s\n", datumInfo.Datum.ID, datumState(datumInfo.State), totalTime)
}
// PrintDetailedDatumInfo pretty-prints detailed info about a datum
func PrintDetailedDatumInfo(w io.Writer, datumInfo *ppsclient.DatumInfo) {
fmt.Fprintf(w, "ID\t%s\n", datumInfo.Datum.ID)
fmt.Fprintf(w, "Job ID\t%s\n", datumInfo.Datum.Job.ID)
fmt.Fprintf(w, "State\t%s\n", datumInfo.State)
fmt.Fprintf(w, "Data Downloaded\t%s\n", pretty.Size(datumInfo.Stats.DownloadBytes))
fmt.Fprintf(w, "Data Uploaded\t%s\n", pretty.Size(datumInfo.Stats.UploadBytes))
totalTime := client.GetDatumTotalTime(datumInfo.Stats).String()
fmt.Fprintf(w, "Total Time\t%s\n", totalTime)
var downloadTime string
dl, err := types.DurationFromProto(datumInfo.Stats.DownloadTime)
if err != nil {
downloadTime = err.Error()
}
downloadTime = dl.String()
fmt.Fprintf(w, "Download Time\t%s\n", downloadTime)
var procTime string
proc, err := types.DurationFromProto(datumInfo.Stats.ProcessTime)
if err != nil {
procTime = err.Error()
}
procTime = proc.String()
fmt.Fprintf(w, "Process Time\t%s\n", procTime)
var uploadTime string
ul, err := types.DurationFromProto(datumInfo.Stats.UploadTime)
if err != nil {
uploadTime = err.Error()
}
uploadTime = ul.String()
fmt.Fprintf(w, "Upload Time\t%s\n", uploadTime)
fmt.Fprintf(w, "PFS State:\n")
tw := tabwriter.NewWriter(w, 10, 1, 3, ' ', 0)
PrintFileHeader(tw)
PrintFile(tw, datumInfo.PfsState)
tw.Flush()
fmt.Fprintf(w, "Inputs:\n")
tw = tabwriter.NewWriter(w, 10, 1, 3, ' ', 0)
PrintFileHeader(tw)
for _, d := range datumInfo.Data {
PrintFile(tw, d.File)
}
tw.Flush()
}
// PrintFileHeader prints the header for a pfs file.
func PrintFileHeader(w io.Writer) {
fmt.Fprintf(w, " REPO\tCOMMIT\tPATH\t\n")
}
// PrintFile values for a pfs file.
func PrintFile(w io.Writer, file *pfsclient.File) {
fmt.Fprintf(w, " %s\t%s\t%s\t\n", file.Commit.Repo.Name, file.Commit.ID, file.Path)
}
func datumState(datumState ppsclient.DatumState) string {
switch datumState {
case ppsclient.DatumState_SKIPPED:
return color.New(color.FgYellow).SprintFunc()("skipped")
case ppsclient.DatumState_FAILED:
return color.New(color.FgRed).SprintFunc()("failed")
case ppsclient.DatumState_SUCCESS:
return color.New(color.FgGreen).SprintFunc()("success")
}
return "-"
}
func jobState(jobState ppsclient.JobState) string {
switch jobState {
case ppsclient.JobState_JOB_STARTING:
return color.New(color.FgYellow).SprintFunc()("starting")
case ppsclient.JobState_JOB_RUNNING:
return color.New(color.FgYellow).SprintFunc()("running")
case ppsclient.JobState_JOB_FAILURE:
return color.New(color.FgRed).SprintFunc()("failure")
case ppsclient.JobState_JOB_SUCCESS:
return color.New(color.FgGreen).SprintFunc()("success")
case ppsclient.JobState_JOB_KILLED:
return color.New(color.FgRed).SprintFunc()("killed")
}
return "-"
}
func pipelineState(pipelineState ppsclient.PipelineState) string {
switch pipelineState {
case ppsclient.PipelineState_PIPELINE_STARTING:
return color.New(color.FgYellow).SprintFunc()("starting")
case ppsclient.PipelineState_PIPELINE_RUNNING:
return color.New(color.FgGreen).SprintFunc()("running")
case ppsclient.PipelineState_PIPELINE_RESTARTING:
return color.New(color.FgYellow).SprintFunc()("restarting")
case ppsclient.PipelineState_PIPELINE_FAILURE:
return color.New(color.FgRed).SprintFunc()("failure")
case ppsclient.PipelineState_PIPELINE_PAUSED:
return color.New(color.FgYellow).SprintFunc()("paused")
}
return "-"
}
func jobInput(jobInfo *ppsclient.JobInfo) string {
if jobInfo.Input == nil {
return ""
}
input, err := json.MarshalIndent(jobInfo.Input, "", " ")
if err != nil {
panic(fmt.Errorf("error marshalling input: %+v", err))
}
return string(input) + "\n"
}
func workerStatus(jobInfo *ppsclient.JobInfo) string {
var buffer bytes.Buffer
writer := tabwriter.NewWriter(&buffer, 20, 1, 3, ' ', 0)
PrintWorkerStatusHeader(writer)
for _, workerStatus := range jobInfo.WorkerStatus {
PrintWorkerStatus(writer, workerStatus)
}
// can't error because buffer can't error on Write
writer.Flush()
return buffer.String()
}
func pipelineInput(pipelineInfo *ppsclient.PipelineInfo) string {
if pipelineInfo.Input == nil {
return ""
}
input, err := json.MarshalIndent(pipelineInfo.Input, "", " ")
if err != nil {
panic(fmt.Errorf("error marshalling input: %+v", err))
}
return string(input) + "\n"
}
func jobCounts(counts map[int32]int32) string {
var buffer bytes.Buffer
for i := int32(ppsclient.JobState_JOB_STARTING); i <= int32(ppsclient.JobState_JOB_SUCCESS); i++ {
fmt.Fprintf(&buffer, "%s: %d\t", jobState(ppsclient.JobState(i)), counts[i])
}
return buffer.String()
}
func prettyTransform(transform *ppsclient.Transform) (string, error) {
result, err := json.MarshalIndent(transform, "", " ")
if err != nil {
return "", err
}
return pretty.UnescapeHTML(string(result)), nil
}
// ShorthandInput renders a pps.Input as a short, readable string
func ShorthandInput(input *ppsclient.Input) string {
switch {
case input.Atom != nil:
return fmt.Sprintf("%s:%s", input.Atom.Repo, input.Atom.Glob)
case input.Cross != nil:
var subInput []string
for _, input := range input.Cross {
subInput = append(subInput, ShorthandInput(input))
}
return "(" + strings.Join(subInput, " ⨯ ") + ")"
case input.Union != nil:
var subInput []string
for _, input := range input.Union {
subInput = append(subInput, ShorthandInput(input))
}
return "(" + strings.Join(subInput, " ∪ ") + ")"
case input.Cron != nil:
return fmt.Sprintf("%s:%s", input.Cron.Name, input.Cron.Spec)
}
return ""
}
var funcMap = template.FuncMap{
"pipelineState": pipelineState,
"jobState": jobState,
"datumState": datumState,
"workerStatus": workerStatus,
"pipelineInput": pipelineInput,
"jobInput": jobInput,
"prettyAgo": pretty.Ago,
"prettyTimeDifference": pretty.TimeDifference,
"prettyDuration": pretty.Duration,
"prettySize": pretty.Size,
"jobCounts": jobCounts,
"prettyTransform": prettyTransform,
}