-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_info_page.go
136 lines (112 loc) · 3.13 KB
/
stream_info_page.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
package main
import (
"encoding/json"
"gopkg.in/yaml.v2"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/solidpulse/natsdash/ds"
)
type StreamInfoPage struct {
*tview.Flex
app *tview.Application
Data *ds.Data
textArea *tview.TextArea
footerTxt *tview.TextView
streamName string
}
func NewStreamInfoPage(app *tview.Application, data *ds.Data) *StreamInfoPage {
sap := &StreamInfoPage{
Flex: tview.NewFlex().SetDirection(tview.FlexRow),
app: app,
Data: data,
}
sap.setupUI()
sap.setupInputCapture()
return sap
}
func (sap *StreamInfoPage) setupUI() {
// Header
headerRow := tview.NewFlex()
headerRow.SetDirection(tview.FlexColumn)
headerRow.SetBorderPadding(1, 0, 1, 1)
headerRow.AddItem(createTextView("[ESC] Back", tcell.ColorWhite), 0, 1, false)
headerRow.SetTitle("STREAM INFO")
sap.AddItem(headerRow, 3, 1, false)
// Text area for YAML
sap.textArea = tview.NewTextArea()
sap.textArea.SetBorder(true)
// sap.textArea.SetDisabled(true)
sap.textArea.SetTitle("Stream Info")
sap.AddItem(sap.textArea, 0, 1, true)
// Footer
footer := tview.NewFlex()
footer.SetBorder(true)
sap.footerTxt = createTextView("", tcell.ColorWhite)
footer.AddItem(sap.footerTxt, 0, 1, false)
sap.AddItem(footer, 3, 1, false)
userFriendlyJSON5 := ``
sap.textArea.SetText(userFriendlyJSON5, false)
}
func (sap *StreamInfoPage) setupInputCapture() {
sap.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyESC {
sap.notify("Loading......", 3*time.Second, "info")
sap.goBack()
return nil
}
return event
})
}
func (sap *StreamInfoPage) goBack() {
pages.SwitchToPage("streamListPage")
_, b := pages.GetFrontPage()
b.(*StreamListPage).redraw(&sap.Data.CurrCtx)
sap.app.SetFocus(b)
}
func (sap *StreamInfoPage) redraw(ctx *ds.Context) {
// Connect to NATS
conn := ctx.Conn
// Get JetStream context
js, err := conn.JetStream()
if err != nil {
sap.notify("Failed to get JetStream context: "+err.Error(), 3*time.Second, "error")
return
}
// Get stream info
stream, err := js.StreamInfo(sap.streamName)
if err != nil {
sap.notify("Failed to get stream info: "+err.Error(), 3*time.Second, "error")
return
}
// First convert to JSON
jsonBytes, err := json.Marshal(stream)
if err != nil {
sap.notify("Failed to convert to JSON: "+err.Error(), 3*time.Second, "error")
return
}
// Parse JSON into generic map
var jsonData interface{}
if err := json.Unmarshal(jsonBytes, &jsonData); err != nil {
sap.notify("Failed to parse JSON: "+err.Error(), 3*time.Second, "error")
return
}
// Convert to YAML
yamlBytes, err := yaml.Marshal(jsonData)
if err != nil {
sap.notify("Failed to convert to YAML: "+err.Error(), 3*time.Second, "error")
return
}
yamlTxt := string(yamlBytes)
sap.textArea.SetText(yamlTxt, false)
go sap.app.Draw()
}
func (sap *StreamInfoPage) notify(message string, duration time.Duration, logLevel string) {
sap.footerTxt.SetText(message)
sap.footerTxt.SetTextColor(getLogLevelColor(logLevel))
go func() {
time.Sleep(duration)
sap.footerTxt.SetText("")
sap.footerTxt.SetTextColor(tcell.ColorWhite)
}()
}