-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocused_mode_view.go
433 lines (388 loc) · 20.2 KB
/
focused_mode_view.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package main
import (
"encoding/json"
"sort"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
temporalEnums "go.temporal.io/api/enums/v1"
"go.temporal.io/api/history/v1"
"go.temporal.io/api/workflow/v1"
"go.temporal.io/api/workflowservice/v1"
)
type FocusedKeyMap struct {
Up key.Binding
Down key.Binding
Exit key.Binding
Back key.Binding
FocusChildWorkflow key.Binding
}
var FocusedModeKeyMap = FocusedKeyMap{
FocusChildWorkflow: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "focus on child workflow"),
),
Up: key.NewBinding(
key.WithKeys("k", "up"), // actual keybindings
key.WithHelp("↑/k", "move up"), // corresponding help text
),
Down: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("↓/j", "move down"),
),
Back: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "esc"),
),
Exit: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "exit"),
),
}
type compactHistoryStackItem struct {
workflowId string
runId string
compactHistory compactedHistory
workflowDescription *workflowservice.DescribeWorkflowExecutionResponse
}
type focusedModeState struct {
cursor int
keys FocusedKeyMap
compactedHistoryStack []compactHistoryStackItem
}
func (m *focusedModeState) getCurrentHistoryStackItem() compactHistoryStackItem {
return m.compactedHistoryStack[len(m.compactedHistoryStack)-1]
}
func (m *model) UpdateFocusedModeState(msg tea.Msg) (tea.Model, tea.Cmd) {
compactedHistory := m.focusedWorkflowState.getCurrentHistoryStackItem().compactHistory
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.focusedWorkflowState.keys.FocusChildWorkflow):
currentHistorySlice := m.focusedWorkflowState.getCurrentCompactHistorySlice()
if len(currentHistorySlice) < 2 {
return m, nil
}
currentHistoryItem := currentHistorySlice[m.focusedWorkflowState.cursor]
// The second event in the compacted history is the child workflow started event (it always comes after the initiated event)
secondHistoryEvent := currentHistoryItem.events[1]
if secondHistoryEvent.EventType == temporalEnums.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED {
executionAttributes := secondHistoryEvent.GetChildWorkflowExecutionStartedEventAttributes()
return m, m.setFocusedWorkflowCmd(executionAttributes.WorkflowExecution.GetWorkflowId(), executionAttributes.WorkflowExecution.GetRunId())
}
case key.Matches(msg, m.focusedWorkflowState.keys.Up):
if m.focusedWorkflowState.cursor > 0 {
m.focusedWorkflowState.cursor--
}
case key.Matches(msg, m.focusedWorkflowState.keys.Down):
if m.focusedWorkflowState.cursor < len(compactedHistory)-1 {
m.focusedWorkflowState.cursor++
}
case key.Matches(msg, m.focusedWorkflowState.keys.Back):
m.focusedWorkflowState.compactedHistoryStack = m.focusedWorkflowState.compactedHistoryStack[:len(m.focusedWorkflowState.compactedHistoryStack)-1]
m.focusedWorkflowState.cursor = 0
case key.Matches(msg, m.focusedWorkflowState.keys.Exit):
return m, tea.Quit
}
}
return m, nil
}
type eventContent struct {
eventType string
eventData string
}
type compactHistoryListItem struct {
events []*history.HistoryEvent
eventsContent []eventContent
icon string
actionType string
rowContent string
}
type compactedHistory map[int64]*compactHistoryListItem
var activityNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00FF")).Bold(true)
var jsonOutputStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00FF")).Bold(false)
func convertDataToPrettyJSON(data []byte) string {
var prettyJSON map[string]interface{}
json.Unmarshal(data, &prettyJSON)
prettyJSONBytes, _ := json.MarshalIndent(prettyJSON, "", " ")
return string(prettyJSONBytes)
}
func createCompactHistory(historyList []*history.HistoryEvent, pendingActivities []*workflow.PendingActivityInfo) compactedHistory {
compactedHistory := make(compactedHistory)
for _, historyEvent := range historyList {
eventType := historyEvent.GetEventType()
switch historyEvent.GetEventType() {
// Activity events
// Activity events are special because they have multiple events that are related to each other
// Activity events are grouped by the scheduled event id
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED:
eventId := historyEvent.GetEventId()
attributes := historyEvent.GetActivityTaskScheduledEventAttributes()
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
compactedHistory[eventId].actionType = "Activity"
compactedHistory[eventId].icon = "📅"
compactedHistory[eventId].rowContent = historyEvent.GetActivityTaskScheduledEventAttributes().GetActivityType().GetName()
for _, pendingActivity := range pendingActivities {
if pendingActivity.GetActivityId() == attributes.GetActivityId() {
errorCause := pendingActivity.GetLastFailure().GetCause().GetMessage()
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Last Error", eventData: errorCause})
compactedHistory[eventId].rowContent += " 🔄" + strconv.Itoa(int(pendingActivity.GetAttempt()))
break
}
}
if attributes.GetInput().GetPayloads() != nil {
prettyJSONString := convertDataToPrettyJSON(attributes.GetInput().GetPayloads()[0].GetData())
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Input", eventData: prettyJSONString})
}
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_STARTED:
activityTaskStartedEventAttributes := historyEvent.GetActivityTaskStartedEventAttributes()
eventId := activityTaskStartedEventAttributes.GetScheduledEventId()
compactedHistory[eventId].icon = "🏃"
compactedHistory[eventId].events = append(compactedHistory[activityTaskStartedEventAttributes.GetScheduledEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_COMPLETED:
activityTaskCompletedEventAttributes := historyEvent.GetActivityTaskCompletedEventAttributes()
eventId := activityTaskCompletedEventAttributes.GetScheduledEventId()
event := compactedHistory[eventId]
prettyJsonString := convertDataToPrettyJSON(activityTaskCompletedEventAttributes.GetResult().GetPayloads()[0].GetData())
event.icon = "✅"
event.events = append(event.events, historyEvent)
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Output", eventData: prettyJsonString})
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_FAILED:
activityTaskFailedEventAttributes := historyEvent.GetActivityTaskFailedEventAttributes()
eventId := activityTaskFailedEventAttributes.GetScheduledEventId()
compactedHistory[eventId].icon = "❌"
compactedHistory[activityTaskFailedEventAttributes.GetScheduledEventId()].events = append(compactedHistory[activityTaskFailedEventAttributes.GetScheduledEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT:
activityTaskTimedOutEventAttributes := historyEvent.GetActivityTaskTimedOutEventAttributes()
eventId := activityTaskTimedOutEventAttributes.GetScheduledEventId()
compactedHistory[eventId].icon = "⏰"
compactedHistory[activityTaskTimedOutEventAttributes.GetScheduledEventId()].events = append(compactedHistory[activityTaskTimedOutEventAttributes.GetScheduledEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED:
activityTaskCancelRequestedEventAttributes := historyEvent.GetActivityTaskCancelRequestedEventAttributes()
eventId := activityTaskCancelRequestedEventAttributes.GetScheduledEventId()
compactedHistory[eventId].icon = "🚫"
compactedHistory[activityTaskCancelRequestedEventAttributes.GetScheduledEventId()].events = append(compactedHistory[activityTaskCancelRequestedEventAttributes.GetScheduledEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_ACTIVITY_TASK_CANCELED:
activityTaskCanceledEventAttributes := historyEvent.GetActivityTaskCanceledEventAttributes()
eventId := activityTaskCanceledEventAttributes.GetScheduledEventId()
compactedHistory[eventId].icon = "🚫"
compactedHistory[activityTaskCanceledEventAttributes.GetScheduledEventId()].events = append(compactedHistory[activityTaskCanceledEventAttributes.GetScheduledEventId()].events, historyEvent)
// Timer events
case temporalEnums.EVENT_TYPE_TIMER_STARTED:
eventId := historyEvent.GetEventId()
// initialize the compacted history list
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
compactedHistory[eventId].actionType = "Timer"
compactedHistory[eventId].icon = "⏰"
compactedHistory[eventId].rowContent = historyEvent.GetTimerStartedEventAttributes().GetTimerId()
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
case temporalEnums.EVENT_TYPE_TIMER_FIRED:
timerFiredEventAttributes := historyEvent.GetTimerFiredEventAttributes()
eventId := timerFiredEventAttributes.GetStartedEventId()
compactedHistory[eventId].icon = "🔥"
compactedHistory[eventId].events = append(compactedHistory[timerFiredEventAttributes.GetStartedEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_TIMER_CANCELED:
timerCanceledEventAttributes := historyEvent.GetTimerCanceledEventAttributes()
eventId := timerCanceledEventAttributes.GetStartedEventId()
compactedHistory[eventId].icon = "🚫"
compactedHistory[eventId].events = append(compactedHistory[timerCanceledEventAttributes.GetStartedEventId()].events, historyEvent)
// Child workflow events
case temporalEnums.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED:
eventId := historyEvent.GetEventId()
// initialize the compacted history list
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
inputPayloads := historyEvent.GetStartChildWorkflowExecutionInitiatedEventAttributes().GetInput().GetPayloads()
if inputPayloads != nil {
prettyJsonString := convertDataToPrettyJSON(historyEvent.GetStartChildWorkflowExecutionInitiatedEventAttributes().GetInput().GetPayloads()[0].GetData())
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Input", eventData: prettyJsonString})
}
compactedHistory[eventId].actionType = "Child Workflow"
compactedHistory[eventId].icon = "👶🏃"
compactedHistory[eventId].rowContent = historyEvent.GetStartChildWorkflowExecutionInitiatedEventAttributes().GetWorkflowType().GetName()
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
case temporalEnums.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED:
childWorkflowExecutionStartedEventAttributes := historyEvent.GetChildWorkflowExecutionStartedEventAttributes()
eventId := childWorkflowExecutionStartedEventAttributes.GetInitiatedEventId()
compactedHistory[eventId].icon = "🏃👶"
compactedHistory[eventId].events = append(compactedHistory[childWorkflowExecutionStartedEventAttributes.GetInitiatedEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED:
childWorkflowExecutionCompletedEventAttributes := historyEvent.GetChildWorkflowExecutionCompletedEventAttributes()
eventId := childWorkflowExecutionCompletedEventAttributes.GetInitiatedEventId()
inputPayloads := childWorkflowExecutionCompletedEventAttributes.GetResult().GetPayloads()
if inputPayloads != nil {
prettyJsonString := convertDataToPrettyJSON(childWorkflowExecutionCompletedEventAttributes.GetResult().GetPayloads()[0].GetData())
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Output", eventData: prettyJsonString})
}
compactedHistory[eventId].icon = "✅👶"
compactedHistory[eventId].events = append(compactedHistory[childWorkflowExecutionCompletedEventAttributes.GetInitiatedEventId()].events, historyEvent)
case temporalEnums.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED:
childWorkflowExecutionFailedEventAttributes := historyEvent.GetChildWorkflowExecutionFailedEventAttributes()
eventId := childWorkflowExecutionFailedEventAttributes.GetInitiatedEventId()
compactedHistory[eventId].icon = "❌👶"
// General workflow events
case temporalEnums.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED:
eventId := historyEvent.GetEventId()
executionStartedEventAttributes := historyEvent.GetWorkflowExecutionStartedEventAttributes()
// initialize the compacted history list
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
inputPayloads := executionStartedEventAttributes.GetInput().GetPayloads()
if inputPayloads != nil {
prettyJsonString := convertDataToPrettyJSON(executionStartedEventAttributes.GetInput().GetPayloads()[0].GetData())
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Input", eventData: prettyJsonString})
}
compactedHistory[eventId].actionType = eventType.String()
compactedHistory[eventId].icon = "🚀"
compactedHistory[eventId].rowContent = "Workflow started"
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
case temporalEnums.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED:
eventId := historyEvent.GetEventId()
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
executionCompletedEventAttributes := historyEvent.GetWorkflowExecutionCompletedEventAttributes()
outputPayloads := executionCompletedEventAttributes.GetResult().GetPayloads()
if outputPayloads != nil {
prettyJsonString := convertDataToPrettyJSON(executionCompletedEventAttributes.GetResult().GetPayloads()[0].GetData())
compactedHistory[eventId].eventsContent = append(compactedHistory[eventId].eventsContent, eventContent{eventType: "Output", eventData: prettyJsonString})
}
compactedHistory[eventId].actionType = eventType.String()
compactedHistory[eventId].icon = "✅"
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
case temporalEnums.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED:
eventId := historyEvent.GetEventId()
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
signalName := historyEvent.GetWorkflowExecutionSignaledEventAttributes().GetSignalName()
compactedHistory[eventId].actionType = eventType.String()
compactedHistory[eventId].icon = "🛜"
compactedHistory[eventId].rowContent = signalName
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
default:
eventId := historyEvent.GetEventId()
eventType := historyEvent.GetEventType()
// initialize the compacted history list
if compactedHistory[eventId] == nil && !strings.Contains(eventType.String(), "WorkflowTask") {
compactedHistory[eventId] = &compactHistoryListItem{events: make([]*history.HistoryEvent, 0)}
compactedHistory[eventId].actionType = eventType.String()
compactedHistory[eventId].events = append(compactedHistory[eventId].events, historyEvent)
}
}
}
return compactedHistory
}
var leftBoxStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var rightBoxStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var bottomBoxStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var historyListBoxStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var historyDetailBoxStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var topBarStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder())
var (
topBarHeight = 3
)
func getModuleBorderStyle(width int, title string) lipgloss.Style {
border := lipgloss.Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "┌",
TopRight: "┐",
BottomLeft: "└",
BottomRight: "┘",
}
firstPartOfBorder := "--|" + title + "|"
border.Top = "--|" + title + "|" + strings.Repeat("-", width-len(firstPartOfBorder)-1)
return lipgloss.NewStyle().
Border(border).
Width(width).
MaxWidth(width + 10)
}
func truncateTextBlock(text string, maxHeight int, maxWidth int) string {
totalNewLines := strings.Count(text, "\n")
if totalNewLines < maxHeight {
return text
}
tmpStyle := lipgloss.NewStyle().Width(maxWidth).Render(text)
newLineIndex := 0
newLineCount := 0
for true {
// Index of the next newline
tmpNewLineIndex := strings.Index(tmpStyle[newLineIndex:], "\n")
newLineIndex += tmpNewLineIndex
if tmpNewLineIndex == -1 {
break
}
if newLineCount == maxHeight {
break
}
newLineCount++
// Add 1 to the newline index to skip the newline character
newLineIndex++
}
if newLineIndex-3 < 0 {
return tmpStyle
}
return tmpStyle[:newLineIndex-3] + "..."
}
func (m *model) createEventDetailsRows(compactHistoryListItem compactHistoryListItem, width int, height int) string {
focusedHistoryEvents := compactHistoryListItem.eventsContent
focusedHistoryEventContent := ""
eventBlockHeight := 0
if len(focusedHistoryEvents) != 0 {
eventBlockHeight = height / len(focusedHistoryEvents)
}
for _, historyEvent := range focusedHistoryEvents {
truncatedHistoryEvent := truncateTextBlock(historyEvent.eventData, eventBlockHeight, width)
focusedHistoryEventContent += getModuleBorderStyle(width-2, historyEvent.eventType).Render(truncatedHistoryEvent) + "\n"
}
return lipgloss.NewStyle().Width(width).Height(height).Render(focusedHistoryEventContent)
}
func (m *focusedModeState) getCurrentCompactHistorySlice() []*compactHistoryListItem {
// Convert compactHistory into a slice
compactHistorySlice := make([]*compactHistoryListItem, 0)
for _, compactHistoryItem := range m.getCurrentHistoryStackItem().compactHistory {
compactHistorySlice = append(compactHistorySlice, compactHistoryItem)
}
// Pending events
sort.Slice(compactHistorySlice, func(i, j int) bool {
// Sort by the first eventid
return compactHistorySlice[i].events[0].GetEventId() > compactHistorySlice[j].events[0].GetEventId()
})
return compactHistorySlice
}
// Each border is .5 characters wide, so we subtract 2 from the width and height
func (m model) focusedModeView() string {
boxWidth := m.viewport.Width / 2
bottomAreaHeight := m.viewport.Height - topBarHeight - 5
historyListBoxStyleWithDem := historyListBoxStyle.Height(bottomAreaHeight).Width(boxWidth - 2)
currentHistoryStackItem := m.focusedWorkflowState.getCurrentHistoryStackItem()
historyEventTableStyle := table.New().
Width(historyListBoxStyleWithDem.GetWidth()).
Border(lipgloss.HiddenBorder()).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == m.focusedWorkflowState.cursor:
return SelectedRowStyle
case row%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
})
// Convert compactHistory into a slice
compactHistorySlice := m.focusedWorkflowState.getCurrentCompactHistorySlice()
for _, compactHistoryItem := range compactHistorySlice {
firstEvent := compactHistoryItem.events[0]
historyEventTableStyle.Row(compactHistoryItem.icon, strconv.FormatInt(firstEvent.GetEventId(), 10), compactHistoryItem.actionType, compactHistoryItem.rowContent)
}
focusedHistoryEvents := compactHistorySlice[m.focusedWorkflowState.cursor]
focusedHistoryEventContent := m.createEventDetailsRows(*focusedHistoryEvents, boxWidth-2, bottomAreaHeight)
statusIcon := statusToStyleMap[currentHistoryStackItem.workflowDescription.GetWorkflowExecutionInfo().GetStatus().String()].icon
childIcon := ""
if currentHistoryStackItem.workflowDescription.GetWorkflowExecutionInfo().GetParentExecution() != nil {
childIcon = "👶"
}
topBarContent := topBarStyle.Height(topBarHeight - 2).Width(m.viewport.Width - 3).Render(childIcon + " " + statusIcon + " Workflow ID: " + currentHistoryStackItem.workflowId)
return lipgloss.JoinVertical(lipgloss.Top, topBarContent, lipgloss.JoinHorizontal(lipgloss.Top, focusedHistoryEventContent, historyListBoxStyleWithDem.Render(historyEventTableStyle.Render())))
}