feat(backend): optimize recent sessions filtering

This commit is contained in:
pycook
2025-08-12 15:43:16 +08:00
parent b76519400e
commit ae4e8fb51f
4 changed files with 13 additions and 10 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -13,7 +13,7 @@ func GetProtocolIcon(protocol string) string {
case "mysql":
return "◆"
case "redis":
return ""
return ""
case "mongodb":
return "◉"
case "postgresql":

View File

@@ -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)
}