Skip to content

Commit

Permalink
Add allocation-specific jq event queries with good default (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 authored Nov 29, 2023
1 parent 115a586 commit 82af204
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
50 changes: 39 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,50 @@ Example yaml file showing all options (copy this into `$HOME/.wander.yaml` and u
# Namespace used in stream for all events. "*" for all namespaces. Default "default"
#wander_event_namespace: "default"

# The jq (https://stedolan.github.io/jq/) query used for parsing events. "." to show entire event JSON. Default is:
# The jq (https://stedolan.github.io/jq/) query used for parsing general events. "." to show entire event JSON. Default is:
# .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])
# "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])
# }
# The numbering exists to preserve ordering, as https://github.com/itchyny/gojq does not keep the order of object keys
#wander_event_jq_query: >
# .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])
# "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])
# }

# The jq (https://stedolan.github.io/jq/) query used for parsing allocation-specific events. "." to show entire event JSON. Default is:
# .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
# }
# The numbering exists to preserve ordering, as https://github.com/itchyny/gojq does not keep the order of object keys
#wander_alloc_event_jq_query: >
# .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
# }

# For `wander serve`. Hostname of the machine hosting the ssh server. Default "localhost"
Expand Down
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ var (
},
"event-jq-query": {
cfgFileEnvVar: "wander_event_jq_query",
description: `jq query for events. "." for entire JSON`,
description: `jq query for general events. "." for entire JSON`,
defaultString: constants.DefaultEventJQQuery,
},
"alloc-event-jq-query": {
cfgFileEnvVar: "wander_alloc_event_jq_query",
description: `jq query for allocation-specific events. "." for entire JSON`,
defaultString: constants.DefaultAllocEventJQQuery,
},
"logo-color": {
cfgFileEnvVar: "wander_logo_color",
},
Expand Down Expand Up @@ -227,6 +232,7 @@ func init() {
"event-topics",
"event-namespace",
"event-jq-query",
"alloc-event-jq-query",
"compact-header",
"start-all-tasks",
"compact-tables",
Expand Down
23 changes: 20 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ func retrieveEventJQQuery(cmd *cobra.Command) *gojq.Code {
return code
}

func retrieveAllocEventJQQuery(cmd *cobra.Command) *gojq.Code {
query := cmd.Flags().Lookup("alloc-event-jq-query").Value.String()
parsed, err := gojq.Parse(query)
if err != nil {
fmt.Printf("Error parsing alloc event jq query: %s\n", err.Error())
os.Exit(1)
}
code, err := gojq.Compile(parsed)
if err != nil {
fmt.Printf("Error compiling alloc event jq query: %s\n", err.Error())
os.Exit(1)
}
return code
}

func retrieveUpdateSeconds(cmd *cobra.Command) int {
updateSecondsString := cmd.Flags().Lookup("update").Value.String()
updateSeconds, err := strconv.Atoi(updateSecondsString)
Expand Down Expand Up @@ -295,6 +310,7 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
eventTopics := retrieveEventTopics(cmd)
eventNamespace := retrieveEventNamespace(cmd)
eventJQQuery := retrieveEventJQQuery(cmd)
allocEventJQQuery := retrieveAllocEventJQQuery(cmd)
updateSeconds := retrieveUpdateSeconds(cmd)
jobColumns := retrieveJobColumns(cmd)
allTaskColumns := retrieveAllTaskColumns(cmd)
Expand Down Expand Up @@ -327,9 +343,10 @@ func setup(cmd *cobra.Command, overrideToken string) (app.Model, []tea.ProgramOp
},
CopySavePath: copySavePath,
Event: app.EventConfig{
Topics: eventTopics,
Namespace: eventNamespace,
JQQuery: eventJQQuery,
Topics: eventTopics,
Namespace: eventNamespace,
JQQuery: eventJQQuery,
AllocJQQuery: allocEventJQQuery,
},
UpdateSeconds: time.Second * time.Duration(updateSeconds),
JobColumns: jobColumns,
Expand Down
Binary file modified img/wander.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 13 additions & 5 deletions internal/tui/components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type TLSConfig struct {
}

type EventConfig struct {
Topics nomad.Topics
Namespace string
JQQuery *gojq.Code
Topics nomad.Topics
Namespace string
JQQuery *gojq.Code
AllocJQQuery *gojq.Code
}

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

switch m.currentPage {
case nomad.JobEventsPage, nomad.AllocEventsPage, nomad.AllEventsPage:
case nomad.JobEventsPage, nomad.AllEventsPage:
m.eventsStream = msg.EventsStream
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.JQQuery))
case nomad.AllocEventsPage:
m.eventsStream = msg.EventsStream
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.AllocJQQuery))
case nomad.LogsPage:
m.getCurrentPageModel().SetViewportSelectionToBottom()
if m.config.Log.Tail {
Expand Down Expand Up @@ -223,7 +227,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.getCurrentPageModel().ScrollViewportToBottom()
}
}
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, m.config.Event.JQQuery))
query := m.config.Event.JQQuery
if m.currentPage == nomad.AllocEventsPage {
query = m.config.Event.AllocJQQuery
}
cmds = append(cmds, nomad.ReadEventsStreamNextMessage(m.eventsStream, query))
}

case nomad.LogsStreamMsg:
Expand Down
5 changes: 3 additions & 2 deletions internal/tui/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"time"
)

const NoVersionString = "built from source"

var LogoString = strings.Join([]string{
"█ █ █ █▀█ █▄ █ █▀▄ █▀▀ █▀█",
"▀▄▀▄▀ █▀█ █ ▀█ █▄▀ ██▄ █▀▄",
Expand Down Expand Up @@ -37,3 +35,6 @@ const DefaultPageInput = "/bin/sh"

// DefaultEventJQQuery is a single line as this shows up verbatim in `wander --help`
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])}`

// DefaultAllocEventJQQuery is a single line as this shows up verbatim in `wander --help`
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 comments on commit 82af204

Please sign in to comment.