add linenum to line, nextlinenum to window

This commit is contained in:
sawka 2022-09-20 17:01:25 -07:00
parent eab785409a
commit db142d97ec
4 changed files with 59 additions and 33 deletions

View File

@ -22,6 +22,7 @@ CREATE TABLE window (
curremoteownerid varchar(36) NOT NULL, curremoteownerid varchar(36) NOT NULL,
curremoteid varchar(36) NOT NULL, curremoteid varchar(36) NOT NULL,
curremotename varchar(50) NOT NULL, curremotename varchar(50) NOT NULL,
nextlinenum int NOT NULL,
winopts json NOT NULL, winopts json NOT NULL,
ownerid varchar(36) NOT NULL, ownerid varchar(36) NOT NULL,
sharemode varchar(12) NOT NULL, sharemode varchar(12) NOT NULL,
@ -63,10 +64,13 @@ CREATE TABLE remote_instance (
CREATE TABLE line ( CREATE TABLE line (
sessionid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL,
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
userid varchar(36) NOT NULL, userid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
ts bigint NOT NULL, ts bigint NOT NULL,
linenum int NOT NULL,
linenumtemp boolean NOT NULL,
linetype varchar(10) NOT NULL, linetype varchar(10) NOT NULL,
linelocal boolean NOT NULL,
text text NOT NULL, text text NOT NULL,
cmdid varchar(36) NOT NULL, cmdid varchar(36) NOT NULL,
ephemeral boolean NOT NULL, ephemeral boolean NOT NULL,

View File

@ -59,10 +59,13 @@ CREATE TABLE remote_instance (
CREATE TABLE line ( CREATE TABLE line (
sessionid varchar(36) NOT NULL, sessionid varchar(36) NOT NULL,
windowid varchar(36) NOT NULL, windowid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
userid varchar(36) NOT NULL, userid varchar(36) NOT NULL,
lineid varchar(36) NOT NULL,
ts bigint NOT NULL, ts bigint NOT NULL,
linenum int NOT NULL,
linenumtemp boolean NOT NULL,
linetype varchar(10) NOT NULL, linetype varchar(10) NOT NULL,
linelocal boolean NOT NULL,
text text NOT NULL, text text NOT NULL,
cmdid varchar(36) NOT NULL, cmdid varchar(36) NOT NULL,
ephemeral boolean NOT NULL, ephemeral boolean NOT NULL,

View File

@ -512,16 +512,17 @@ func GetScreenById(ctx context.Context, sessionId string, screenId string) (*Scr
func txCreateWindow(tx *TxWrap, sessionId string, curRemote RemotePtrType) string { func txCreateWindow(tx *TxWrap, sessionId string, curRemote RemotePtrType) string {
w := &WindowType{ w := &WindowType{
SessionId: sessionId, SessionId: sessionId,
WindowId: uuid.New().String(), WindowId: uuid.New().String(),
CurRemote: curRemote, CurRemote: curRemote,
WinOpts: WindowOptsType{}, NextLineNum: 1,
ShareMode: ShareModeLocal, WinOpts: WindowOptsType{},
ShareOpts: WindowShareOptsType{}, ShareMode: ShareModeLocal,
ShareOpts: WindowShareOptsType{},
} }
wmap := w.ToMap() wmap := w.ToMap()
query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, winopts, ownerid, sharemode, shareopts) query := `INSERT INTO window ( sessionid, windowid, curremoteownerid, curremoteid, curremotename, nextlinenum, winopts, ownerid, sharemode, shareopts)
VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:winopts,:ownerid,:sharemode,:shareopts)` VALUES (:sessionid,:windowid,:curremoteownerid,:curremoteid,:curremotename,:nextlinenum,:winopts,:ownerid,:sharemode,:shareopts)`
tx.NamedExecWrap(query, wmap) tx.NamedExecWrap(query, wmap)
return w.WindowId return w.WindowId
} }
@ -533,6 +534,9 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if line.LineId == "" { if line.LineId == "" {
return fmt.Errorf("line must have lineid set") return fmt.Errorf("line must have lineid set")
} }
if line.LineNum != 0 {
return fmt.Errorf("line should not hage linenum set")
}
return WithTx(ctx, func(tx *TxWrap) error { return WithTx(ctx, func(tx *TxWrap) error {
var windowId string var windowId string
query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?` query := `SELECT windowid FROM window WHERE sessionid = ? AND windowid = ?`
@ -540,9 +544,14 @@ func InsertLine(ctx context.Context, line *LineType, cmd *CmdType) error {
if !hasWindow { if !hasWindow {
return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId) return fmt.Errorf("window not found, cannot insert line[%s/%s]", line.SessionId, line.WindowId)
} }
query = `INSERT INTO line ( sessionid, windowid, lineid, ts, userid, linetype, text, cmdid, ephemeral) query = `SELECT nextlinenum FROM window WHERE sessionid = ? AND windowid = ?`
VALUES (:sessionid,:windowid,:lineid,:ts,:userid,:linetype,:text,:cmdid,:ephemeral)` nextLineNum := tx.GetInt(query, line.SessionId, line.WindowId)
line.LineNum = int64(nextLineNum)
query = `INSERT INTO line ( sessionid, windowid, userid, lineid, ts, linenum, linenumtemp, linelocal, linetype, text, cmdid, ephemeral)
VALUES (:sessionid,:windowid,:userid,:lineid,:ts,:linenum,:linenumtemp,:linelocal,:linetype,:text,:cmdid,:ephemeral)`
tx.NamedExecWrap(query, line) tx.NamedExecWrap(query, line)
query = `UPDATE window SET nextlinenum = ? WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, nextLineNum+1, line.SessionId, line.WindowId)
if cmd != nil { if cmd != nil {
cmdMap := cmd.ToMap() cmdMap := cmd.ToMap()
query = ` query = `
@ -876,6 +885,8 @@ func ClearWindow(ctx context.Context, sessionId string, windowId string) (*Model
lineIds = tx.SelectStrings(query, sessionId, windowId) lineIds = tx.SelectStrings(query, sessionId, windowId)
query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?` query = `DELETE FROM line WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, sessionId, windowId) tx.ExecWrap(query, sessionId, windowId)
query = `UPDATE window SET nextlinenum = 1 WHERE sessionid = ? AND windowid = ?`
tx.ExecWrap(query, sessionId, windowId)
return nil return nil
}) })
if txErr != nil { if txErr != nil {

View File

@ -197,15 +197,16 @@ func (r RemotePtrType) MakeFullRemoteRef() string {
} }
type WindowType struct { type WindowType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
CurRemote RemotePtrType `json:"curremote"` CurRemote RemotePtrType `json:"curremote"`
WinOpts WindowOptsType `json:"winopts"` WinOpts WindowOptsType `json:"winopts"`
OwnerId string `json:"ownerid"` OwnerId string `json:"ownerid"`
ShareMode string `json:"sharemode"` NextLineNum int64 `json:"nextlinenum"`
ShareOpts WindowShareOptsType `json:"shareopts"` ShareMode string `json:"sharemode"`
Lines []*LineType `json:"lines"` ShareOpts WindowShareOptsType `json:"shareopts"`
Cmds []*CmdType `json:"cmds"` Lines []*LineType `json:"lines"`
Cmds []*CmdType `json:"cmds"`
// only for updates // only for updates
Remove bool `json:"remove,omitempty"` Remove bool `json:"remove,omitempty"`
@ -218,6 +219,7 @@ func (w *WindowType) ToMap() map[string]interface{} {
rtn["curremoteownerid"] = w.CurRemote.OwnerId rtn["curremoteownerid"] = w.CurRemote.OwnerId
rtn["curremoteid"] = w.CurRemote.RemoteId rtn["curremoteid"] = w.CurRemote.RemoteId
rtn["curremotename"] = w.CurRemote.Name rtn["curremotename"] = w.CurRemote.Name
rtn["nextlinenum"] = w.NextLineNum
rtn["winopts"] = quickJson(w.WinOpts) rtn["winopts"] = quickJson(w.WinOpts)
rtn["ownerid"] = w.OwnerId rtn["ownerid"] = w.OwnerId
rtn["sharemode"] = w.ShareMode rtn["sharemode"] = w.ShareMode
@ -235,6 +237,7 @@ func WindowFromMap(m map[string]interface{}) *WindowType {
quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid") quickSetStr(&w.CurRemote.OwnerId, m, "curremoteownerid")
quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid") quickSetStr(&w.CurRemote.RemoteId, m, "curremoteid")
quickSetStr(&w.CurRemote.Name, m, "curremotename") quickSetStr(&w.CurRemote.Name, m, "curremotename")
quickSetInt64(&w.NextLineNum, m, "nextlinenum")
quickSetJson(&w.WinOpts, m, "winopts") quickSetJson(&w.WinOpts, m, "winopts")
quickSetStr(&w.OwnerId, m, "ownerid") quickSetStr(&w.OwnerId, m, "ownerid")
quickSetStr(&w.ShareMode, m, "sharemode") quickSetStr(&w.ShareMode, m, "sharemode")
@ -416,16 +419,19 @@ type RemoteInstance struct {
} }
type LineType struct { type LineType struct {
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
LineId string `json:"lineid"` UserId string `json:"userid"`
Ts int64 `json:"ts"` LineId string `json:"lineid"`
UserId string `json:"userid"` Ts int64 `json:"ts"`
LineType string `json:"linetype"` LineNum int64 `json:"linenum"`
Text string `json:"text,omitempty"` LineNumTemp bool `json:"linenumtemp,omitempty"`
CmdId string `json:"cmdid,omitempty"` LineLocal bool `json:"linelocal"`
Ephemeral bool `json:"ephemeral,omitempty"` LineType string `json:"linetype"`
Remove bool `json:"remove,omitempty"` Text string `json:"text,omitempty"`
CmdId string `json:"cmdid,omitempty"`
Ephemeral bool `json:"ephemeral,omitempty"`
Remove bool `json:"remove,omitempty"`
} }
type SSHOpts struct { type SSHOpts struct {
@ -574,9 +580,10 @@ func makeNewLineCmd(sessionId string, windowId string, userId string, cmdId stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.UserId = userId
rtn.LineId = uuid.New().String() rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.LineLocal = true
rtn.LineType = LineTypeCmd rtn.LineType = LineTypeCmd
rtn.CmdId = cmdId rtn.CmdId = cmdId
return rtn return rtn
@ -586,9 +593,10 @@ func makeNewLineText(sessionId string, windowId string, userId string, text stri
rtn := &LineType{} rtn := &LineType{}
rtn.SessionId = sessionId rtn.SessionId = sessionId
rtn.WindowId = windowId rtn.WindowId = windowId
rtn.UserId = userId
rtn.LineId = uuid.New().String() rtn.LineId = uuid.New().String()
rtn.Ts = time.Now().UnixMilli() rtn.Ts = time.Now().UnixMilli()
rtn.UserId = userId rtn.LineLocal = true
rtn.LineType = LineTypeText rtn.LineType = LineTypeText
rtn.Text = text rtn.Text = text
return rtn return rtn