Skip to content

Commit 82af204

Browse files
Add allocation-specific jq event queries with good default (#112)
1 parent 115a586 commit 82af204

File tree

6 files changed

+82
-22
lines changed

6 files changed

+82
-22
lines changed

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,50 @@ Example yaml file showing all options (copy this into `$HOME/.wander.yaml` and u
165165
# Namespace used in stream for all events. "*" for all namespaces. Default "default"
166166
#wander_event_namespace: "default"
167167

168-
# The jq (https://stedolan.github.io/jq/) query used for parsing events. "." to show entire event JSON. Default is:
168+
# The jq (https://stedolan.github.io/jq/) query used for parsing general events. "." to show entire event JSON. Default is:
169169
# .Events[] | {
170-
# "1:Index": .Index,
171-
# "2:Topic": .Topic,
172-
# "3:Type": .Type,
173-
# "4:Name": .Payload | (.Job // .Allocation // .Deployment // .Evaluation) | (.JobID // .ID),
174-
# "5:ID": .Payload | (.Job.ID // (.Allocation // .Deployment // .Evaluation).ID[:8])
170+
# "1:Index": .Index,
171+
# "2:Topic": .Topic,
172+
# "3:Type": .Type,
173+
# "4:Name": .Payload | (.Job // .Allocation // .Deployment // .Evaluation) | (.JobID // .ID),
174+
# "5:ID": .Payload | (.Job.ID // (.Allocation // .Deployment // .Evaluation).ID[:8])
175175
# }
176176
# The numbering exists to preserve ordering, as https://github.com/itchyny/gojq does not keep the order of object keys
177177
#wander_event_jq_query: >
178178
# .Events[] | {
179-
# "1:Index": .Index,
180-
# "2:Topic": .Topic,
181-
# "3:Type": .Type,
182-
# "4:Name": .Payload | (.Job // .Allocation // .Deployment // .Evaluation) | (.JobID // .ID),
183-
# "5:ID": .Payload | (.Job.ID // (.Allocation // .Deployment // .Evaluation).ID[:8])
179+
# "1:Index": .Index,
180+
# "2:Topic": .Topic,
181+
# "3:Type": .Type,
182+
# "4:Name": .Payload | (.Job // .Allocation // .Deployment // .Evaluation) | (.JobID // .ID),
183+
# "5:ID": .Payload | (.Job.ID // (.Allocation // .Deployment // .Evaluation).ID[:8])
184+
# }
185+
186+
# The jq (https://stedolan.github.io/jq/) query used for parsing allocation-specific events. "." to show entire event JSON. Default is:
187+
# .Index as $index | .Events[] | .Type as $type | .Payload.Allocation |
188+
# .DeploymentStatus.Healthy as $healthy | .ClientStatus as $clientStatus | .Name as $allocName |
189+
# (.TaskStates // {"":{"Events": [{}]}}) | to_entries[] | .key as $k | .value.Events[] | {
190+
# "0:Index": $index,
191+
# "1:AllocName": $allocName,
192+
# "2:TaskName": $k,
193+
# "3:Type": $type,
194+
# "4:Time": ((.Time // 0) / 1000000000 | todate),
195+
# "5:Msg": .DisplayMessage,
196+
# "6:Healthy": $healthy,
197+
# "7:ClientStatus": $clientStatus
198+
# }
199+
# The numbering exists to preserve ordering, as https://github.com/itchyny/gojq does not keep the order of object keys
200+
#wander_alloc_event_jq_query: >
201+
# .Index as $index | .Events[] | .Type as $type | .Payload.Allocation |
202+
# .DeploymentStatus.Healthy as $healthy | .ClientStatus as $clientStatus | .Name as $allocName |
203+
# (.TaskStates // {"":{"Events": [{}]}}) | to_entries[] | .key as $k | .value.Events[] | {
204+
# "0:Index": $index,
205+
# "1:AllocName": $allocName,
206+
# "2:TaskName": $k,
207+
# "3:Type": $type,
208+
# "4:Time": ((.Time // 0) / 1000000000 | todate),
209+
# "5:Msg": .DisplayMessage,
210+
# "6:Healthy": $healthy,
211+
# "7:ClientStatus": $clientStatus
184212
# }
185213

186214
# For `wander serve`. Hostname of the machine hosting the ssh server. Default "localhost"

cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,14 @@ var (
134134
},
135135
"event-jq-query": {
136136
cfgFileEnvVar: "wander_event_jq_query",
137-
description: `jq query for events. "." for entire JSON`,
137+
description: `jq query for general events. "." for entire JSON`,
138138
defaultString: constants.DefaultEventJQQuery,
139139
},
140+
"alloc-event-jq-query": {
141+
cfgFileEnvVar: "wander_alloc_event_jq_query",
142+
description: `jq query for allocation-specific events. "." for entire JSON`,
143+
defaultString: constants.DefaultAllocEventJQQuery,
144+
},
140145
"logo-color": {
141146
cfgFileEnvVar: "wander_logo_color",
142147
},
@@ -227,6 +232,7 @@ func init() {
227232
"event-topics",
228233
"event-namespace",
229234
"event-jq-query",
235+
"alloc-event-jq-query",
230236
"compact-header",
231237
"start-all-tasks",
232238
"compact-tables",

cmd/util.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ func retrieveEventJQQuery(cmd *cobra.Command) *gojq.Code {
174174
return code
175175
}
176176

177+
func retrieveAllocEventJQQuery(cmd *cobra.Command) *gojq.Code {
178+
query := cmd.Flags().Lookup("alloc-event-jq-query").Value.String()
179+
parsed, err := gojq.Parse(query)
180+
if err != nil {
181+
fmt.Printf("Error parsing alloc event jq query: %s\n", err.Error())
182+
os.Exit(1)
183+
}
184+
code, err := gojq.Compile(parsed)
185+
if err != nil {
186+
fmt.Printf("Error compiling alloc event jq query: %s\n", err.Error())
187+
os.Exit(1)
188+
}
189+
return code
190+
}
191+
177192
func retrieveUpdateSeconds(cmd *cobra.Command) int {
178193
updateSecondsString := cmd.Flags().Lookup("update").Value.String()
179194
updateSeconds, err := strconv.Atoi(updateSecondsString)
@@ -295,6 +310,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
295310
eventTopics := retrieveEventTopics(cmd)
296311
eventNamespace := retrieveEventNamespace(cmd)
297312
eventJQQuery := retrieveEventJQQuery(cmd)
313+
allocEventJQQuery := retrieveAllocEventJQQuery(cmd)
298314
updateSeconds := retrieveUpdateSeconds(cmd)
299315
jobColumns := retrieveJobColumns(cmd)
300316
allTaskColumns := retrieveAllTaskColumns(cmd)
@@ -327,9 +343,10 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
327343
},
328344
CopySavePath: copySavePath,
329345
Event: app.EventConfig{
330-
Topics: eventTopics,
331-
Namespace: eventNamespace,
332-
JQQuery: eventJQQuery,
346+
Topics: eventTopics,
347+
Namespace: eventNamespace,
348+
JQQuery: eventJQQuery,
349+
AllocJQQuery: allocEventJQQuery,
333350
},
334351
UpdateSeconds: time.Second * time.Duration(updateSeconds),
335352
JobColumns: jobColumns,

img/wander.gif

217 KB
Loading

internal/tui/components/app/app.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ type TLSConfig struct {
2727
}
2828

2929
type EventConfig struct {
30-
Topics nomad.Topics
31-
Namespace string
32-
JQQuery *gojq.Code
30+
Topics nomad.Topics
31+
Namespace string
32+
JQQuery *gojq.Code
33+
AllocJQQuery *gojq.Code
3334
}
3435

3536
type LogConfig struct {
@@ -192,9 +193,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
192193
}
193194

194195
switch m.currentPage {
195-
case nomad.JobEventsPage, nomad.AllocEventsPage, nomad.AllEventsPage:
196+
case nomad.JobEventsPage, nomad.AllEventsPage:
196197
m.eventsStream = msg.EventsStream
197198
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.JQQuery))
199+
case nomad.AllocEventsPage:
200+
m.eventsStream = msg.EventsStream
201+
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.AllocJQQuery))
198202
case nomad.LogsPage:
199203
m.getCurrentPageModel().SetViewportSelectionToBottom()
200204
if m.config.Log.Tail {
@@ -223,7 +227,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
223227
m.getCurrentPageModel().ScrollViewportToBottom()
224228
}
225229
}
226-
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.JQQuery))
230+
query := m.config.Event.JQQuery
231+
if m.currentPage == nomad.AllocEventsPage {
232+
query = m.config.Event.AllocJQQuery
233+
}
234+
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, query))
227235
}
228236

229237
case nomad.LogsStreamMsg:

internal/tui/constants/constants.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"time"
88
)
99

10-
const NoVersionString = "built from source"
11-
1210
var LogoString = strings.Join([]string{
1311
"█ █ █ █▀█ █▄ █ █▀▄ █▀▀ █▀█",
1412
"▀▄▀▄▀ █▀█ █ ▀█ █▄▀ ██▄ █▀▄",
@@ -37,3 +35,6 @@ const DefaultPageInput = "/bin/sh"
3735

3836
// DefaultEventJQQuery is a single line as this shows up verbatim in `wander --help`
3937
const DefaultEventJQQuery = `.Events[] | {"1:Index": .Index, "2:Topic": .Topic, "3:Type": .Type, "4:Name": .Payload | (.Job // .Allocation // .Deployment // .Evaluation) | (.JobID // .ID), "5:ID": .Payload | (.Job.ID // (.Allocation // .Deployment // .Evaluation).ID[:8])}`
38+
39+
// DefaultAllocEventJQQuery is a single line as this shows up verbatim in `wander --help`
40+
const DefaultAllocEventJQQuery = `.Index as $index | .Events[] | .Type as $type | .Payload.Allocation | .DeploymentStatus.Healthy as $healthy | .ClientStatus as $clientStatus | .Name as $allocName | (.TaskStates // {"":{"Events": [{}]}}) | to_entries[] | .key as $k | .value.Events[] | {"0:Index": $index, "1:AllocName": $allocName, "2:TaskName": $k, "3:Type": $type, "4:Time": ((.Time // 0) / 1000000000 | todate), "5:Msg": .DisplayMessage, "6:Healthy": $healthy, "7:ClientStatus": $clientStatus}`

0 commit comments

Comments
 (0)