From ae4e8fb51f4a59e7db9b05e5fccbfa348740edb7 Mon Sep 17 00:00:00 2001 From: pycook Date: Tue, 12 Aug 2025 15:43:16 +0800 Subject: [PATCH] feat(backend): optimize recent sessions filtering --- backend/internal/repository/session.go | 4 ++++ backend/internal/sshsrv/colors/theme.go | 2 +- backend/internal/sshsrv/icons/icons.go | 2 +- backend/internal/sshsrv/view.go | 15 +++++++-------- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/backend/internal/repository/session.go b/backend/internal/repository/session.go index b86957c..42180cf 100644 --- a/backend/internal/repository/session.go +++ b/backend/internal/repository/session.go @@ -194,6 +194,8 @@ func (r *sessionRepository) GetRecentSessionsByUser(ctx context.Context, uid int Where("uid = ?", uid). Where("asset_id > 0"). Where("account_id > 0"). + Where("protocol NOT LIKE ?", "rdp%"). + Where("protocol NOT LIKE ?", "vnc%"). Group("asset_id, account_id"). Find(&maxSessions).Error @@ -214,6 +216,8 @@ func (r *sessionRepository) GetRecentSessionsByUser(ctx context.Context, uid int // Get the full session records for those IDs err = dbpkg.DB.Model(&model.Session{}). Where("id IN ?", sessionIds). + Where("protocol NOT LIKE ?", "rdp%"). + Where("protocol NOT LIKE ?", "vnc%"). Order("created_at DESC"). Limit(limit). Find(&sessions).Error diff --git a/backend/internal/sshsrv/colors/theme.go b/backend/internal/sshsrv/colors/theme.go index 4cfa682..063ec32 100644 --- a/backend/internal/sshsrv/colors/theme.go +++ b/backend/internal/sshsrv/colors/theme.go @@ -30,7 +30,7 @@ var ( // Protocol-specific colors (using primary palette) SSHColor = PrimaryColor9 // Bright blue for SSH MySQLColor = PrimaryColor // Deep blue for MySQL - RedisColor = lipgloss.Color("#DC382D") // Keep Redis brand red + RedisColor = lipgloss.Color("#9C27B0") // Purple for Redis MongoDBColor = lipgloss.Color("#4DB33D") // Keep MongoDB brand green PostgreSQLColor = PrimaryColor2 // Light blue for PostgreSQL TelnetColor = PrimaryColor8 // Soft blue for Telnet diff --git a/backend/internal/sshsrv/icons/icons.go b/backend/internal/sshsrv/icons/icons.go index cfb2b9c..9dcc5d6 100644 --- a/backend/internal/sshsrv/icons/icons.go +++ b/backend/internal/sshsrv/icons/icons.go @@ -13,7 +13,7 @@ func GetProtocolIcon(protocol string) string { case "mysql": return "◆" case "redis": - return "⚡" + return "●" case "mongodb": return "◉" case "postgresql": diff --git a/backend/internal/sshsrv/view.go b/backend/internal/sshsrv/view.go index b851bc2..6825e14 100644 --- a/backend/internal/sshsrv/view.go +++ b/backend/internal/sshsrv/view.go @@ -120,12 +120,12 @@ func initialView(ctx *gin.Context, sess ssh.Session, r io.ReadCloser, w io.Write ti.Cursor.Style = colors.AccentStyle // Disable Tab for AcceptSuggestion to handle it ourselves ti.KeyMap.AcceptSuggestion = key.NewBinding(key.WithKeys("ctrl+x")) // Use a key that won't be pressed - + // Initialize spinner s := spinner.New() s.Spinner = spinner.Dot s.Style = colors.PrimaryStyle - + v := view{ Ctx: ctx, Sess: sess, @@ -163,7 +163,7 @@ func (m *view) Update(msg tea.Msg) (tea.Model, tea.Cmd) { tableCmd tea.Cmd spinnerCmd tea.Cmd ) - + // Update spinner if connecting if m.connecting { m.spinner, spinnerCmd = m.spinner.Update(msg) @@ -265,7 +265,7 @@ func (m *view) Update(msg tea.Msg) (tea.Model, tea.Cmd) { height = 24 } - // Get recent sessions + // Get recent sessions (filtered at database level) sessions, err := m.getRecentSessions() if err != nil { return m, tea.Sequence( @@ -589,15 +589,15 @@ func (m *view) handleConnectionCommand(cmd string) tea.Cmd { m.connecting = true return tea.Sequence( - tea.Printf("\n %s %s\n", - colors.PrimaryStyle.Render("⚡"), + tea.Printf("\n %s %s\n", + colors.PrimaryStyle.Render("⚡"), colors.AccentStyle.Render(fmt.Sprintf("Initiating secure connection to %s", cmd))), // Start spinner and connection in background m.spinner.Tick, tea.Exec(&connector{Ctx: newCtx, Sess: m.Sess, Vw: m, gctx: m.gctx}, func(err error) tea.Msg { m.connecting = false if err != nil { - return errMsg(fmt.Errorf("%s Connection failed: %v", + return errMsg(fmt.Errorf("%s Connection failed: %v", colors.ErrorStyle.Render("✗"), err)) } return nil @@ -744,7 +744,6 @@ func (m *view) magicn() tea.Msg { } func (m *view) getRecentSessions() ([]*model.Session, error) { - // Use repository to get recent sessions, deduplicated by asset_id and account_id sessionRepo := repository.NewSessionRepository() return sessionRepo.GetRecentSessionsByUser(m.gctx, m.currentUser.GetUid(), 20) }