-
Notifications
You must be signed in to change notification settings - Fork 16
/
jobs.go
104 lines (92 loc) · 3.17 KB
/
jobs.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
package nomad
import (
"errors"
tea "github.com/charmbracelet/bubbletea"
"github.com/hashicorp/nomad/api"
"github.com/robinovitch61/wander/internal/tui/components/page"
"github.com/robinovitch61/wander/internal/tui/formatter"
"github.com/robinovitch61/wander/internal/tui/message"
"sort"
"strconv"
"strings"
)
func FetchJobs(client api.Client, columns []string) tea.Cmd {
return func() tea.Msg {
jobListOpts := &api.JobListOptions{
Fields: &api.JobListFields{Meta: true},
}
jobResults, _, err := client.Jobs().ListOptions(jobListOpts, nil)
if err != nil {
if strings.Contains(err.Error(), "UUID must be 36 characters") {
return message.ErrMsg{Err: errors.New("token must be 36 characters")}
} else if strings.Contains(err.Error(), "ACL token not found") {
return message.ErrMsg{Err: errors.New("token not authorized to list jobs")}
}
return message.ErrMsg{Err: err}
}
sort.Slice(jobResults, func(x, y int) bool {
firstJob := jobResults[x]
secondJob := jobResults[y]
if firstJob.Name == secondJob.Name {
return firstJob.Namespace < secondJob.Namespace
}
return jobResults[x].Name < jobResults[y].Name
})
tableHeader, allPageData := jobResponsesAsTable(jobResults, columns)
return PageLoadedMsg{Page: JobsPage, TableHeader: tableHeader, AllPageRows: allPageData}
}
}
func getCount(row *api.JobListStub) string {
num, denom := 0, 0
for _, v := range row.JobSummary.Summary {
num += v.Running
denom += v.Running + v.Starting + v.Queued
}
return strconv.Itoa(num) + "/" + strconv.Itoa(denom)
}
func getJobRowFromColumns(row *api.JobListStub, columns []string) []string {
knownColMap := map[string]string{
"Job": row.ID,
"Type": row.Type,
"Namespace": row.Namespace,
"Priority": strconv.Itoa(row.Priority),
"Status": row.Status,
"Count": getCount(row),
"Submitted": formatter.FormatTimeNs(row.SubmitTime),
"Since Submit": getUptime(row.Status, row.SubmitTime),
}
var rowEntries []string
for _, col := range columns {
// potential conflict here between "known job columns" and meta columns,
// e.g. if meta key is "Type", it will be overwritten by the job type
if v, exists := knownColMap[col]; exists {
rowEntries = append(rowEntries, v)
} else if m, inMeta := row.Meta[col]; inMeta {
rowEntries = append(rowEntries, m)
} else {
rowEntries = append(rowEntries, "-")
}
}
return rowEntries
}
func jobResponsesAsTable(jobResponse []*api.JobListStub, columns []string) ([]string, []page.Row) {
var jobResponseRows [][]string
var keys []string
for _, row := range jobResponse {
jobResponseRows = append(jobResponseRows, getJobRowFromColumns(row, columns))
keys = append(keys, toJobsKey(row))
}
table := formatter.GetRenderedTableAsString(columns, jobResponseRows)
var rows []page.Row
for idx, row := range table.ContentRows {
rows = append(rows, page.Row{Key: keys[idx], Row: row})
}
return table.HeaderRows, rows
}
func toJobsKey(jobResponseEntry *api.JobListStub) string {
return jobResponseEntry.ID + " " + jobResponseEntry.Namespace
}
func JobIDAndNamespaceFromKey(key string) (string, string) {
split := strings.Split(key, " ")
return split[0], split[1]
}