From 22d0c5687e6a2e3e49d532b0b3139a4b689b8da4 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 3 Mar 2023 13:31:16 -0800 Subject: [PATCH] call to purge history items --- pkg/cmdrunner/cmdrunner.go | 74 +++++++++++++++++++++++++++++--------- pkg/sstore/dbops.go | 59 ++++++++++++++++++++++-------- pkg/sstore/updatebus.go | 1 + 3 files changed, 103 insertions(+), 31 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 48ec85662..3ceb7582b 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -178,6 +178,7 @@ func init() { registerCmdFn("history", HistoryCommand) registerCmdFn("history:viewall", HistoryViewAllCommand) + registerCmdFn("history:purge", HistoryPurgeCommand) registerCmdFn("bookmarks:show", BookmarksShowCommand) @@ -1814,6 +1815,38 @@ func ClearCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore } +func HistoryPurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { + if len(pk.Args) == 0 { + return nil, fmt.Errorf("/history:purge requires at least one argument (history id)") + } + var historyIds []string + for _, historyArg := range pk.Args { + _, err := uuid.Parse(historyArg) + if err != nil { + return nil, fmt.Errorf("invalid historyid (must be uuid)") + } + historyIds = append(historyIds, historyArg) + } + historyItemsRemoved, err := sstore.PurgeHistoryByIds(ctx, historyIds) + if err != nil { + return nil, fmt.Errorf("/history:purge error purging items: %v", err) + } + update := sstore.ModelUpdate{} + for _, historyItem := range historyItemsRemoved { + if historyItem.LineId == "" { + continue + } + lineObj := &sstore.LineType{ + SessionId: historyItem.SessionId, + WindowId: historyItem.WindowId, + LineId: historyItem.LineId, + Remove: true, + } + update.Lines = append(update.Lines, lineObj) + } + return update, nil +} + const HistoryViewPageSize = 50 func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { @@ -2258,27 +2291,34 @@ func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss return nil, err } if len(pk.Args) == 0 { - return nil, fmt.Errorf("/line:purge requires an argument (line number or id)") + return nil, fmt.Errorf("/line:purge requires at least one argument (line number or id)") } - lineArg := pk.Args[0] - lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + var lineIds []string + for _, lineArg := range pk.Args { + lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg) + if err != nil { + return nil, fmt.Errorf("error looking up lineid: %v", err) + } + if lineId == "" { + return nil, fmt.Errorf("line %q not found", lineArg) + } + lineIds = append(lineIds, lineId) + } + err = sstore.PurgeLinesByIds(ctx, ids.SessionId, lineIds) if err != nil { - return nil, fmt.Errorf("error looking up lineid: %v", err) + return nil, fmt.Errorf("/line:purge error purging lines: %v", err) } - if lineId == "" { - return nil, fmt.Errorf("line %q not found", lineArg) + update := sstore.ModelUpdate{} + for _, lineId := range lineIds { + lineObj := &sstore.LineType{ + SessionId: ids.SessionId, + WindowId: ids.WindowId, + LineId: lineId, + Remove: true, + } + update.Lines = append(update.Lines, lineObj) } - err = sstore.PurgeLineById(ctx, ids.SessionId, lineId) - if err != nil { - return nil, fmt.Errorf("/line:purge error purging line: %v", err) - } - lineObj := &sstore.LineType{ - SessionId: ids.SessionId, - WindowId: ids.WindowId, - LineId: lineId, - Remove: true, - } - return sstore.ModelUpdate{Line: lineObj}, nil + return update, nil } func LineShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 8201c183e..6886b7e72 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1885,21 +1885,23 @@ func purgeCmdById(ctx context.Context, sessionId string, cmdId string) error { return txErr } -func PurgeLineById(ctx context.Context, sessionId string, lineId string) error { +func PurgeLinesByIds(ctx context.Context, sessionId string, lineIds []string) error { txErr := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT cmdid FROM line WHERE sessionid = ? AND lineid = ?` - cmdId := tx.GetString(query, sessionId, lineId) - query = `DELETE FROM line WHERE sessionid = ? AND lineid = ?` - tx.Exec(query, sessionId, lineId) - query = `DELETE FROM history WHERE sessionid = ? AND lineid = ?` - tx.Exec(query, sessionId, lineId) - if cmdId != "" { - query = `SELECT count(*) FROM line WHERE sessionid = ? AND cmdid = ?` - cmdRefCount := tx.GetInt(query, sessionId, cmdId) - if cmdRefCount == 0 { - err := purgeCmdById(tx.Context(), sessionId, cmdId) - if err != nil { - return err + for _, lineId := range lineIds { + query := `SELECT cmdid FROM line WHERE sessionid = ? AND lineid = ?` + cmdId := tx.GetString(query, sessionId, lineId) + query = `DELETE FROM line WHERE sessionid = ? AND lineid = ?` + tx.Exec(query, sessionId, lineId) + query = `DELETE FROM history WHERE sessionid = ? AND lineid = ?` + tx.Exec(query, sessionId, lineId) + if cmdId != "" { + query = `SELECT count(*) FROM line WHERE sessionid = ? AND cmdid = ?` + cmdRefCount := tx.GetInt(query, sessionId, cmdId) + if cmdRefCount == 0 { + err := purgeCmdById(tx.Context(), sessionId, cmdId) + if err != nil { + return err + } } } } @@ -2388,3 +2390,32 @@ func GetLineCmdsFromHistoryItems(ctx context.Context, historyItems []*HistoryIte } return lineArr, cmdArr, nil } + +func PurgeHistoryByIds(ctx context.Context, historyIds []string) ([]*HistoryItemType, error) { + var rtn []*HistoryItemType + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT * FROM history WHERE historyid IN (SELECT value FROM json_each(?))` + marr := tx.SelectMaps(query, quickJsonArr(historyIds)) + for _, m := range marr { + hitem := HistoryItemFromMap(m) + if hitem != nil { + rtn = append(rtn, hitem) + } + } + query = `DELETE FROM history WHERE historyid IN (SELECT value FROM json_each(?))` + tx.Exec(query, quickJsonArr(historyIds)) + for _, hitem := range rtn { + if hitem.LineId != "" { + err := PurgeLinesByIds(tx.Context(), hitem.SessionId, []string{hitem.LineId}) + if err != nil { + return err + } + } + } + return nil + }) + if txErr != nil { + return nil, txErr + } + return rtn, nil +} diff --git a/pkg/sstore/updatebus.go b/pkg/sstore/updatebus.go index 1e9e30dc9..cd7d7cf37 100644 --- a/pkg/sstore/updatebus.go +++ b/pkg/sstore/updatebus.go @@ -35,6 +35,7 @@ type ModelUpdate struct { Windows []*WindowType `json:"windows,omitempty"` ScreenWindows []*ScreenWindowType `json:"screenwindows,omitempty"` Line *LineType `json:"line,omitempty"` + Lines []*LineType `json:"lines,omitempty"` Cmd *CmdType `json:"cmd,omitempty"` CmdLine *CmdLineType `json:"cmdline,omitempty"` Info *InfoMsgType `json:"info,omitempty"`