pico

created pr with 32.1 on 2024-11-12T21:13:35Z · by c8ef7d19
added 32.2 on 2024-11-14T15:42:32Z · by c8ef7d19
1: 7ec3569 = 1: 7ec3569 feat(auth): subscribe to pico's metric-drain pipe
2: 8a197f0 = 2: 8a197f0 chore: update pubsub
3: d4bda15 = 3: d4bda15 refactor: use pipe for analytics
-: ------- > 4: 2b7c358 chore: prep for release
cmds
checkout latest patchset:
ssh pr.pico.sh print 32 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print 32.[rev] | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 32
+33 -1 auth/api.go #
......@@ -1,6 +1,7 @@
11 package auth
22
33 import (
4+ "bufio"
45 "context"
56 "crypto/hmac"
67 "encoding/json"
......@@ -18,6 +19,7 @@ import (
1819 "github.com/picosh/pico/db"
1920 "github.com/picosh/pico/db/postgres"
2021 "github.com/picosh/pico/shared"
22+ "github.com/picosh/pubsub"
2123 "github.com/picosh/utils"
2224 )
2325
......@@ -639,6 +641,31 @@ func handler(routes []shared.Route, client *Client) http.HandlerFunc {
639641 }
640642 }
641643
644+func metricDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger) {
645+ conn := shared.NewPicoPipeClient()
646+ stdoutPipe, err := pubsub.RemoteSub("sub metric-drain -k", ctx, conn)
647+
648+ if err != nil {
649+ logger.Error("could not sub to metric-drain", "err", err)
650+ return
651+ }
652+
653+ scanner := bufio.NewScanner(stdoutPipe)
654+ for scanner.Scan() {
655+ line := scanner.Text()
656+ view := db.AnalyticsVisits{}
657+ err := json.Unmarshal([]byte(line), &view)
658+ if err != nil {
659+ logger.Error("json unmarshal", "err", err)
660+ continue
661+ }
662+ err = dbpool.InsertVisit(&view)
663+ if err != nil {
664+ logger.Error("could not insert view record", "err", err)
665+ }
666+ }
667+}
668+
642669 type AuthCfg struct {
643670 Debug bool
644671 Port string
......@@ -667,6 +694,11 @@ func StartApiServer() {
667694 Logger: logger,
668695 }
669696
697+ ctx := context.Background()
698+ // gather metrics in the auth service
699+ go metricDrainSub(ctx, db, logger)
700+ defer ctx.Done()
701+
670702 routes := createMainRoutes()
671703
672704 if cfg.Debug {
......@@ -679,6 +711,6 @@ func StartApiServer() {
679711 client.Logger.Info("starting server on port", "port", cfg.Port)
680712 err := http.ListenAndServe(portStr, router)
681713 if err != nil {
682- client.Logger.Info(err.Error())
714+ client.Logger.Info("http-serve", "err", err.Error())
683715 }
684716 }
+10 -10 db/db.go #
......@@ -161,16 +161,16 @@ type PostAnalytics struct {
161161 }
162162
163163 type AnalyticsVisits struct {
164- ID string
165- UserID string
166- ProjectID string
167- PostID string
168- Host string
169- Path string
170- IpAddress string
171- UserAgent string
172- Referer string
173- Status int
164+ ID string `json:"id"`
165+ UserID string `json:"user_id"`
166+ ProjectID string `json:"project_id"`
167+ PostID string `json:"post_id"`
168+ Host string `json:"host"`
169+ Path string `json:"path"`
170+ IpAddress string `json:"ip_adress"`
171+ UserAgent string `json:"user_agent"`
172+ Referer string `json:"referer"`
173+ Status int `json:"status"`
174174 }
175175
176176 type VisitInterval struct {
+2 -7 pico/cli.go #
......@@ -70,13 +70,8 @@ func (c *Cmd) notifications() error {
7070 }
7171
7272 func (c *Cmd) logs(ctx context.Context) error {
73- stdoutPipe, err := pipeLogger.ConnectToLogs(ctx, &pipeLogger.PubSubConnectionInfo{
74- RemoteHost: utils.GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"),
75- KeyLocation: utils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"),
76- KeyPassphrase: utils.GetEnv("PICO_PIPE_PASSPHRASE", ""),
77- RemoteHostname: utils.GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"),
78- RemoteUser: utils.GetEnv("PICO_PIPE_USER", "pico"),
79- })
73+ conn := shared.NewPicoPipeClient()
74+ stdoutPipe, err := pipeLogger.ConnectToLogs(ctx, conn)
8075
8176 if err != nil {
8277 return err
+2 -7 shared/config.go #
......@@ -279,13 +279,8 @@ func CreateLogger(space string) *slog.Logger {
279279 newLogger := log
280280
281281 if strings.ToLower(utils.GetEnv("PICO_PIPE_ENABLED", "true")) == "true" {
282- newLog, err := pipeLogger.SendLogRegister(log, &pipeLogger.PubSubConnectionInfo{
283- RemoteHost: utils.GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"),
284- KeyLocation: utils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"),
285- KeyPassphrase: utils.GetEnv("PICO_PIPE_PASSPHRASE", ""),
286- RemoteHostname: utils.GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"),
287- RemoteUser: utils.GetEnv("PICO_PIPE_USER", "pico"),
288- }, 100)
282+ conn := NewPicoPipeClient()
283+ newLog, err := pipeLogger.SendLogRegister(log, conn, 100)
289284
290285 if err == nil {
291286 newLogger = newLog
+16 -0 shared/pubsub.go #
......@@ -0,0 +1,16 @@
1+package shared
2+
3+import (
4+ "github.com/picosh/pubsub"
5+ "github.com/picosh/utils"
6+)
7+
8+func NewPicoPipeClient() *pubsub.RemoteClientInfo {
9+ return &pubsub.RemoteClientInfo{
10+ RemoteHost: utils.GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"),
11+ KeyLocation: utils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"),
12+ KeyPassphrase: utils.GetEnv("PICO_PIPE_PASSPHRASE", ""),
13+ RemoteHostname: utils.GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"),
14+ RemoteUser: utils.GetEnv("PICO_PIPE_USER", "pico"),
15+ }
16+}
+3 -8 tui/logs/logs.go #
......@@ -11,6 +11,7 @@ import (
1111 "github.com/charmbracelet/bubbles/viewport"
1212 tea "github.com/charmbracelet/bubbletea"
1313 "github.com/charmbracelet/lipgloss"
14+ "github.com/picosh/pico/shared"
1415 "github.com/picosh/pico/tui/common"
1516 "github.com/picosh/pico/tui/pages"
1617 "github.com/picosh/utils"
......@@ -170,14 +171,8 @@ func (m Model) waitForActivity(sub chan map[string]any) tea.Cmd {
170171
171172 func (m Model) connectLogs(sub chan map[string]any) tea.Cmd {
172173 return func() tea.Msg {
173- stdoutPipe, err := pipeLogger.ConnectToLogs(m.ctx, &pipeLogger.PubSubConnectionInfo{
174- RemoteHost: utils.GetEnv("PICO_PIPE_ENDPOINT", "pipe.pico.sh:22"),
175- KeyLocation: utils.GetEnv("PICO_PIPE_KEY", "ssh_data/term_info_ed25519"),
176- KeyPassphrase: utils.GetEnv("PICO_PIPE_PASSPHRASE", ""),
177- RemoteHostname: utils.GetEnv("PICO_PIPE_REMOTE_HOST", "pipe.pico.sh"),
178- RemoteUser: utils.GetEnv("PICO_PIPE_USER", "pico"),
179- })
180-
174+ conn := shared.NewPicoPipeClient()
175+ stdoutPipe, err := pipeLogger.ConnectToLogs(m.ctx, conn)
181176 if err != nil {
182177 return errMsg(err)
183178 }
Back to top