/
textui.go
487 lines (453 loc) · 15.6 KB
/
textui.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/Shopify/sarama"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/henrikengstrom/jokk/common"
"github.com/henrikengstrom/jokk/kafka"
)
type UICtrl struct {
infoArea *widgets.Paragraph
mainArea *widgets.Paragraph
commandArea *widgets.Paragraph
grid *ui.Grid
}
func listTopicsLoop(envCtrl EnvCtrl, uiCtrl UICtrl) {
envCtrl.logger.Clear()
titleText := fmt.Sprintf("List Topics - data retrieved %s", time.Now().Format("2006-01-02 15:04:05"))
if envCtrl.args.Filter != "" {
titleText = fmt.Sprintf("%s - filter '%s'", titleText, envCtrl.args.Filter)
}
if envCtrl.args.StartTime != "" || envCtrl.args.EndTime != "" {
titleText = fmt.Sprintf("%s - period '%s to %s'", titleText, envCtrl.args.StartTime, envCtrl.args.EndTime)
}
// Indicate that something is in progress
uiCtrl.mainArea.Title = titleText
uiCtrl.mainArea.Text = "Retrieving topics..."
ui.Render(uiCtrl.grid)
topics, _ := listTopics(envCtrl.logger, envCtrl.admin, envCtrl.client, envCtrl.args)
menuText := "T:Topic Info, C:Create Topic, R:Remove Topic, F:Filter, Z:Refresh Page, M:Main, Q:Quit"
availableRows := uiCtrl.mainArea.Dy()
content := envCtrl.logger.ContentString()
rowsContent := strings.Split(content, "\n")
numberRows := strings.Count(content, "\n")
if numberRows > availableRows {
// navigation should be enabled
menuText = "D:Down, U:Up, " + menuText
text := ""
for i := 0; i < availableRows; i++ {
text = fmt.Sprintf("%s%s\n", text, rowsContent[i])
}
uiCtrl.mainArea.Text = text
} else {
uiCtrl.mainArea.Text = envCtrl.logger.ContentString()
}
if envCtrl.args.Filter != "" {
menuText = "A:All Topics, " + menuText
}
uiCtrl.commandArea.Text = menuText
ui.Render(uiCtrl.grid)
scrollPosition := 0
contentPosition := 0
for e := range ui.PollEvents() {
if e.Type == ui.KeyboardEvent {
switch strings.ToUpper(e.ID) {
case "Q":
ui.Close()
os.Exit(0)
case "C":
// create topic
uiCtrl.commandArea.Text = "Topic name: "
ui.Render(uiCtrl.grid)
topicName := keyboardInput(uiCtrl, "0")
uiCtrl.commandArea.Text = "Number of partitions: "
ui.Render(uiCtrl.grid)
partitions := keyboardInput(uiCtrl, "0")
numPartitions, _ := strconv.Atoi(partitions)
uiCtrl.commandArea.Text = "Replication factor: "
ui.Render(uiCtrl.grid)
replicationFactor := keyboardInput(uiCtrl, "0")
numReplicationFactor, _ := strconv.Atoi(replicationFactor)
addTopic(topicName, int32(numPartitions), int16(numReplicationFactor), envCtrl.logger, envCtrl.admin)
listTopicsLoop(envCtrl, uiCtrl)
case "R":
// remove/delete topic
uiCtrl.commandArea.Text = "Type the topic number to delete and press enter (or X to leave): "
ui.Render(uiCtrl.grid)
topicNumber := keyboardInput(uiCtrl, "X")
if topicNumber != "X" {
topicName := extractTopicName(topicNumber, rowsContent)
if topicName != "" {
deleteTopic(topicName, envCtrl.logger, envCtrl.admin)
}
listTopicsLoop(envCtrl, uiCtrl)
}
case "A":
// reset filter to get all topics
envCtrl.args.Filter = ""
listTopicsLoop(envCtrl, uiCtrl)
case "F":
uiCtrl.commandArea.Text = "Type filter and press enter (or 0 to leave): "
ui.Render(uiCtrl.grid)
result := keyboardInput(uiCtrl, "0")
if result != "0" {
envCtrl.args.Filter = result
}
listTopicsLoop(envCtrl, uiCtrl)
case "M":
internalMainManuLoop(envCtrl, uiCtrl)
case "D":
cPos, sPos, text := handleScroll("|1|", scrollPosition, 1, availableRows, rowsContent)
contentPosition = cPos
scrollPosition = sPos
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
case "U":
if scrollPosition > contentPosition {
cPos, sPos, text := handleScroll("|1|", scrollPosition, -1, availableRows, rowsContent)
contentPosition = cPos
scrollPosition = sPos
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
}
case "T":
uiCtrl.commandArea.Text = "Type the topic number and press enter (or X to leave): "
ui.Render(uiCtrl.grid)
topicNumber := keyboardInput(uiCtrl, "X")
if topicNumber != "X" {
topicName := extractTopicName(topicNumber, rowsContent)
if topicName != "" {
topicDetails, _, _ := filterTopics(topics, topicName)
topicDetail := topicDetails[topicName]
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
}
}
case "Z":
listTopicsLoop(envCtrl, uiCtrl)
}
}
}
}
func topicInfoLoop(topicName string, topicDetail sarama.TopicDetail, envCtrl EnvCtrl, uiCtrl UICtrl) {
envCtrl.logger.Clear()
topicInfo(envCtrl.logger, topicName, topicDetail, envCtrl.admin, envCtrl.client)
titleText := fmt.Sprintf("Topic Info - data retrieved %s", time.Now().Format("2006-01-02 15:04:05"))
if envCtrl.args.StartTime != "" || envCtrl.args.EndTime != "" {
titleText = fmt.Sprintf("%s - period '%s to %s'", titleText, envCtrl.args.StartTime, envCtrl.args.EndTime)
}
uiCtrl.mainArea.Title = titleText
menuText := "C:Clear/Empty Topic, V:View Messages, S:Save Messages, I:Import Messages, L:List Topics, Z:Refresh Page, M:Main, Q:Quit"
availableRows := uiCtrl.mainArea.Dy()
content := envCtrl.logger.ContentString()
rowsContent := strings.Split(content, "\n")
numberRows := strings.Count(content, "\n")
if numberRows > availableRows {
// navigation should be enabled
menuText = "D:Down, U:Up, " + menuText
text := ""
for i := 0; i < availableRows; i++ {
text = fmt.Sprintf("%s%s\n", text, rowsContent[i])
}
uiCtrl.mainArea.Text = text
} else {
uiCtrl.mainArea.Text = envCtrl.logger.ContentString()
}
uiCtrl.commandArea.Text = menuText
ui.Render(uiCtrl.grid)
scrollPosition := 0
contentPosition := 0
for e := range ui.PollEvents() {
if e.Type == ui.KeyboardEvent {
switch strings.ToUpper(e.ID) {
case "C":
uiCtrl.commandArea.Text = "Type Y to clear topic messages: "
ui.Render(uiCtrl.grid)
response := keyboardInput(uiCtrl, "X")
if response == "Y" {
uiCtrl.mainArea.Text = "Clearing messages from topic..."
ui.Render(uiCtrl.grid)
clearTopic(topicName, envCtrl.logger, envCtrl.admin, envCtrl.client)
}
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
case "S":
uiCtrl.commandArea.Text = "File name to save messages to: "
ui.Render(uiCtrl.commandArea)
fileName := keyboardInput(uiCtrl, "X")
if fileName != "X" {
storeMessages(envCtrl.logger, fileName, topicName, envCtrl.consumer, envCtrl.args)
uiCtrl.commandArea.Text = fmt.Sprintf("Messages saved to: %s - press enter to continue", fileName)
ui.Render(uiCtrl.commandArea)
keyboardInput(uiCtrl, "X")
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
}
case "I":
uiCtrl.commandArea.Text = "File name to import messages from: "
ui.Render(uiCtrl.commandArea)
fileName := keyboardInput(uiCtrl, "X")
if fileName != "X" {
msgCount, err := importMessages(envCtrl.logger, fileName, topicName, []string{envCtrl.kafkaHost}, envCtrl.producerConfig, envCtrl.args)
if err != nil {
uiCtrl.commandArea.Text = fmt.Sprintf("Could not import messages from file: %s, %v - press enter to continue", fileName, err)
} else {
uiCtrl.commandArea.Text = fmt.Sprintf("Imported %d messages to topic %s - press enter to continue", msgCount, topicName)
}
ui.Render(uiCtrl.commandArea)
keyboardInput(uiCtrl, "X")
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
}
case "L":
listTopicsLoop(envCtrl, uiCtrl)
case "M":
internalMainManuLoop(envCtrl, uiCtrl)
case "Q", "<C-c>":
ui.Close()
os.Exit(0)
case "D":
cPos, sPos, text := handleScroll(topicName, scrollPosition, 1, availableRows, rowsContent)
contentPosition = cPos
scrollPosition = sPos
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
case "U":
if scrollPosition > contentPosition {
cPos, sPos, text := handleScroll(topicName, scrollPosition, -1, availableRows, rowsContent)
contentPosition = cPos
scrollPosition = sPos
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
}
case "Z":
uiCtrl.mainArea.Text = "Refreshing page..."
ui.Render(uiCtrl.grid)
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
case "V":
viewMessagesLoop(topicName, topicDetail, envCtrl, uiCtrl)
}
}
}
}
type MsgInfo struct {
timestamp time.Time
offset int64
value string
}
func viewMessagesLoop(topicName string, topicDetail sarama.TopicDetail, envCtrl EnvCtrl, uiCtrl UICtrl) {
resultChan := make(chan sarama.ConsumerMessage)
commandChan := make(chan string)
go viewMessages(topicName, envCtrl.logger, envCtrl.consumer, envCtrl.args, resultChan, commandChan)
titleText := fmt.Sprintf("View Messages - topic '%s'", topicName)
if envCtrl.args.StartTime != "" || envCtrl.args.EndTime != "" {
titleText = fmt.Sprintf("%s - period '%s to %s'", titleText, envCtrl.args.StartTime, envCtrl.args.EndTime)
}
uiCtrl.mainArea.Title = titleText
commandText := "N:Next Message, Z:Refresh Page, T:Topic Info, L:List Topics, M:Main, Q:Quit"
uiCtrl.commandArea.Text = commandText
text := "Retrieving messages..."
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
msgs := []MsgInfo{}
for {
select {
case e := <-ui.PollEvents():
if e.Type == ui.KeyboardEvent {
switch strings.ToUpper(e.ID) {
case "N":
commandChan <- "Y"
case "T":
topicInfoLoop(topicName, topicDetail, envCtrl, uiCtrl)
case "L":
listTopicsLoop(envCtrl, uiCtrl)
case "M":
resultChan = nil
commandChan <- "N"
commandChan = nil
internalMainManuLoop(envCtrl, uiCtrl)
case "Z":
resultChan = nil
commandChan <- "N"
commandChan = nil
viewMessagesLoop(topicName, topicDetail, envCtrl, uiCtrl)
case "Q", "<C-c>":
ui.Close()
os.Exit(0)
}
}
case msg := <-resultChan:
if msg.Topic != "" {
msgs = append(msgs, MsgInfo{
timestamp: msg.Timestamp,
offset: msg.Offset,
value: string(msg.Value),
})
// FIXME: Only render parts of the messages since the text area can only present a couple.
// The logic here could keep track of total number of messages and the ones that are currently being rendered.
// E.g. msgs 114-135 is currently viewed. When the user scrolls up or down the messages rendered are changed accordingly.
text = CreateMessagesTable(msgs, uiCtrl.mainArea.Dx())
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.mainArea)
} else {
uiCtrl.commandArea.Text = "Z:Refresh Page, T:Topic Info, L:List Topics, M:Main, Q:Quit"
if strings.Contains(text, "Retreiving messages...") {
text = "*** No messages aviailable ***"
} else {
text = fmt.Sprintf("*** No more messages available ***\n%s", text)
}
uiCtrl.mainArea.Text = text
ui.Render(uiCtrl.grid)
}
}
}
}
func keyboardInput(uiCtrl UICtrl, exitChar string) string {
result := ""
originalText := uiCtrl.commandArea.Text
for e := range ui.PollEvents() {
if e.Type == ui.KeyboardEvent {
if e.ID == "<C-c>" || e.ID == exitChar {
result = exitChar
break
} else if e.ID == "<Enter>" {
break
} else if e.ID == "<Space>" {
result = result + " "
} else if e.ID == "<Backspace>" {
result = result[:len(result)-1]
} else {
result = result + e.ID
}
uiCtrl.commandArea.Text = originalText + result
ui.Render(uiCtrl.grid)
}
}
return result
}
func internalMainManuLoop(envCtrl EnvCtrl, uiCtrl UICtrl) {
titleText := "Main menu"
if envCtrl.args.Filter != "" {
titleText = fmt.Sprintf("%s - filter '%s'", titleText, envCtrl.args.Filter)
}
if envCtrl.args.StartTime != "" || envCtrl.args.EndTime != "" {
titleText = fmt.Sprintf("%s - period '%s to %s'", titleText, envCtrl.args.StartTime, envCtrl.args.EndTime)
}
uiCtrl.mainArea.Title = titleText
mainAreaText := `
_ _ _ _ _ _ _ _ _ _ _
(_)(_)(_)_(_)(_)(_)(_)_ (_) _ (_)(_) _ (_)
(_) (_) (_)(_) _ (_) (_) _ (_)
(_) (_) (_)(_) _ (_) (_) _ (_)
(_) (_) (_)(_)(_) _ (_)(_) _
_ (_) (_) (_)(_) (_) _ (_) (_) _
(_) _ (_) (_)_ _ _ _(_)(_) (_) _ (_) (_) _
(_)(_)(_) (_)(_)(_)(_) (_) (_)(_) (_)
The lightweight Kafka insights tool that provides the stuff you need.
Developed in 2022 by Henrik Engström. Free to use without any guarantees whatsoever! ;P
See the "Available Commands" below to get started.
`
uiCtrl.mainArea.Text = mainAreaText
uiCtrl.commandArea.Text = "L:List Topics, F: Filter, P: Period, H:Help, Q:Quit"
ui.Render(uiCtrl.grid)
for e := range ui.PollEvents() {
if e.Type == ui.KeyboardEvent {
switch strings.ToUpper(e.ID) {
case "Q", "<C-c>":
ui.Close()
os.Exit(0)
case "L":
listTopicsLoop(envCtrl, uiCtrl)
case "F":
uiCtrl.commandArea.Text = "Type string to activate filter and press enter (or 0 to leave): "
ui.Render(uiCtrl.grid)
result := keyboardInput(uiCtrl, "0")
if result != "0" {
envCtrl.args.Filter = result
}
internalMainManuLoop(envCtrl, uiCtrl)
case "P":
uiCtrl.commandArea.Text = "Type start time format 'YYYY-MM-DD HH:MM:SS' or B string for the beginning of time (X to leave): "
ui.Render(uiCtrl.grid)
start := keyboardInput(uiCtrl, "X")
if start != "X" {
uiCtrl.commandArea.Text = "Type end time format 'YYYY-MM-DD HH:MM:SS' or N for now (X to leave): "
ui.Render(uiCtrl.commandArea)
end := keyboardInput(uiCtrl, "X")
if end != "X" {
// reset start or end time based on indicators - the parseTime method has logic to interpret empty strings
if start == "B" {
start = ""
}
if end == "N" {
end = ""
}
st, et, err := parseTime(envCtrl.logger, start, end)
if err != nil {
uiCtrl.commandArea.Text = fmt.Sprintf("Press enter to try again: Input error %v", err)
ui.Render(uiCtrl.grid)
<-ui.PollEvents()
} else {
envCtrl.args.StartTime = st.Local().Format("2006-01-02 15:04:05")
envCtrl.args.EndTime = et.Local().Format("2006-01-02 15:04:05")
}
}
}
internalMainManuLoop(envCtrl, uiCtrl)
case "H":
//helpLoop(envCtrl, uiCtrl)
default:
uiCtrl.mainArea.Title = e.ID
ui.Render(uiCtrl.grid)
}
}
}
}
func MainMenuLoop(log common.Logger, admin sarama.ClusterAdmin, client sarama.Client, consumer kafka.JokkConsumer, producerConf *sarama.Config, args Args, kafkaHost string) {
if err := ui.Init(); err != nil {
log.Panicf("failed to initialize termui: %v", err)
}
defer ui.Close()
grid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
infoArea := widgets.NewParagraph()
infoArea.PaddingLeft = 1
infoArea.Title = "Connection Information"
infoArea.Text = fmt.Sprintf("Connected to environment: %s, host: %s", args.Environment, kafkaHost)
mainArea := widgets.NewParagraph()
mainArea.PaddingLeft = 1
commandArea := widgets.NewParagraph()
commandArea.PaddingLeft = 1
commandArea.Title = "Available Commands"
grid.Set(
ui.NewRow(1.0/15,
ui.NewCol(1.0/1, infoArea),
),
ui.NewRow(13.0/15,
ui.NewCol(1.0/1, mainArea),
),
ui.NewRow(1.0/15,
ui.NewCol(1.0/1, commandArea),
),
)
ui.Render(grid)
var logger common.Logger = common.NewCacheLogger()
envCtrl := EnvCtrl{
logger: logger,
admin: admin,
client: client,
consumer: consumer,
args: args,
kafkaHost: kafkaHost,
producerConfig: producerConf,
}
uiCtrl := UICtrl{
infoArea: infoArea,
mainArea: mainArea,
commandArea: commandArea,
grid: grid,
}
internalMainManuLoop(envCtrl, uiCtrl)
}