/
models.go
585 lines (544 loc) · 13.6 KB
/
models.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
package ui
import (
"cmp"
"fmt"
"slices"
"sort"
"strings"
"time"
"get.pme.sh/pmesh/client"
"get.pme.sh/pmesh/session"
"get.pme.sh/pmesh/util"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/erikgeiser/promptkit/selection"
"github.com/erikgeiser/promptkit/textinput"
"github.com/samber/lo"
)
type ServiceControl struct {
Use, Short string
Aliases []string
WaitMsg string
Display string
Do func(cli client.Client, name string) (msg string, err error)
}
var ServiceControls = []ServiceControl{
{
Use: "restart [name]",
Short: "Restart service",
Aliases: []string{"r"},
WaitMsg: "Restarting...",
Display: "💫 Restart",
Do: func(cli client.Client, name string) (string, error) {
n, e := cli.ServiceRestart(name, false)
if e != nil {
return "", e
} else {
return fmt.Sprintf("Restarted %d services", n.Count), nil
}
},
},
{
Use: "rebuild [name]",
Short: "Invalidates build cache and restarts service",
Aliases: []string{"invalidate"},
WaitMsg: "Rebuilding...",
Display: "🔨 Rebuild",
Do: func(cli client.Client, name string) (string, error) {
n, e := cli.ServiceRestart(name, true)
if e != nil {
return "", e
} else {
return fmt.Sprintf("Rebuilt %d services", n.Count), nil
}
},
},
{
Use: "stop [name]",
Short: "Stops service",
Aliases: []string{"kill"},
WaitMsg: "Stopping the service...",
Display: "🛑 Stop",
Do: func(cli client.Client, name string) (string, error) {
n, e := cli.ServiceStop(name)
if e != nil {
return "", e
} else {
return fmt.Sprintf("Stopped %d services", n.Count), nil
}
},
},
}
type unblockMsg struct {
m tea.Msg
}
type updateMsg struct {
session.ServiceMetrics
err error
}
type ServiceDetailModel struct {
entry *ServiceItem
cl client.Client
controlIndex int
busy bool
}
func (m ServiceDetailModel) Init() tea.Cmd {
return func() tea.Msg {
time.Sleep(100 * time.Millisecond)
info, err := m.cl.ServiceMetrics(m.entry.Name)
return updateMsg{info, err}
}
}
func (m ServiceDetailModel) Update(msg tea.Msg) (PageModel, tea.Cmd) {
switch msg := msg.(type) {
case updateMsg:
if msg.err != nil {
return m, ErrMsg(msg.err)
}
m.entry.ServiceMetrics = msg.ServiceMetrics
return m, m.Init()
case unblockMsg:
m.busy = false
return m, tea.Batch(
func() tea.Msg {
return msg.m
},
SetSpinnerState(false),
)
case KeyMatchMsg:
switch msg.Key {
case "esc":
return m, Navigate(MakeServiceListModel(m.cl))
case "right", "l":
m.controlIndex++
if m.controlIndex >= len(ServiceControls) {
m.controlIndex = 0
}
case "left", "h":
m.controlIndex--
if m.controlIndex < 0 {
m.controlIndex = len(ServiceControls) - 1
}
case "enter":
if m.busy {
return m, StatusMsg("Busy")
}
ctrl := ServiceControls[m.controlIndex]
if ctrl.Do != nil {
m.busy = true
return m, tea.Batch(
func() tea.Msg {
msg, err := ctrl.Do(m.cl, m.entry.Name)
if err != nil {
return unblockMsg{ErrMsg(err)}
} else {
return unblockMsg{StatusMsg(msg)}
}
},
SetSpinnerState(true),
)
} else {
return m, ErrMsg("Not implemented")
}
}
}
return m, nil
}
func (m ServiceDetailModel) upstreamViewBasic() [][]Pair {
tbl := [][]Pair{}
for _, u := range m.entry.Server.Upstreams {
state := "Down"
if u.Healthy {
state = "Up"
}
tbl = append(tbl, Pairs(
"State", state,
"Address", u.Address,
"Load", fmt.Sprint(u.LoadFactor),
"Requests", fmt.Sprint(u.RequestCount),
"5xx", fmt.Sprint(u.ServerErrorCount),
"4xx", fmt.Sprint(u.ClientErrorCount),
))
}
return tbl
}
func (m ServiceDetailModel) upstreamView(w int) string {
tbl := table.New().Width(w).
BorderStyle(lipgloss.NewStyle().Foreground(FaintColor))
tbl.StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return lipgloss.NewStyle().Bold(true)
}
return lipgloss.NewStyle().Padding(0, 1)
})
tbl.Headers("🏹 Address", "🔥 Load", "📥 Requests", "🚨 5xx", "😝 4xx")
for _, u := range m.entry.Server.Upstreams {
state := "🔴 "
if u.Healthy {
state = "🟢 "
}
tbl.Row(
state+u.Address,
fmt.Sprint(u.LoadFactor),
fmt.Sprint(u.RequestCount),
fmt.Sprint(u.ServerErrorCount),
fmt.Sprint(u.ClientErrorCount),
)
}
return tbl.Render()
}
func (m ServiceDetailModel) processListViewBasic() [][]Pair {
tbl := [][]Pair{}
for _, p := range m.entry.Processes {
tbl = append(tbl, Pairs(
"PID", fmt.Sprint(p.PID),
"Command", p.BriefCmd(),
"CPU", DisplayFloatWithGran(p.CPU, 100),
"Memory", util.Size(int(p.RSS)).Display(),
"IO", util.Size(int(p.IoRead+p.IoWrite)).Display(),
"Uptime", util.Duration(time.Since(p.CreateTime)).Display(),
))
}
return tbl
}
func (m ServiceDetailModel) processListView(w int) string {
tbl := table.New().Width(w).
BorderStyle(lipgloss.NewStyle().Foreground(FaintColor))
tbl.StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return lipgloss.NewStyle().Bold(true)
}
return lipgloss.NewStyle().Padding(0, 1)
})
sort.Slice(m.entry.Processes, func(i, j int) bool {
return m.entry.Processes[i].CreateTime.Before(m.entry.Processes[j].CreateTime)
})
tbl.Headers("💳 PID", "🎯 Command", "💻 CPU", "🧠 Memory", "💾 IO", "⌚ Uptime")
for _, p := range m.entry.Processes {
tbl.Row(
fmt.Sprint(p.PID),
lipgloss.NewStyle().MaxWidth(20).Render(p.BriefCmd()),
DisplayFloatWithGran(p.CPU, 100),
util.Size(int(p.RSS)).Display(),
util.Size(int(p.IoRead+p.IoWrite)).Display(),
util.Duration(time.Since(p.CreateTime)).Display(),
)
}
return tbl.Render()
}
func (m ServiceDetailModel) buttonsView() string {
var buttons []string
for i, c := range ServiceControls {
style := lipgloss.NewStyle().Width(12).Padding(0, 1).Border(lipgloss.NormalBorder(), true, true, true, true)
if i != m.controlIndex {
style = style.BorderForeground(FaintColor)
}
buttons = append(buttons, style.Render(c.Display))
}
return lipgloss.JoinHorizontal(lipgloss.Top, buttons...)
}
func (m ServiceDetailModel) View(w, h int) string {
status, _ := m.entry.viewStatus()
buttons := m.buttonsView()
padmax := w - lipgloss.Width(status)
padmax -= lipgloss.Width(buttons) + 2
if padmax > 0 {
status = lipgloss.NewStyle().PaddingRight(padmax).Render(status)
}
tblstyle := lipgloss.NewStyle().Margin(1, 0, 1, 0)
return lipgloss.JoinVertical(
lipgloss.Left,
lipgloss.JoinHorizontal(
lipgloss.Top,
status,
buttons,
),
lipgloss.JoinHorizontal(
lipgloss.Top,
tblstyle.Render(m.upstreamView(w/2)),
tblstyle.Render(m.processListView(w/2)),
),
)
}
func (m ServiceDetailModel) Run() error {
u, err := m.cl.ServiceMetrics(m.entry.Name)
if err != nil {
return err
}
m.entry.ServiceMetrics = u
fmt.Println(BasicTable(m.processListViewBasic()))
fmt.Println(BasicTable(m.upstreamViewBasic()))
return nil
}
type ServiceItem struct {
Name string
session.ServiceMetrics
}
func (i ServiceItem) Title() string {
return i.Name + " " + FaintStyle.Render("("+i.Type+")")
}
func (i *ServiceItem) getCPU() string {
var cpu float64
for _, p := range i.Processes {
cpu += p.CPU
}
return DisplayFloatWithGran(cpu, 100)
}
func (i *ServiceItem) getMemory() string {
var mem int
for _, p := range i.Processes {
mem += int(p.RSS)
}
return util.Size(mem).Display()
}
func (i ServiceItem) getErrs() (load, serr, cerr uint64) {
for _, p := range i.Server.Upstreams {
serr += uint64(p.ServerErrorCount + p.ErrorCount)
cerr += uint64(p.ClientErrorCount)
load += uint64(p.LoadFactor)
}
return
}
func (i ServiceItem) viewStatus() (view string, norun bool) {
statusMsg := i.Status
instanceState := ""
// Status
if i.Total != 0 || i.Status != "OK" {
if i.Status == "Down" {
instanceState = fmt.Sprintf("❌ %d/%d", i.Healthy, i.Total)
if i.Err != "" {
return "❌ " + ErrStyle.Render(i.Err), true
} else {
statusMsg = ErrStyle.Render("Down")
}
} else if i.Status == "OK" {
instanceState = fmt.Sprintf("🟢 %d/%d", i.Healthy, i.Total)
statusMsg = OkStyle.Render("OK")
} else {
instanceState = fmt.Sprintf("❓ %d/%d", i.Healthy, i.Total)
}
} else {
if np := len(i.Processes); np == 0 {
return "🧊 Passive", true
} else {
instanceState = fmt.Sprintf("🔨 %d", np)
statusMsg = BrownStyle.Render("OK")
}
}
return lipgloss.JoinVertical(lipgloss.Top, instanceState, statusMsg), false
}
func (i ServiceItem) viewRuntime() (view string) {
rsrcUse := ""
webStatus := ""
if len(i.Processes) > 0 {
rsrcUse += fmt.Sprintf("💻 cpu: %s ", i.getCPU())
rsrcUse += fmt.Sprintf("🧠 mem: %s ", i.getMemory())
}
load, serr, cerr := i.getErrs()
if len(i.Server.Upstreams) != 0 {
webStatus += fmt.Sprintf("⌛ wait: %s ", DisplayUint(load))
webStatus += fmt.Sprintf("😝 4xx: %s ", DisplayUint(cerr))
webStatus += fmt.Sprintf("🚨 5xx: %s", DisplayUint(serr))
}
return lipgloss.JoinVertical(lipgloss.Top, rsrcUse, webStatus)
}
func (i ServiceItem) Description() string {
status, norun := i.viewStatus()
if norun {
return status
}
runtime := i.viewRuntime()
colStyle := lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, true, false, false).
Foreground(FaintColor).
BorderForeground(FaintColor).
MarginRight(2).
Height(2)
leftColStyle := colStyle.Copy().Width(15)
rightColStyle := colStyle.Copy().Width(40).BorderRight(false)
return lipgloss.JoinHorizontal(
lipgloss.Top,
leftColStyle.Render(status),
rightColStyle.Render(runtime),
)
}
func (i ServiceItem) FilterValue() string { return i.Name }
func (i ServiceItem) Entries() (m []Pair) {
load, serr, cerr := i.getErrs()
return Pairs(
"Name", i.Name,
"Type", i.Type,
"Status", i.Status,
"Health", fmt.Sprintf("%d/%d", i.Healthy, i.Total),
"Load", fmt.Sprintf("%d", load),
"4xx", fmt.Sprintf("%d", cerr),
"5xx", fmt.Sprintf("%d", serr),
"CPU", i.getCPU(),
"Mem", i.getMemory(),
)
}
type spinnyModel struct {
spinner.Model
msg string
done <-chan struct{}
}
type spinnyDone struct{}
func (m spinnyModel) Init() tea.Cmd {
return tea.Batch(
m.Model.Tick,
func() tea.Msg {
<-m.done
return spinnyDone{}
},
)
}
func (m spinnyModel) View() string {
return m.Model.View() + " " + m.msg
}
func (m spinnyModel) Update(msg tea.Msg) (mm tea.Model, cmd tea.Cmd) {
switch msg := msg.(type) {
case spinnyDone:
return m, tea.Quit
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
default:
m.Model, cmd = m.Model.Update(msg)
}
return m, cmd
}
func SpinnyAsync(msg string, done <-chan struct{}) {
p := tea.NewProgram(spinnyModel{
Model: spinner.New(
spinner.WithSpinner(spinner.Points),
spinner.WithStyle(SpinnerStyle),
),
msg: msg,
done: done,
})
if _, err := p.Run(); err != nil {
ExitWithError(err)
}
select {
case <-done:
return
default:
ExitWithError("aborted")
}
}
func SpinnyWait[T any](msg string, f func() (T, error)) (res T) {
done := make(chan struct{})
go func() {
var err error
if res, err = f(); err != nil {
ExitWithError(err)
}
close(done)
}()
SpinnyAsync(msg, done)
return
}
func PromptString(msg string, initial string, placeholder string, validate func(s string) error) string {
input := textinput.New(msg)
input.InitialValue = initial
input.Placeholder = placeholder
if validate != nil {
input.Validate = validate
}
res, err := input.RunPrompt()
if err != nil {
ExitWithError(err)
}
return res
}
func PromptSelect(title string, list []string) string {
sp := selection.New("", list)
sp.PageSize = 4
sp.FilterPrompt = title
sp.FilterPlaceholder = ""
choice, err := sp.RunPrompt()
if err != nil {
ExitWithError(err)
}
return choice
}
func PromptSelectValueDesc[V cmp.Ordered](title string, vd map[V]string) V {
keys := lo.Keys(vd)
slices.Sort(keys)
desc := make([]string, len(keys))
maxlen := 0
for i, k := range keys {
desc[i] = fmt.Sprint(k)
maxlen = max(maxlen, len(desc[i]))
}
for i, s := range desc {
desc[i] = "\033[1m" + s + "\033[0m:" + strings.Repeat(" ", 1+maxlen-len(s))
}
for i, k := range keys {
desc[i] = desc[i] + vd[k]
}
sel := PromptSelect(title, desc)
for i := range desc {
if desc[i] == sel {
return keys[i]
}
}
ExitWithError("invalid selection")
panic("unreachable")
}
func PromptSelectService(cl client.Client) string {
mp, err := cl.ServiceMetricsMap()
if err != nil {
ExitWithError(err)
}
return PromptSelect("Pick a service: ", lo.Keys(mp))
}
func MakeServiceListModel(cl client.Client) Bimodel {
del := list.NewDefaultDelegate()
del.SetHeight(3)
return NewList[*ServiceItem](del).
WithTitle("Services").
WithPull(func() ([]*ServiceItem, error) {
services, err := cl.ServiceMetricsMap()
if err != nil {
return nil, err
}
return MapToStableList(services, func(k string, sv session.ServiceMetrics) *ServiceItem {
return &ServiceItem{k, sv}
}), nil
}).
WithThen(func(i *ServiceItem) tea.Model {
if i == nil {
return nil
}
return MakeServiceDetailModel(cl, i)
})
}
func MakeServiceDetailModel(cl client.Client, item *ServiceItem) Bimodel {
return NewPage(ServiceDetailModel{entry: item, cl: cl}, PageProps{
Title: "/" + item.Name,
Keys: []key.Binding{
key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "back to list"),
),
key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "apply"),
),
key.NewBinding(
key.WithKeys("right", "l"),
key.WithHelp("→/l", "next control"),
),
key.NewBinding(
key.WithKeys("left", "h"),
key.WithHelp("←/h", "prev control"),
),
},
})
}