-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
152 lines (137 loc) · 3.32 KB
/
list.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
package robin
import (
"encoding/json"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/unkaktus/tablewriter"
)
func roundToMinutes(d time.Duration) string {
if d == 0 || d < time.Minute {
return "0m"
}
return strings.TrimSuffix(d.Round(time.Minute).String(), "0s")
}
func showTable(jobList []Job, full bool) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetRoundedStyle()
if full {
table.SetHeader([]string{"Name", "ID", "State", "Queue", "Time", "Nodes"})
} else {
table.SetHeader([]string{"Name", "State", "Queue", "Time", "Nodes"})
}
for _, job := range jobList {
timeString := ""
if job.RequestedWalltime == time.Duration(0) {
timeString = roundToMinutes(job.Walltime)
} else {
timeProgress := job.Walltime
if !job.StartTime.IsZero() {
// Time in the future indicates that this is the scheduled time
if job.StartTime.After(time.Now()) {
// Negative time progress
timeProgress = time.Since(job.StartTime)
}
}
timePercentage := int(100 * job.Walltime.Seconds() / job.RequestedWalltime.Seconds())
timeString = fmt.Sprintf("[%d%%] %s/%s",
timePercentage,
roundToMinutes(timeProgress),
roundToMinutes(job.RequestedWalltime),
)
}
if full {
table.Append([]string{
job.Name,
job.ID,
fmt.Sprintf("%s [%d]", job.State, job.ExitCode),
job.Queue,
timeString,
strings.Join(job.Nodes, ", "),
})
} else {
table.Append([]string{
job.Name,
fmt.Sprintf("%s [%d]", job.State, job.ExitCode),
job.Queue,
timeString,
strconv.Itoa(job.NodeNumber),
})
}
}
table.Render()
return nil
}
type ListRequest struct {
All bool
Full bool
MachineReadable bool
State string
}
func ListJobs(bs BatchSystem, request ListRequest) error {
jobList, err := bs.ListJobs(request.All)
if err != nil {
return fmt.Errorf("query job list: %w", err)
}
jobMap := map[string]Job{}
for _, job := range jobList {
addedJob, ok := jobMap[job.Name]
if ok {
if job.CreationTime.Before(addedJob.CreationTime) {
jobMap[job.Name] = job
}
} else {
jobMap[job.Name] = job
}
}
jobList = []Job{}
for _, job := range jobMap {
// Skip the job with other states
if request.State != "" && job.State != request.State {
continue
}
jobList = append(jobList, job)
}
sort.Slice(jobList, func(i, j int) bool {
return jobList[i].Name < jobList[j].Name
})
switch {
case request.MachineReadable:
if err := json.NewEncoder(os.Stdout).Encode(jobList); err != nil {
return fmt.Errorf("encode list: %w", err)
}
default:
if err := showTable(jobList, request.Full); err != nil {
return fmt.Errorf("show table: %w", err)
}
}
return nil
}
func LatestJob(bs BatchSystem) (*Job, error) {
jobList, err := bs.ListJobs(false)
if err != nil {
return nil, fmt.Errorf("query job list: %w", err)
}
sort.Slice(jobList, func(i, j int) bool {
return jobList[i].CreationTime.After(jobList[j].CreationTime)
})
if len(jobList) == 0 {
return nil, fmt.Errorf("no jobs found")
}
return &jobList[0], nil
}
func findJob(b BatchSystem, jobName string) (job Job, err error) {
jobList, err := b.ListJobs(false)
if err != nil {
return job, fmt.Errorf("list jobs: %w", err)
}
for _, job = range jobList {
if job.Name == jobName {
return job, nil
}
}
return job, fmt.Errorf("job not found")
}