Brijesh's Git Server — navigator-v2 @ 19f964e87980aeb2d2f6a016091d7bfe1bdb3732

api/internal/database/browser.go (view raw)

 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
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
package database

import (
	"context"
	"database/sql"
	"errors"

	"github.com/google/uuid"
)

func (s *service) CreateBrowserSession(ctx context.Context, session *BrowserSession) error {
	query := `
	INSERT INTO browser_sessions
	(id, display_number, vnc_port, debug_port, status, error_message, xvfb_pid, chrome_pid, vnc_pid)
	VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
			`
	_, err := s.db.ExecContext(ctx, query,
		session.ID,
		session.DisplayNumber,
		session.VNCPort,
		session.DebugPort,
		session.Status,
		session.ErrorMessage,
		session.XvfbPID,
		session.ChromePID,
		session.VNCPID,
	)
	return err
}

func (s *service) GetBrowserSession(ctx context.Context, id uuid.UUID) (*BrowserSession, error) {
	query := `
		SELECT id, display_number, vnc_port, debug_port, status, created_at, updated_at,
									error_message, xvfb_pid, chrome_pid, vnc_pid
		FROM browser_sessions
		WHERE id = $1
			`

	session := &BrowserSession{}
	err := s.db.QueryRowContext(ctx, query, id).Scan(
		&session.ID,
		&session.DisplayNumber,
		&session.VNCPort,
		&session.DebugPort,
		&session.Status,
		&session.CreatedAt,
		&session.UpdatedAt,
		&session.ErrorMessage,
		&session.XvfbPID,
		&session.ChromePID,
		&session.VNCPID,
	)

	if err == sql.ErrNoRows {
		return nil, errors.New("session not found")
	}
	return session, err
}

func (s *service) ListBrowserSessions(ctx context.Context) ([]*BrowserSession, error) {
	query := `
		SELECT id, display_number, vnc_port, debug_port, status, created_at, updated_at,
									error_message, xvfb_pid, chrome_pid, vnc_pid
		FROM browser_sessions
		WHERE status != $1
		ORDER BY created_at DESC
			`

	rows, err := s.db.QueryContext(ctx, query, StatusStopped)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var sessions []*BrowserSession
	for rows.Next() {
		session := &BrowserSession{}
		err := rows.Scan(
			&session.ID,
			&session.DisplayNumber,
			&session.VNCPort,
			&session.DebugPort,
			&session.Status,
			&session.CreatedAt,
			&session.UpdatedAt,
			&session.ErrorMessage,
			&session.XvfbPID,
			&session.ChromePID,
			&session.VNCPID,
		)
		if err != nil {
			return nil, err
		}
		sessions = append(sessions, session)
	}
	return sessions, nil
}

func (s *service) UpdateBrowserSession(ctx context.Context, session *BrowserSession) error {
	query := `
		UPDATE browser_sessions
		SET display_number = $1,
						vnc_port = $2,
						debug_port = $3,
						status = $4,
						error_message = $5,
						xvfb_pid = $6,
						chrome_pid = $7,
						vnc_pid = $8
		WHERE id = $9
			`

	result, err := s.db.ExecContext(ctx, query,
		session.DisplayNumber,
		session.VNCPort,
		session.DebugPort,
		session.Status,
		session.ErrorMessage,
		session.XvfbPID,
		session.ChromePID,
		session.VNCPID,
		session.ID,
	)
	if err != nil {
		return err
	}

	rows, err := result.RowsAffected()
	if err != nil {
		return err
	}
	if rows == 0 {
		return errors.New("session not found")
	}
	return nil
}

func (s *service) DeleteBrowserSession(ctx context.Context, id uuid.UUID) error {
	query := `DELETE FROM browser_sessions WHERE id = $1`
	result, err := s.db.ExecContext(ctx, query, id)
	if err != nil {
		return err
	}

	rows, err := result.RowsAffected()
	if err != nil {
		return err
	}
	if rows == 0 {
		return errors.New("session not found")
	}
	return nil
}

func (s *service) GetNextAvailableDisplayNumber(ctx context.Context) (int, error) {
	query := `
		WITH RECURSIVE numbers AS (
						SELECT 1 as num
						UNION ALL
						SELECT num + 1
						FROM numbers
						WHERE num < 100  -- Set a reasonable upper limit
		)
		SELECT num
		FROM numbers
		WHERE num NOT IN (
						SELECT display_number
						FROM browser_sessions
						WHERE status != $1
		)
		ORDER BY num
		LIMIT 1
			`

	var displayNumber int
	err := s.db.QueryRowContext(ctx, query, StatusStopped).Scan(&displayNumber)
	if err != nil {
		return 0, err
	}
	return displayNumber, nil
}