-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1064 lines (974 loc) · 34.9 KB
/
main.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"go.temporal.io/api/common/v1"
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"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func formatNumber(number int) string {
switch {
case number > 1_000_000:
return fmt.Sprintf("%.1fM", float64(number)/1_000_000)
case number > 1_000:
return fmt.Sprintf("%.1fK", float64(number)/1_000)
default:
return fmt.Sprintf("%d", number)
}
}
// ========================================
// Keybindings
// ========================================
type KeyMap struct {
Up key.Binding
Down key.Binding
SearchWorkflowType key.Binding
SearchWorkflowId key.Binding
SearchExecutionStatus key.Binding
Help key.Binding
Exit key.Binding
ClearSearch key.Binding
RefetchWorkflows key.Binding
Select key.Binding
OpenWorkflowInWeb key.Binding
TerminateWorkflow key.Binding
RestartWorkflow key.Binding
ToggleParentWorkflowMode key.Binding
FocusWorkflow key.Binding
NextPage key.Binding
PrevPage key.Binding
}
var DefaultKeyMap = KeyMap{
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"),
),
SearchWorkflowType: key.NewBinding(
key.WithKeys("w", "search Type"),
key.WithHelp("w", "search Type"),
),
SearchWorkflowId: key.NewBinding(
key.WithKeys("i", "search WorkflowId"),
key.WithHelp("i", "search WorkflowId"),
),
SearchExecutionStatus: key.NewBinding(
key.WithKeys("s", "search Status"),
key.WithHelp("s", "search Status"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "toggle help"),
),
Exit: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "exit"),
),
ClearSearch: key.NewBinding(
key.WithKeys("c"),
key.WithHelp("c", "clear search"),
),
RefetchWorkflows: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "refetch"),
),
Select: key.NewBinding(
key.WithKeys("enter", "space"),
key.WithHelp("enter/space", "toggle selection"),
),
OpenWorkflowInWeb: key.NewBinding(
key.WithKeys("o"),
key.WithHelp("o", "open in web"),
),
TerminateWorkflow: key.NewBinding(
key.WithKeys("t"),
key.WithHelp("t", "terminate workflow"),
),
RestartWorkflow: key.NewBinding(
key.WithKeys("R"),
key.WithHelp("R", "restart workflow"),
),
ToggleParentWorkflowMode: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "toggle parent workflow mode"),
),
FocusWorkflow: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "focus workflow"),
),
NextPage: key.NewBinding(
key.WithKeys("]"),
key.WithHelp("]", "Go to next page"),
),
PrevPage: key.NewBinding(
key.WithKeys("["),
key.WithHelp("[", "Go to previous page"),
),
}
// ShortHelp returns keybindings to be shown in the mini help view. It's part
// of the key.Map interface.
func (k KeyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Up, k.Down, k.SearchWorkflowType, k.SearchWorkflowId, k.SearchExecutionStatus, k.Help, k.ClearSearch, k.RefetchWorkflows,
k.Select, k.OpenWorkflowInWeb, k.TerminateWorkflow, k.RestartWorkflow, k.Exit,
}
}
// FullHelp returns keybindings for the expanded help view. It's part of the
// key.Map interface.
func (k KeyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down, k.SearchWorkflowType, k.SearchExecutionStatus, k.SearchWorkflowId, k.ToggleParentWorkflowMode, k.OpenWorkflowInWeb, k.ClearSearch, k.RefetchWorkflows, k.RestartWorkflow, k.TerminateWorkflow, k.Exit, k.NextPage, k.PrevPage},
}
}
// ========================================
// Status Styles
// ========================================
// Status String to style map
var temporalEnumStatusList = []string{
temporalEnums.WORKFLOW_EXECUTION_STATUS_RUNNING.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_COMPLETED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_FAILED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_CANCELED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_TERMINATED.String(),
// I removed the CONTINUED_AS_NEW status
}
var TABLE_LIST_PAGE_SIZE = 40
type ExecutionStatusStyleInfo struct {
displayName string
icon string
color string
}
var statusToStyleMap = map[string]ExecutionStatusStyleInfo{
temporalEnums.WORKFLOW_EXECUTION_STATUS_COMPLETED.String(): {
displayName: "Completed",
icon: "✅",
color: "#00ff00",
},
temporalEnums.WORKFLOW_EXECUTION_STATUS_FAILED.String(): {
displayName: "Failed",
icon: "❌",
color: "#ff0000",
},
temporalEnums.WORKFLOW_EXECUTION_STATUS_CANCELED.String(): {
displayName: "Canceled",
icon: "✋",
color: "#808080",
},
temporalEnums.WORKFLOW_EXECUTION_STATUS_RUNNING.String(): {
displayName: "Running",
icon: "🏃",
color: "#0000ff",
},
temporalEnums.WORKFLOW_EXECUTION_STATUS_TERMINATED.String(): {
displayName: "Terminated",
// Skull icon
icon: "💀",
color: "#ffff00",
},
temporalEnums.WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW.String(): {
displayName: "Cont. New",
icon: "🔄",
color: "#800080",
},
}
// ========================================
// Confirmation Message Flow
// ========================================
type confirmationFlowStateEnums string
const (
NO_FLOW_RUNNING confirmationFlowStateEnums = "NO_FLOW_RUNNING"
AWAITING_CONFIRMATION confirmationFlowStateEnums = "AWAITING_CONFIRMATION"
EXECUTING_ACTION confirmationFlowStateEnums = "EXECUTING_ACTION"
ACTION_COMPLETED confirmationFlowStateEnums = "ACTION_COMPLETED"
)
type confirmationFlowStateMsg struct {
state confirmationFlowStateEnums
pendingConfirmationMessage string
executionSuccessMessage string
areYouSureMessage string
commandThatRunsOnConfirmation tea.Cmd
}
func (m model) startConfirmationMessageFlowCmd(confirmationFlowStateMsg confirmationFlowStateMsg) tea.Cmd {
return func() tea.Msg {
confirmationFlowStateMsg.state = AWAITING_CONFIRMATION
return func() tea.Msg {
confirmationFlowStateMsg.commandThatRunsOnConfirmation()
confirmationFlowStateMsg.state = ACTION_COMPLETED
return confirmationFlowStateMsg
}
}
}
// ========================================
// Screen Constants
// ========================================
const (
// App Header Height
HEADER_HEIGHT = 4
// App Search Input Height
SEARCH_INPUT_HEIGHT = 1
)
// ========================================
// Search Logic and Rendering
// ========================================
var textInputWrapperStyle = lipgloss.NewStyle().Height(SEARCH_INPUT_HEIGHT)
type retrievedSearchOptionsMsg struct {
searchOptions []string
}
// The struct that stores the search options by serach mode
type activeSearchParams map[searchMode][]string
func (m model) handleSearchModeSelect(msg tea.KeyMsg) model {
if key.Matches(msg, m.keys.SearchWorkflowType) {
m.searchMode = WORKFLOWTYPE
m.searchInput.Prompt = "Search WorkflowType: "
m.searchInput.Focus()
}
if key.Matches(msg, m.keys.SearchWorkflowId) {
m.searchMode = WORKFLOWID
m.searchInput.Focus()
m.searchInput.Prompt = "Search WorkflowId: "
}
if key.Matches(msg, m.keys.SearchExecutionStatus) {
m.searchMode = EXECUTIONSTATUS
m.searchInput.Prompt = "Search WorkflowStatus: "
m.searchInput.Focus()
}
return m
}
func (m model) handleSearchUpdate(msg tea.KeyMsg) (model, tea.Cmd) {
if m.searchInput.Focused() && msg.String() == "enter" {
if m.searchMode == EXECUTIONSTATUS {
caser := cases.Title(language.English)
m.searchInput.SetValue(caser.String(m.searchInput.Value()))
}
m.activeSearchParams[m.searchMode] = append(m.activeSearchParams[m.searchMode], m.searchInput.Value())
m.searchInput.Blur()
m.searchInput.SetValue("")
m.searchMode = ""
m.clearListState()
return m, m.refetchWorkflowsCmd()
}
if msg.String() == "esc" {
m.searchInput.Blur()
m.searchMode = ""
return m, nil
}
if msg.String() == "ctrl+c" {
m.searchInput.Blur()
m.searchMode = ""
return m, nil
}
if m.searchInput.Focused() {
var cmd tea.Cmd
m.searchInput, cmd = m.searchInput.Update(msg)
return m, tea.Batch(m.getPossibleSearchOptionsCmd, cmd)
}
return m, nil
}
func (m model) getPossibleSearchOptionsCmd() tea.Msg {
if m.searchInput.Value() == "" {
return []string{}
}
if m.searchMode == WORKFLOWTYPE || m.searchMode == WORKFLOWID {
temporalClient, _ := m.getTemporalClient()
query := fmt.Sprintf("%s BETWEEN \"%s\" AND \"%s~\"", m.searchMode, m.searchInput.Value(), m.searchInput.Value())
result, err := temporalClient.ListWorkflow(context.Background(), &workflowservice.ListWorkflowExecutionsRequest{
Query: query,
PageSize: 1,
})
if err != nil {
log.Fatalf("Failed to list workflows: %v", err)
}
opts := []string{}
for _, w := range result.GetExecutions() {
if m.searchMode == WORKFLOWID {
opts = append(opts, w.GetExecution().WorkflowId)
}
if m.searchMode == WORKFLOWTYPE {
opts = append(opts, w.GetType().Name)
}
}
return retrievedSearchOptionsMsg{searchOptions: opts}
}
if m.searchMode == EXECUTIONSTATUS {
opts := []string{
temporalEnums.WORKFLOW_EXECUTION_STATUS_COMPLETED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_FAILED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_CANCELED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_RUNNING.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_TERMINATED.String(),
temporalEnums.WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW.String(),
}
return retrievedSearchOptionsMsg{searchOptions: opts}
}
return []string{}
}
func (m model) constructQueryString() string {
currentSearchParams := m.activeSearchParams
// Loop through the search params and construct the query string
queryString := ""
if m.parentWorkflowMode {
queryString = "ParentWorkflowId is null"
}
for searchMode, searchValues := range currentSearchParams {
if len(searchValues) == 0 {
continue
}
if queryString != "" {
queryString += " AND "
}
querySegments := []string{}
for _, searchValue := range searchValues {
querySegments = append(querySegments, fmt.Sprintf("%s = '%s'", searchMode, searchValue))
}
queryGroupString := fmt.Sprintf("(%s)", strings.Join(querySegments, " OR "))
queryString += queryGroupString
}
return queryString
}
func (m model) renderFooter() string {
if m.confirmationFlowState.state == EXECUTING_ACTION {
return m.confirmationFlowState.pendingConfirmationMessage + "..."
}
if m.confirmationFlowState.state == ACTION_COMPLETED {
return m.confirmationFlowState.executionSuccessMessage
}
if m.confirmationFlowState.state == AWAITING_CONFIRMATION {
return m.confirmationFlowState.areYouSureMessage + " (y/n)"
}
helpView := m.help.View(m.keys)
if m.searchMode == "" {
return helpView
}
textInputWrapperStyle := textInputWrapperStyle.Width(m.viewport.Width)
searchInputStyle := m.searchInput.View()
return textInputWrapperStyle.Render(searchInputStyle)
}
// ========================================
// Table Logic and Rendering
// ========================================
type setFocusedWorkflowMsg struct {
compactedHistoryStackItem compactHistoryStackItem
}
func (m *model) clearListState() {
m.page = 0
m.cursor = 0
nextPageTokenCache := make(map[int][]byte)
nextPageTokenCache[0] = []byte{}
m.nextPageTokenCache = nextPageTokenCache
}
func (m *model) setFocusedWorkflowCmd(workflowId string, runId string) tea.Cmd {
return func() tea.Msg {
temporalClient, _ := m.getTemporalClient()
executionDescription, err := temporalClient.DescribeWorkflowExecution(context.Background(), workflowId, runId)
historyIterator := temporalClient.GetWorkflowHistory(context.Background(), workflowId, runId, false, 0)
pendingActivities := executionDescription.GetPendingActivities()
// Nested loop. We break out of the loop if we find an activity with an attempt > 0
// The append below will alows run
if err != nil {
log.Fatalf("Failed to describe workflow: %v", err)
}
history := []*history.HistoryEvent{}
for historyIterator.HasNext() {
historyEvent, err := historyIterator.Next()
if err != nil {
log.Fatalf("Failed to get workflow history: %v", err)
}
history = append(history, historyEvent)
}
compactedHistory := createCompactHistory(history, pendingActivities)
newCompactedHistoryStackItem := compactHistoryStackItem{
workflowId: workflowId,
runId: runId,
compactHistory: compactedHistory,
workflowDescription: executionDescription,
}
return setFocusedWorkflowMsg{compactedHistoryStackItem: newCompactedHistoryStackItem}
}
}
func (m model) clearCompletionCmd() tea.Cmd {
return tea.Tick(time.Second*3, func(_ time.Time) tea.Msg {
m.confirmationFlowState.state = NO_FLOW_RUNNING
return m.confirmationFlowState
})
}
func (m model) restartWorkflowCmd(workflowId string, runId string) tea.Cmd {
restartWorkflowCmd := func() tea.Msg {
temporalClient, _ := m.getTemporalClient()
namespaceInfo := m.getTemporalConfig()
workflowHistory := temporalClient.GetWorkflowHistory(context.Background(), workflowId, runId, false, 0)
// Find first eventId that is `WORKFLOW_TASK_COMPLETED`,`WORKFLOW_TASK_TIMED_OUT`, `WORKFLOW_TASK_FAILED`
eventId := int64(0)
for workflowHistory.HasNext() {
historyEvent, err := workflowHistory.Next()
if err != nil {
log.Fatalf("Failed to get workflow history: %v", err)
}
switch historyEvent.GetEventType() {
case temporalEnums.EVENT_TYPE_WORKFLOW_TASK_COMPLETED, temporalEnums.EVENT_TYPE_WORKFLOW_TASK_TIMED_OUT, temporalEnums.EVENT_TYPE_WORKFLOW_TASK_FAILED:
eventId = historyEvent.GetEventId()
break
}
}
namespace := namespaceInfo.TemporalNamespace
if eventId == 0 {
log.Fatalf("Failed to find eventId to restart workflow")
}
_, err := temporalClient.ResetWorkflowExecution(context.Background(),
&workflowservice.ResetWorkflowExecutionRequest{
Namespace: namespace,
WorkflowExecution: &common.WorkflowExecution{
WorkflowId: workflowId,
RunId: runId,
},
Reason: "CLI Restart",
WorkflowTaskFinishEventId: eventId,
},
)
if err != nil {
log.Fatalf("Failed to restart workflow: %v", err)
}
return nil
}
return func() tea.Msg {
return confirmationFlowStateMsg{
state: AWAITING_CONFIRMATION,
executionSuccessMessage: "Workflow restarted successfully",
areYouSureMessage: fmt.Sprintf("Are you sure you want to restart workflow %s?", workflowId),
pendingConfirmationMessage: "Are you sure you want to restart this workflow?",
commandThatRunsOnConfirmation: restartWorkflowCmd,
}
}
}
func (m model) terminateWorkflowCmd(workflowId string, runId string) tea.Cmd {
termanateWorkflowCmd := func() tea.Msg {
temporalClient, _ := m.getTemporalClient()
err := temporalClient.TerminateWorkflow(context.Background(), workflowId, runId, "CLI Termination")
if err != nil {
log.Fatalf("Failed to terminate workflow: %v", err)
}
return nil
}
return func() tea.Msg {
return confirmationFlowStateMsg{
state: AWAITING_CONFIRMATION,
areYouSureMessage: fmt.Sprintf("Are you sure you want to terminate workflow %s?", workflowId),
pendingConfirmationMessage: "Are you sure you want to terminate this workflow?",
commandThatRunsOnConfirmation: termanateWorkflowCmd,
}
}
}
func (m model) renderHeader() string {
headerStyle := lipgloss.NewStyle().Padding(0, 0).Width(m.viewport.Width).Height(HEADER_HEIGHT)
queryStringStyle := lipgloss.NewStyle().Padding(0, 0).Width(m.viewport.Width).Height(1)
// Construct the count string
currentQuery := m.constructQueryString()
// Order the upToDateWorkflowCount map by the order of the temporalEnums
// This is to ensure that the order of the counts is consistent
styleStrArray := []string{}
for _, status := range temporalEnumStatusList {
upperCaseStatus := strings.ToUpper(status)
statusInt := temporalEnums.WorkflowExecutionStatus_value[fmt.Sprintf("WORKFLOW_EXECUTION_STATUS_%s", upperCaseStatus)]
count := m.upToDateWorkflowCount[temporalEnums.WorkflowExecutionStatus(statusInt)]
currentCountStyle := lipgloss.NewStyle()
styleStruct := statusToStyleMap[status]
rawCountStr := fmt.Sprintf("%s %s: %s ", styleStruct.icon, styleStruct.displayName, formatNumber(int(count)))
renderedStr := currentCountStyle.Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color(styleStruct.color)).Render(rawCountStr)
styleStrArray = append(styleStrArray, renderedStr)
}
row := lipgloss.JoinHorizontal(lipgloss.Top, styleStrArray...)
return headerStyle.Render(row + "\n" + queryStringStyle.Render(currentQuery))
}
var HeaderStyle = lipgloss.NewStyle().Padding(0, 0).Bold(true)
var EvenRowStyle = lipgloss.NewStyle().Padding(0, 0).Background(lipgloss.Color("#222222"))
var OddRowStyle = lipgloss.NewStyle().Padding(0, 0)
var SelectedRowStyle = lipgloss.NewStyle().Padding(0, 0).Background(lipgloss.Color("#005500"))
var highlightedStatusIconStyle = lipgloss.NewStyle().Background(lipgloss.Color("#0000ff")).Foreground(lipgloss.Color("#ffffff"))
var attemptsStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000"))
func (m model) renderTable(workflows []*workflowTableListItem) string {
helpHeight := lipgloss.Height(m.help.View(m.keys))
tableSurroundStyle := lipgloss.NewStyle().Padding(0, 0).Height(m.viewport.Height - HEADER_HEIGHT - helpHeight)
t := table.New().
Border(lipgloss.RoundedBorder()).
BorderRight(false).
BorderLeft(false).
BorderTop(false).
BorderBottom(false).
BorderHeader(true).
BorderColumn(true).
Width(m.viewport.Width).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == m.cursor:
return SelectedRowStyle
case row%2 == 0:
return EvenRowStyle
default:
return OddRowStyle
}
}).
Headers("Status", "Type", "Id", "Start Time", "Close Time", "Attempts")
for _, w := range workflows {
workflowId := w.workflow.Execution.WorkflowId
closeTime := w.workflow.GetCloseTime().AsTime().In(time.Local).Format(time.RFC3339)
// If close time starts with 1970, it means the workflow is still running and has no close time
if w.workflow.GetStatus().String() == "Running" {
closeTime = "--"
}
attempts := strconv.Itoa(int(w.attempts))
if w.attempts > 3 {
attempts = attemptsStyle.Render(attempts)
}
if w.attempts == 0 {
attempts = "--"
}
childIcon := ""
if w.workflow.GetParentExecution().GetWorkflowId() != "" {
childIcon = "👶"
}
statusIcon := statusToStyleMap[w.workflow.GetStatus().String()].icon
startTimeDiff := getRelativeTimeDiff(time.Now(), w.workflow.GetStartTime().AsTime())
t.Row(statusIcon+childIcon, w.workflow.GetType().Name, workflowId, startTimeDiff, closeTime, attempts)
}
return tableSurroundStyle.Render(t.Render())
}
type backgroundUpdateWorkflowCountMsg struct {
executionStatus temporalEnums.WorkflowExecutionStatus
count int64
}
func (m model) backgroundUpdateWorkflowCountCmd(exeuctionStatus temporalEnums.WorkflowExecutionStatus) tea.Cmd {
return tea.Tick(time.Second*5, func(_ time.Time) tea.Msg {
result := m.refetchWorkflowCountCmd(exeuctionStatus)()
switch msg := result.(type) {
case updateWorkflowCountMsg:
return backgroundUpdateWorkflowCountMsg{executionStatus: exeuctionStatus, count: msg.count}
}
return nil
})
}
type updateWorkflowsMsg struct {
workflows []*workflowTableListItem
nextPageToken []byte
}
type refetchWorkflowCmdOptions struct {
nextPageToken []byte
}
func (m *model) refetchWorkflowsCmd() tea.Cmd {
return func() tea.Msg {
temporalClient, _ := m.getTemporalClient()
query := m.constructQueryString()
nextPageToken := m.nextPageTokenCache[m.page]
queryResult, err := temporalClient.ListWorkflow(context.Background(), &workflowservice.ListWorkflowExecutionsRequest{
Query: query,
PageSize: int32(TABLE_LIST_PAGE_SIZE),
NextPageToken: nextPageToken,
})
if err != nil {
log.Fatalf("Failed to list workflows: %v", err)
}
result := queryResult.GetExecutions()
returnObj := []*workflowTableListItem{}
for _, workflow := range result {
listItem := &workflowTableListItem{workflow: workflow, attempts: 0}
returnObj = append(returnObj, listItem)
}
// I don't love this...but it's needed to solve the issue of updateVisibleWorkflowAttempsBackgroundCmd
// Need the workflow list be up to date. tea.Sequence runs when the message is returned, not when the message is handled
// TODO: Restructure code so updateVisibleWorkflowAttempsBackgroundCmd runs after the updateWorkflowsMsg is handled
m.workflows = returnObj
return updateWorkflowsMsg{workflows: returnObj, nextPageToken: queryResult.NextPageToken}
}
}
type updateVisibleWorkflowsMsg struct {
workflowsMap map[string]*workflow.WorkflowExecutionInfo
}
type updateVisibleWorkflowAttempsMsg struct {
updateMapping map[string]int32
}
func (m *model) updateVisibleWorkflowAttempsBackgroundCmd(delay time.Duration) tea.Cmd {
return tea.Tick(time.Second*delay, func(_ time.Time) tea.Msg {
returnObj := make(map[string]int32)
temporalClient, _ := m.getTemporalClient()
currentRunningExecutionIds := []string{}
for _, execution := range m.workflows {
if execution.workflow.GetCloseTime() == nil {
currentRunningExecutionIds = append(currentRunningExecutionIds, "'"+execution.workflow.GetExecution().WorkflowId+"'")
}
}
if len(currentRunningExecutionIds) == 0 {
return updateVisibleWorkflowAttempsMsg{updateMapping: returnObj}
}
query := fmt.Sprintf("WorkflowId IN (%s)", strings.Join(currentRunningExecutionIds, ","))
queryResult, err := temporalClient.ListWorkflow(context.Background(), &workflowservice.ListWorkflowExecutionsRequest{
Query: query,
PageSize: int32(TABLE_LIST_PAGE_SIZE),
})
if err != nil {
log.Fatalf("Failed to list workflows: %v", err)
}
// Look for workflows that are in the current list and update them
workflows := queryResult.GetExecutions()
for _, workflow := range workflows {
// Skip if started less that 10 minutes ago
if workflow.GetStartTime().AsTime().UTC().After(time.Now().UTC().Add(-10 * time.Minute)) {
continue
}
if workflow.GetStatus() == temporalEnums.WORKFLOW_EXECUTION_STATUS_RUNNING {
execution, err := temporalClient.DescribeWorkflowExecution(
context.Background(),
workflow.GetExecution().WorkflowId,
workflow.GetExecution().RunId,
)
if err != nil {
break
}
pendingActivities := execution.GetPendingActivities()
// Nested loop. We break out of the loop if we find an activity with an attempt > 0
for _, activity := range pendingActivities {
for _, currentWorkflow := range m.workflows {
if workflow.GetExecution().WorkflowId == currentWorkflow.workflow.GetExecution().WorkflowId {
returnObj[workflow.GetExecution().WorkflowId] = activity.GetAttempt()
break
}
}
}
continue
}
}
return updateVisibleWorkflowAttempsMsg{updateMapping: returnObj}
})
}
func (m model) updateVisibleWorkflowsBackgroundCmd() tea.Cmd {
return tea.Tick(time.Second*5, func(_ time.Time) tea.Msg {
returnObj := make(map[string]*workflow.WorkflowExecutionInfo)
temporalClient, _ := m.getTemporalClient()
currentRunningExecutionIds := []string{}
for _, execution := range m.workflows {
if execution.workflow.GetCloseTime() == nil {
currentRunningExecutionIds = append(currentRunningExecutionIds, "'"+execution.workflow.GetExecution().WorkflowId+"'")
}
}
if len(currentRunningExecutionIds) == 0 {
return updateVisibleWorkflowsMsg{workflowsMap: returnObj}
}
query := fmt.Sprintf("WorkflowId IN (%s)", strings.Join(currentRunningExecutionIds, ","))
queryResult, err := temporalClient.ListWorkflow(context.Background(), &workflowservice.ListWorkflowExecutionsRequest{
Query: query,
PageSize: int32(TABLE_LIST_PAGE_SIZE),
})
if err != nil {
log.Fatalf("Failed to list workflows: %v", err)
}
// Look for workflows that are in the current list and update them
workflows := queryResult.GetExecutions()
for _, updatedWorkflow := range workflows {
for _, currentWorkflow := range m.workflows {
if updatedWorkflow.GetExecution().WorkflowId == currentWorkflow.workflow.GetExecution().WorkflowId {
returnObj[updatedWorkflow.GetExecution().WorkflowId] = updatedWorkflow
}
}
}
return updateVisibleWorkflowsMsg{workflowsMap: returnObj}
})
}
type updateWorkflowCountMsg struct {
executionStatus temporalEnums.WorkflowExecutionStatus
count int64
}
func (m model) refetchWorkflowCountCmd(executionStatus temporalEnums.WorkflowExecutionStatus) tea.Cmd {
return func() tea.Msg {
temporalClient, _ := m.getTemporalClient()
statusQuery := fmt.Sprintf("ExecutionStatus = %d", executionStatus)
query := m.constructQueryString()
if query == "" {
query = statusQuery
}
if query != "" {
query = fmt.Sprintf("%s AND %s", query, statusQuery)
}
queryResult, err := temporalClient.CountWorkflow(context.Background(), &workflowservice.CountWorkflowExecutionsRequest{
Query: query,
})
if err != nil {
log.Fatalf("Failed to count workflows: %v", err)
}
result := queryResult.GetCount()
return updateWorkflowCountMsg{executionStatus: executionStatus, count: result}
}
}
// ========================================
// Main Bubble Tea Control Loop
// ========================================
type searchMode string
const (
WORKFLOWTYPE searchMode = "WorkflowType"
WORKFLOWID searchMode = "WorkflowId"
EXECUTIONSTATUS searchMode = "ExecutionStatus"
)
type workflowTableListItem struct {
workflow *workflow.WorkflowExecutionInfo
history []*history.HistoryEvent
pendingActivities []*workflow.PendingActivityInfo
attempts int32
}
type model struct {
focusedWorkflowState focusedModeState
parentWorkflowMode bool
confirmationFlowState confirmationFlowStateMsg
keys KeyMap
help help.Model
page int
nextPageTokenCache map[int][]byte
activeSearchParams activeSearchParams
searchMode searchMode
searchOptions []string
searchInput textinput.Model
ready bool
workflows []*workflowTableListItem
cursor int // which to-do list item our cursor is pointing at
selected map[int]bool
viewport viewport.Model
// This is the workflow count that is up to date in the background
upToDateWorkflowCount map[temporalEnums.WorkflowExecutionStatus]int64
}
func initialModel() model {
textInput := textinput.New()
textInput.Prompt = ""
textInput.ShowSuggestions = true
activeSearchParams := make(map[searchMode][]string)
activeSearchParams[WORKFLOWTYPE] = []string{}
activeSearchParams[WORKFLOWID] = []string{}
activeSearchParams[EXECUTIONSTATUS] = []string{}
nextPageTokenCache := make(map[int][]byte)
nextPageTokenCache[0] = []byte{}
return model{
nextPageTokenCache: nextPageTokenCache,
page: 0,
focusedWorkflowState: focusedModeState{
keys: FocusedModeKeyMap,
compactedHistoryStack: make([]compactHistoryStackItem, 0),
},
parentWorkflowMode: false,
confirmationFlowState: confirmationFlowStateMsg{
state: NO_FLOW_RUNNING,
pendingConfirmationMessage: "",
areYouSureMessage: "",
executionSuccessMessage: "",
commandThatRunsOnConfirmation: func() tea.Msg { return nil },
},
cursor: 0,
keys: DefaultKeyMap,
help: help.New(),
activeSearchParams: activeSearchParams,
searchInput: textInput,
ready: false,
workflows: []*workflowTableListItem{},
selected: make(map[int]bool),
upToDateWorkflowCount: map[temporalEnums.WorkflowExecutionStatus]int64{
temporalEnums.WORKFLOW_EXECUTION_STATUS_COMPLETED: 0,
temporalEnums.WORKFLOW_EXECUTION_STATUS_RUNNING: 0,
temporalEnums.WORKFLOW_EXECUTION_STATUS_FAILED: 0,
temporalEnums.WORKFLOW_EXECUTION_STATUS_CANCELED: 0,
},
}
}
func (m model) View() string {
if len(m.focusedWorkflowState.compactedHistoryStack) > 0 {
return m.focusedModeView()
}
view := m.renderHeader() + "\n" + m.renderTable(m.workflows) + "\n" + m.renderFooter()
return view
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.help.Width = msg.Width
if !m.ready {
m.viewport = viewport.New(msg.Width, msg.Height)
m.viewport.YPosition = 0
m.ready = true
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
}
case setFocusedWorkflowMsg:
m.focusedWorkflowState.cursor = 0
m.focusedWorkflowState.compactedHistoryStack = append(m.focusedWorkflowState.compactedHistoryStack, msg.compactedHistoryStackItem)
return m, nil
case confirmationFlowStateMsg:
m.confirmationFlowState = msg
switch msg.state {
case NO_FLOW_RUNNING, AWAITING_CONFIRMATION, EXECUTING_ACTION:
m.confirmationFlowState = msg
return m, nil
case ACTION_COMPLETED:
m.confirmationFlowState = msg
m.clearListState()
return m, m.clearCompletionCmd()
}
return m, nil
case updateWorkflowCountMsg:
m.upToDateWorkflowCount[msg.executionStatus] = msg.count
return m, nil
case backgroundUpdateWorkflowCountMsg:
m.upToDateWorkflowCount[msg.executionStatus] = msg.count
return m, m.backgroundUpdateWorkflowCountCmd(msg.executionStatus)
case updateVisibleWorkflowAttempsMsg:
for i, existingWorkflow := range m.workflows {
workflowId := existingWorkflow.workflow.GetExecution().WorkflowId
if _, ok := msg.updateMapping[workflowId]; ok {
m.workflows[i].attempts = msg.updateMapping[workflowId]
}
}
return m, m.updateVisibleWorkflowAttempsBackgroundCmd(10)
case updateVisibleWorkflowsMsg:
for i, existingWorkflow := range m.workflows {
workflowId := existingWorkflow.workflow.GetExecution().WorkflowId
if _, ok := msg.workflowsMap[workflowId]; ok {
m.workflows[i].workflow = msg.workflowsMap[workflowId]
}
}
return m, m.updateVisibleWorkflowsBackgroundCmd()
case retrievedSearchOptionsMsg:
m.searchInput.SetSuggestions(msg.searchOptions)
m.searchOptions = msg.searchOptions
return m, nil
case updateWorkflowsMsg:
m.workflows = msg.workflows
m.nextPageTokenCache[m.page+1] = msg.nextPageToken
return m, nil
// Is it a key press?
case tea.KeyMsg:
if m.confirmationFlowState.state == AWAITING_CONFIRMATION {
if msg.String() == "y" {
m.confirmationFlowState.state = EXECUTING_ACTION
// Wrap the command to set the state to action completed
wrappedFunc := func() tea.Msg {
m.confirmationFlowState.commandThatRunsOnConfirmation()
m.confirmationFlowState.state = ACTION_COMPLETED
m.clearListState()
return m.confirmationFlowState
}
return m, wrappedFunc
}
if msg.String() == "n" {
m.confirmationFlowState.state = NO_FLOW_RUNNING
return m, nil
}
}
if m.searchInput.Focused() {
return m.handleSearchUpdate(msg)
}
m = m.handleSearchModeSelect(msg)
switch {
// These keys should exit the program.
case key.Matches(msg, m.keys.Exit):
return m, tea.Quit
case key.Matches(msg, m.keys.ToggleParentWorkflowMode):
m.parentWorkflowMode = !m.parentWorkflowMode
return m, m.refetchWorkflowsCmd()
case key.Matches(msg, m.keys.RestartWorkflow):
if m.cursor < len(m.workflows) {
workflowId := m.workflows[m.cursor].workflow.GetExecution().WorkflowId
runId := m.workflows[m.cursor].workflow.Execution.GetRunId()
return m, m.restartWorkflowCmd(workflowId, runId)
}
case key.Matches(msg, m.keys.TerminateWorkflow):
if m.cursor < len(m.workflows) {
workflowId := m.workflows[m.cursor].workflow.GetExecution().WorkflowId
runId := m.workflows[m.cursor].workflow.Execution.GetRunId()
return m, m.terminateWorkflowCmd(workflowId, runId)
}
case key.Matches(msg, m.keys.OpenWorkflowInWeb):
if m.cursor < len(m.workflows) {
workflowId := m.workflows[m.cursor].workflow.GetExecution().WorkflowId
runId := m.workflows[m.cursor].workflow.Execution.GetRunId()
m.openWorkflowInBrowser(workflowId, runId)
}
case key.Matches(msg, m.keys.Help):
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, m.keys.NextPage):
m.page++
return m, m.refetchWorkflowsCmd()
case key.Matches(msg, m.keys.PrevPage):
if m.page > 0 {
m.page--
}
return m, m.refetchWorkflowsCmd()
// Reset the search params if c is pressed