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
|
diff --git a/pkg/tui/tuns.go b/pkg/tui/tuns.go
index 8224e09..a632ec4 100644
--- a/pkg/tui/tuns.go
+++ b/pkg/tui/tuns.go
@@ -15,6 +15,7 @@ import (
"git.sr.ht/~rockorager/vaxis/vxfw/list"
"git.sr.ht/~rockorager/vaxis/vxfw/richtext"
"git.sr.ht/~rockorager/vaxis/vxfw/text"
+ "github.com/picosh/pico/pkg/db"
"github.com/picosh/pico/pkg/shared"
"github.com/picosh/utils/pipe"
)
@@ -78,21 +79,25 @@ type ResultLogLineLoaded struct {
type TunsLoaded struct{}
+type EventLogsLoaded struct{}
+
type TunsPage struct {
shared *SharedModel
- loading bool
- err error
- tuns []TunsClientSimple
- selected string
- focus string
- leftPane list.Dynamic
- rightPane *Pager
- logs []*ResultLog
- logList list.Dynamic
- ctx context.Context
- done context.CancelFunc
- isAdmin bool
+ loading bool
+ err error
+ tuns []TunsClientSimple
+ selected string
+ focus string
+ leftPane list.Dynamic
+ rightPane *Pager
+ logs []*ResultLog
+ logList list.Dynamic
+ ctx context.Context
+ done context.CancelFunc
+ isAdmin bool
+ eventLogs []*db.TunsEventLog
+ eventLogList list.Dynamic
}
func NewTunsPage(shrd *SharedModel) *TunsPage {
@@ -103,6 +108,7 @@ func NewTunsPage(shrd *SharedModel) *TunsPage {
}
m.leftPane = list.Dynamic{DrawCursor: true, Builder: m.getLeftWidget}
m.logList = list.Dynamic{DrawCursor: true, Builder: m.getLogWidget}
+ m.eventLogList = list.Dynamic{DrawCursor: true, Builder: m.getEventLogWidget}
ff, _ := shrd.Dbpool.FindFeatureForUser(m.shared.User.ID, "admin")
if ff != nil {
m.isAdmin = true
@@ -145,6 +151,24 @@ func (m *TunsPage) getLogWidget(i uint, cursor uint) vxfw.Widget {
return txt
}
+func (m *TunsPage) getEventLogWidget(i uint, cursor uint) vxfw.Widget {
+ if int(i) >= len(m.tuns) {
+ return nil
+ }
+
+ log := m.eventLogs[i]
+ style := vaxis.Style{Foreground: green}
+ if log.EventType == "disconnect" {
+ style = vaxis.Style{Foreground: red}
+ }
+ txt := richtext.New([]vaxis.Segment{
+ {Text: log.CreatedAt.Format(time.RFC3339) + " "},
+ {Text: log.EventType, Style: style},
+ })
+ txt.Softwrap = false
+ return txt
+}
+
func (m *TunsPage) connectToLogs() error {
ctx, cancel := context.WithCancel(m.shared.Session.Context())
m.ctx = ctx
@@ -208,6 +232,8 @@ func (m *TunsPage) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command
m.logList.SetCursor(uint(len(m.logs) - 1))
}
return vxfw.RedrawCmd{}, nil
+ case EventLogsLoaded:
+ return vxfw.RedrawCmd{}, nil
case TunsLoaded:
m.focus = "tuns"
return vxfw.BatchCmd([]vxfw.Command{
@@ -218,6 +244,8 @@ func (m *TunsPage) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command
if msg.Matches(vaxis.KeyEnter) {
m.selected = m.tuns[m.leftPane.Cursor()].TunAddress
m.logs = []*ResultLog{}
+ m.eventLogs = []*db.TunsEventLog{}
+ go m.fetchEventLogs()
return vxfw.RedrawCmd{}, nil
}
if msg.Matches(vaxis.KeyTab) {
@@ -325,7 +353,20 @@ func (m *TunsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
Characters: vaxis.Characters,
Max: vxfw.Size{
Width: uint16(rightPaneW) - 4,
- Height: ctx.Max.Height - uint16(ah) - 3,
+ Height: 15,
+ },
+ })
+ rightSurf.AddChild(0, ah, surf)
+ ah += int(surf.Size.Height)
+
+ brd = NewBorder(&m.eventLogList)
+ brd.Label = "conn events"
+ m.focusBorder(brd)
+ surf, _ = brd.Draw(vxfw.DrawContext{
+ Characters: vaxis.Characters,
+ Max: vxfw.Size{
+ Width: uint16(rightPaneW) - 4,
+ Height: 15,
},
})
rightSurf.AddChild(0, ah, surf)
@@ -376,6 +417,15 @@ func fetch(fqdn, auth string) (map[string]*TunsClient, error) {
return data.Clients, nil
}
+func (m *TunsPage) fetchEventLogs() {
+ logs, err := m.shared.Dbpool.FindTunsEventLog(m.shared.User.ID, m.selected)
+ if err != nil {
+ m.err = err
+ return
+ }
+ m.eventLogs = logs
+}
+
func (m *TunsPage) fetchTuns() {
tMap, err := fetch("tuns.sh", m.shared.Cfg.TunsSecret)
if err != nil {
|