-
Notifications
You must be signed in to change notification settings - Fork 16
/
util.go
126 lines (112 loc) · 2.91 KB
/
util.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
package nomad
import (
"encoding/json"
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"
"strconv"
"strings"
"time"
)
const keySeparator = "|【=◈︿◈=】|"
type AdminAction int8
// all admin actions, task or job
// the definition order of these is important, as it's used for sorting
const (
RestartTaskAction AdminAction = iota
RestartAllocAction
StopAllocAction
RestartJobAction
StopJobAction
StopAndPurgeJobAction
)
// AdminActionToKey and KeyToAdminAction are used for admin menu serialization/deserialization
func AdminActionToKey(adminAction AdminAction) string {
switch adminAction {
case RestartTaskAction:
return "restart-task"
case RestartAllocAction:
return "restart-allocation"
case StopAllocAction:
return "stop-task"
case RestartJobAction:
return "restart-job"
case StopJobAction:
return "stop-job"
case StopAndPurgeJobAction:
return "stop-and-purge-job"
default:
return ""
}
}
func KeyToAdminAction(adminAction string) AdminAction {
switch adminAction {
case "restart-task":
return RestartTaskAction
case "restart-allocation":
return RestartAllocAction
case "stop-task":
return StopAllocAction
case "restart-job":
return RestartJobAction
case "stop-job":
return StopJobAction
case "stop-and-purge-job":
return StopAndPurgeJobAction
default:
return -1
}
}
type taskRowEntry struct {
FullAllocationAsJSON string
NodeID, JobID, ID, TaskGroup, Name, TaskName, State string
StartedAt, FinishedAt time.Time
}
func toTaskKey(state, fullAllocationAsJSON, taskName string) string {
isRunning := "false"
if state == "running" {
isRunning = "true"
}
return fullAllocationAsJSON + keySeparator + taskName + keySeparator + isRunning
}
type TaskInfo struct {
Alloc api.Allocation
TaskName string
Running bool
}
func TaskInfoFromKey(key string) (TaskInfo, error) {
split := strings.Split(key, keySeparator)
running, err := strconv.ParseBool(split[2])
if err != nil {
return TaskInfo{}, err
}
var alloc api.Allocation
err = json.Unmarshal([]byte(split[0]), &alloc)
if err != nil {
return TaskInfo{}, err
}
return TaskInfo{Alloc: alloc, TaskName: split[1], Running: running}, nil
}
func PrettifyLine(l string, p Page) tea.Cmd {
return func() tea.Msg {
// nothing async actually happens here, but this fits the PageLoadedMsg pattern
pretty := formatter.PrettyJsonStringAsLines(l)
var rows []page.Row
for _, row := range pretty {
rows = append(rows, page.Row{Key: "", Row: row})
}
return PageLoadedMsg{
Page: p,
TableHeader: []string{},
AllPageRows: rows,
}
}
}
func getUptime(status string, startTime int64) string {
uptime := "-"
if status == "running" {
uptime = formatter.FormatTimeNsSinceNow(startTime)
}
return uptime
}