remove screens from session. add screenlinestype (replaces 'window' lines)

This commit is contained in:
sawka 2023-03-13 10:50:29 -07:00
parent fd33157130
commit 1059a10727
5 changed files with 67 additions and 66 deletions

View File

@ -194,19 +194,20 @@ func HandleLogActiveState(w http.ResponseWriter, r *http.Request) {
}
// params: screenid
func HandleGetFullScreen(w http.ResponseWriter, r *http.Request) {
func HandleGetScreenLines(w http.ResponseWriter, r *http.Request) {
qvals := r.URL.Query()
screenId := qvals.Get("screenid")
if _, err := uuid.Parse(screenId); err != nil {
WriteJsonError(w, fmt.Errorf("invalid screenid: %w", err))
return
}
screen, err := sstore.GetFullScreenById(r.Context(), screenId)
screenLines, err := sstore.GetScreenLinesById(r.Context(), screenId)
if err != nil {
WriteJsonError(w, err)
return
}
WriteJsonSuccess(w, screen)
update := &sstore.ModelUpdate{ScreenLines: screenLines}
WriteJsonSuccess(w, update)
return
}
@ -562,7 +563,7 @@ func main() {
gr.HandleFunc("/api/ptyout", AuthKeyWrap(HandleGetPtyOut))
gr.HandleFunc("/api/remote-pty", AuthKeyWrap(HandleRemotePty))
gr.HandleFunc("/api/rtnstate", AuthKeyWrap(HandleRtnState))
gr.HandleFunc("/api/get-full-screen", AuthKeyWrap(HandleGetFullScreen))
gr.HandleFunc("/api/get-screen-lines", AuthKeyWrap(HandleGetScreenLines))
gr.HandleFunc("/api/run-command", AuthKeyWrap(HandleRunCommand)).Methods("POST")
gr.HandleFunc("/api/get-client-data", AuthKeyWrap(HandleGetClientData))
gr.HandleFunc("/api/set-winsize", AuthKeyWrap(HandleSetWinSize))

View File

@ -461,13 +461,8 @@ func ScreenArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType)
if err != nil {
return nil, fmt.Errorf("/screen:archive cannot get updated screen obj: %v", err)
}
bareSession, err := sstore.GetBareSessionById(ctx, ids.SessionId)
if err != nil {
return nil, fmt.Errorf("/screen:archive cannot retrieve updated session obj: %v", err)
}
bareSession.Screens = append(bareSession.Screens, screen)
update := sstore.ModelUpdate{
Sessions: []*sstore.SessionType{bareSession},
Screens: []*sstore.ScreenType{screen},
}
return update, nil
}

View File

@ -395,27 +395,17 @@ func GetBareSessionById(ctx context.Context, sessionId string) (*SessionType, er
}
func GetAllSessions(ctx context.Context) (*ModelUpdate, error) {
var rtn []*SessionType
var activeSessionId string
txErr := WithTx(ctx, func(tx *TxWrap) error {
return WithTxRtn(ctx, func(tx *TxWrap) (*ModelUpdate, error) {
update := &ModelUpdate{}
query := `SELECT * FROM session ORDER BY archived, sessionidx, archivedts`
tx.Select(&rtn, query)
tx.Select(&update.Sessions, query)
sessionMap := make(map[string]*SessionType)
for _, session := range rtn {
for _, session := range update.Sessions {
sessionMap[session.SessionId] = session
session.Full = true
}
query = `SELECT * FROM screen ORDER BY archived, screenidx, archivedts`
screens := SelectMapsGen[*ScreenType](tx, query)
screenMap := make(map[string][]*ScreenType)
for _, screen := range screens {
screenArr := screenMap[screen.SessionId]
screenArr = append(screenArr, screen)
screenMap[screen.SessionId] = screenArr
}
for _, session := range rtn {
session.Screens = screenMap[session.SessionId]
}
update.Screens = SelectMapsGen[*ScreenType](tx, query)
query = `SELECT * FROM remote_instance`
riArr := SelectMapsGen[*RemoteInstance](tx, query)
for _, ri := range riArr {
@ -425,19 +415,15 @@ func GetAllSessions(ctx context.Context) (*ModelUpdate, error) {
}
}
query = `SELECT activesessionid FROM client`
activeSessionId = tx.GetString(query)
return nil
update.ActiveSessionId = tx.GetString(query)
return update, nil
})
if txErr != nil {
return nil, txErr
}
return &ModelUpdate{Sessions: rtn, ActiveSessionId: activeSessionId}, nil
}
func GetFullScreenById(ctx context.Context, screenId string) (*ScreenType, error) {
return WithTxRtn(ctx, func(tx *TxWrap) (*ScreenType, error) {
query := `SELECT * FROM screen WHERE screenid = ?`
screen := GetMapGen[*ScreenType](tx, query, screenId)
func GetScreenLinesById(ctx context.Context, screenId string) (*ScreenLinesType, error) {
return WithTxRtn(ctx, func(tx *TxWrap) (*ScreenLinesType, error) {
query := `SELECT sessionid, screenid, windowid FROM screen WHERE screenid = ?`
screen := GetMappable[*ScreenLinesType](tx, query, screenId)
if screen == nil {
return nil, nil
}
@ -445,7 +431,6 @@ func GetFullScreenById(ctx context.Context, screenId string) (*ScreenType, error
tx.Select(&screen.Lines, query, screen.SessionId, screen.WindowId)
query = `SELECT * FROM cmd WHERE cmdid IN (SELECT cmdid FROM line WHERE sessionid = ? AND windowid = ?)`
screen.Cmds = SelectMapsGen[*CmdType](tx, query, screen.SessionId, screen.WindowId)
screen.Full = true
return screen, nil
})
}
@ -678,12 +663,15 @@ func InsertScreen(ctx context.Context, sessionId string, origScreenName string,
if err != nil {
return nil, err
}
update := ModelUpdate{Screens: []*ScreenType{newScreen}}
if activate {
bareSession, err := GetBareSessionById(ctx, sessionId)
if err != nil {
return nil, err
return nil, txErr
}
bareSession.Screens = append(bareSession.Screens, newScreen)
return ModelUpdate{Sessions: []*SessionType{bareSession}}, nil
update.Sessions = []*SessionType{bareSession}
}
return update, nil
}
func GetScreenById(ctx context.Context, screenId string) (*ScreenType, error) {
@ -975,6 +963,7 @@ func CleanWindow(sessionId string, windowId string) {
}
func ArchiveScreen(ctx context.Context, sessionId string, screenId string) (UpdatePacket, error) {
var isActive bool
txErr := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT screenid FROM screen WHERE sessionid = ? AND screenid = ?`
if !tx.Exists(query, sessionId, screenId) {
@ -992,7 +981,7 @@ func ArchiveScreen(ctx context.Context, sessionId string, screenId string) (Upda
}
query = `UPDATE screen SET archived = 1, archivedts = ?, screenidx = 0 WHERE sessionid = ? AND screenid = ?`
tx.Exec(query, time.Now().UnixMilli(), sessionId, screenId)
isActive := tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, sessionId, screenId)
isActive = tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, sessionId, screenId)
if isActive {
screenIds := tx.SelectStrings(`SELECT screenid FROM screen WHERE sessionid = ? AND NOT archived ORDER BY screenidx`, sessionId)
nextId := getNextId(screenIds, screenId)
@ -1003,14 +992,17 @@ func ArchiveScreen(ctx context.Context, sessionId string, screenId string) (Upda
if txErr != nil {
return nil, txErr
}
newScreen, err := GetScreenById(ctx, screenId)
if err != nil {
return nil, fmt.Errorf("cannot retrive archived screen: %w", err)
}
update := ModelUpdate{Screens: []*ScreenType{newScreen}}
if isActive {
bareSession, err := GetBareSessionById(ctx, sessionId)
if err != nil {
return nil, txErr
return nil, err
}
update := ModelUpdate{Sessions: []*SessionType{bareSession}}
newScreen, _ := GetScreenById(ctx, screenId)
if newScreen != nil {
bareSession.Screens = append(bareSession.Screens, newScreen)
update.Sessions = []*SessionType{bareSession}
}
return update, nil
}
@ -1031,6 +1023,7 @@ func UnArchiveScreen(ctx context.Context, sessionId string, screenId string) err
func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) {
var swkeys SWKeys
var isActive bool
txErr := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT screenid FROM screen WHERE screenid = ?`
if !tx.Exists(query, screenId) {
@ -1046,7 +1039,7 @@ func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) {
if numScreens <= 1 {
return fmt.Errorf("cannot purge the last screen in a session")
}
isActive := tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, swkeys.SessionId, screenId)
isActive = tx.Exists(`SELECT sessionid FROM session WHERE sessionid = ? AND activescreenid = ?`, swkeys.SessionId, screenId)
if isActive {
screenIds := tx.SelectStrings(`SELECT screenid FROM screen WHERE sessionid = ? AND NOT archived ORDER BY screenidx`, swkeys.SessionId)
nextId := getNextId(screenIds, screenId)
@ -1060,12 +1053,15 @@ func DeleteScreen(ctx context.Context, screenId string) (UpdatePacket, error) {
return nil, txErr
}
go CleanWindow(swkeys.SessionId, swkeys.WindowId)
update := ModelUpdate{}
update.Screens = []*ScreenType{&ScreenType{SessionId: swkeys.SessionId, ScreenId: screenId, Remove: true}}
if isActive {
bareSession, err := GetBareSessionById(ctx, swkeys.SessionId)
if err != nil {
return nil, err
}
bareSession.Screens = append(bareSession.Screens, &ScreenType{SessionId: swkeys.SessionId, ScreenId: screenId, Remove: true})
update := ModelUpdate{Sessions: []*SessionType{bareSession}}
update.Sessions = []*SessionType{bareSession}
}
return update, nil
}
@ -1308,11 +1304,11 @@ func ArchiveScreenLines(ctx context.Context, screenId string) (*ModelUpdate, err
if txErr != nil {
return nil, txErr
}
screen, err := GetFullScreenById(ctx, screenId)
screenLines, err := GetScreenLinesById(ctx, screenId)
if err != nil {
return nil, err
}
return &ModelUpdate{Screens: []*ScreenType{screen}}, nil
return &ModelUpdate{ScreenLines: screenLines}, nil
}
func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error) {
@ -1338,7 +1334,11 @@ func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error
return nil, txErr
}
go cleanSessionCmds(context.Background(), swkeys.SessionId)
screen, err := GetFullScreenById(ctx, screenId)
screen, err := GetScreenById(ctx, screenId)
if err != nil {
return nil, err
}
screenLines, err := GetScreenLinesById(ctx, screenId)
if err != nil {
return nil, err
}
@ -1349,9 +1349,9 @@ func PurgeScreenLines(ctx context.Context, screenId string) (*ModelUpdate, error
LineId: lineId,
Remove: true,
}
screen.Lines = append(screen.Lines, line)
screenLines.Lines = append(screenLines.Lines, line)
}
return &ModelUpdate{Screens: []*ScreenType{screen}}, nil
return &ModelUpdate{Screens: []*ScreenType{screen}, ScreenLines: screenLines}, nil
}
func GetRunningWindowCmds(ctx context.Context, sessionId string, windowId string) ([]*CmdType, error) {

View File

@ -202,7 +202,7 @@ type ClientData struct {
FeOpts FeOptsType `json:"feopts"`
}
func (c ClientData) UseDBMap() {}
func (ClientData) UseDBMap() {}
type CloudAclType struct {
UserId string `json:"userid"`
@ -218,7 +218,6 @@ type SessionType struct {
NotifyNum int64 `json:"notifynum"`
Archived bool `json:"archived,omitempty"`
ArchivedTs int64 `json:"archivedts,omitempty"`
Screens []*ScreenType `json:"screens"`
Remotes []*RemoteInstance `json:"remotes"`
// only for updates
@ -387,6 +386,16 @@ type SWKeys struct {
WindowId string
}
type ScreenLinesType struct {
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
WindowId string `json:"windowid"`
Lines []*LineType `json:"lines" dbmap:"-"`
Cmds []*CmdType `json:"cmds" dbmap:"-"`
}
func (ScreenLinesType) UseDBMap() {}
type ScreenType struct {
SessionId string `json:"sessionid"`
ScreenId string `json:"screenid"`
@ -404,13 +413,8 @@ type ScreenType struct {
Archived bool `json:"archived,omitempty"`
ArchivedTs int64 `json:"archivedts,omitempty"`
// only for "full"
Lines []*LineType `json:"lines"`
Cmds []*CmdType `json:"cmds"`
// only for updates
Remove bool `json:"remove,omitempty"`
Full bool `json:"full,omitempty"`
}
func (s *ScreenType) ToMap() map[string]interface{} {

View File

@ -33,6 +33,7 @@ type ModelUpdate struct {
Sessions []*SessionType `json:"sessions,omitempty"`
ActiveSessionId string `json:"activesessionid,omitempty"`
Screens []*ScreenType `json:"screens,omitempty"`
ScreenLines *ScreenLinesType `json:"screenlines,omitempty"`
Line *LineType `json:"line,omitempty"`
Lines []*LineType `json:"lines,omitempty"`
Cmd *CmdType `json:"cmd,omitempty"`