repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
042e4a4
parent
8d9ea91
author
Antonio Mika
date
2025-03-21 16:31:33 -0400 EDT
Fixes races and cleanup rogue print
4 files changed,  +25, -6
M pkg/db/postgres/storage.go
+0, -1
1@@ -1810,7 +1810,6 @@ func (me *PsqlDB) InsertTunsEventLog(log *db.TunsEventLog) error {
2 
3 func (me *PsqlDB) FindTunsEventLogsByAddr(userID, addr string) ([]*db.TunsEventLog, error) {
4 	logs := []*db.TunsEventLog{}
5-	fmt.Println(addr)
6 	rs, err := me.Db.Query(
7 		`SELECT id, user_id, server_id, remote_addr, event_type, tunnel_type, connection_type, tunnel_id, created_at
8 		FROM tuns_event_logs WHERE user_id=$1 AND tunnel_id=$2 ORDER BY created_at DESC`, userID, addr)
M pkg/tui/analytics.go
+1, -1
1@@ -103,7 +103,7 @@ func (m *AnalyticsPage) HandleEvent(ev vaxis.Event, phase vxfw.EventPhase) (vxfw
2 		if msg.Matches('t') {
3 			enabled, err := m.toggleAnalytics()
4 			if err != nil {
5-				fmt.Println(err)
6+				m.err = err
7 			}
8 			var wdgt vxfw.Widget = m
9 			if enabled {
M pkg/tui/tuns.go
+21, -1
 1@@ -8,6 +8,7 @@ import (
 2 	"math"
 3 	"net/http"
 4 	"sort"
 5+	"sync"
 6 	"time"
 7 
 8 	"git.sr.ht/~rockorager/vaxis"
 9@@ -98,6 +99,8 @@ type TunsPage struct {
10 	isAdmin      bool
11 	eventLogs    []*db.TunsEventLog
12 	eventLogList list.Dynamic
13+
14+	mu sync.RWMutex
15 }
16 
17 func NewTunsPage(shrd *SharedModel) *TunsPage {
18@@ -149,6 +152,8 @@ func (m *TunsPage) getLogWidget(i uint, cursor uint) vxfw.Widget {
19 }
20 
21 func (m *TunsPage) getEventLogWidget(i uint, cursor uint) vxfw.Widget {
22+	m.mu.RLock()
23+	defer m.mu.RUnlock()
24 	if int(i) >= len(m.eventLogs) {
25 		return nil
26 	}
27@@ -201,7 +206,11 @@ func (m *TunsPage) connectToLogs() error {
28 		user := parsedData.User
29 		userId := parsedData.UserId
30 		isUser := user == m.shared.User.Name || userId == m.shared.User.ID
31-		if (m.isAdmin || isUser) && parsedData.TunnelID == m.selected {
32+
33+		m.mu.RLock()
34+		selected := m.selected
35+		m.mu.RUnlock()
36+		if (m.isAdmin || isUser) && parsedData.TunnelID == selected {
37 			m.shared.App.PostEvent(ResultLogLineLoaded{parsedData})
38 		}
39 	}
40@@ -232,9 +241,11 @@ func (m *TunsPage) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command
41 		m.focus = "page"
42 		return vxfw.FocusWidgetCmd(m), nil
43 	case PageOut:
44+		m.mu.Lock()
45 		m.selected = ""
46 		m.logs = []*ResultLog{}
47 		m.err = nil
48+		m.mu.Unlock()
49 		m.done()
50 	case ResultLogLineLoaded:
51 		m.logs = append(m.logs, &msg.Line)
52@@ -253,9 +264,11 @@ func (m *TunsPage) HandleEvent(ev vaxis.Event, ph vxfw.EventPhase) (vxfw.Command
53 		}), nil
54 	case vaxis.Key:
55 		if msg.Matches(vaxis.KeyEnter) {
56+			m.mu.Lock()
57 			m.selected = m.tuns[m.leftPane.Cursor()].TunAddress
58 			m.logs = []*ResultLog{}
59 			m.eventLogs = []*db.TunsEventLog{}
60+			m.mu.Unlock()
61 			go m.fetchEventLogs()
62 			return vxfw.RedrawCmd{}, nil
63 		}
64@@ -391,12 +404,14 @@ func (m *TunsPage) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
65 		root.AddChild(int(leftPaneW), 0, pagerSurf)
66 	}
67 
68+	m.mu.RLock()
69 	if m.err != nil {
70 		txt := text.New(m.err.Error())
71 		txt.Style = vaxis.Style{Foreground: red}
72 		surf, _ := txt.Draw(createDrawCtx(ctx, 1))
73 		root.AddChild(0, int(ctx.Max.Height-1), surf)
74 	}
75+	m.mu.RUnlock()
76 
77 	return root, nil
78 }
79@@ -431,9 +446,14 @@ func fetch(fqdn, auth string) (map[string]*TunsClient, error) {
80 func (m *TunsPage) fetchEventLogs() {
81 	logs, err := m.shared.Dbpool.FindTunsEventLogsByAddr(m.shared.User.ID, m.selected)
82 	if err != nil {
83+		m.mu.Lock()
84+		defer m.mu.Unlock()
85 		m.err = err
86 		return
87 	}
88+
89+	m.mu.Lock()
90+	defer m.mu.Unlock()
91 	m.eventLogs = logs
92 }
93 
M pkg/tui/ui.go
+3, -3
 1@@ -195,15 +195,15 @@ func (m *HeaderWdgt) Draw(ctx vxfw.DrawContext) (vxfw.Surface, error) {
 2 	root := vxfw.NewSurface(ctx.Max.Width, ctx.Max.Height, m)
 3 	// header
 4 	wdgt := richtext.New([]vaxis.Segment{
 5-		vaxis.Segment{Text: " " + logoTxt + " ", Style: vaxis.Style{Background: purp, Foreground: black}},
 6-		vaxis.Segment{Text: " • " + m.page, Style: vaxis.Style{Foreground: green}},
 7+		{Text: " " + logoTxt + " ", Style: vaxis.Style{Background: purp, Foreground: black}},
 8+		{Text: " • " + m.page, Style: vaxis.Style{Foreground: green}},
 9 	})
10 	surf, _ := wdgt.Draw(ctx)
11 	root.AddChild(0, 0, surf)
12 
13 	if m.shared.User != nil {
14 		user := richtext.New([]vaxis.Segment{
15-			vaxis.Segment{Text: "~" + m.shared.User.Name, Style: vaxis.Style{Foreground: cream}},
16+			{Text: "~" + m.shared.User.Name, Style: vaxis.Style{Foreground: cream}},
17 		})
18 		surf, _ = user.Draw(ctx)
19 		root.AddChild(int(ctx.Max.Width)-int(surf.Size.Width)-1, 0, surf)