From f86f0a78b121b1b24d70a113a1c43504ec7aa227 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 15 Mar 2023 12:27:17 -0700 Subject: [PATCH] restore historynum and add linenum to historyitems --- pkg/sstore/dbops.go | 25 +++++++++++++------------ pkg/sstore/sstore.go | 2 ++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index b0986d0d6..b61476e36 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -17,7 +17,7 @@ import ( "github.com/scripthaus-dev/sh2-server/pkg/scbase" ) -const HistoryCols = "historyid, ts, userid, sessionid, screenid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito" +const HistoryCols = "h.historyid, h.ts, h.userid, h.sessionid, h.screenid, h.lineid, h.cmdid, h.haderror, h.cmdstr, h.remoteownerid, h.remoteid, h.remotename, h.ismetacmd, h.incognito" const DefaultMaxHistoryItems = 1000 type SingleConnDBGetter struct { @@ -287,35 +287,36 @@ func runHistoryQuery(tx *TxWrap, opts HistoryQueryOpts, realOffset int, itemLimi return nil, fmt.Errorf("malformed remoteid") } } - hnumStr := "" whereClause := "WHERE 1" var queryArgs []interface{} + hNumStr := "" if opts.SessionId != "" && opts.ScreenId != "" { - whereClause += fmt.Sprintf(" AND sessionid = '%s' AND screenid = '%s'", opts.SessionId, opts.ScreenId) - hnumStr = "w" + whereClause += fmt.Sprintf(" AND h.sessionid = '%s' AND h.screenid = '%s'", opts.SessionId, opts.ScreenId) + hNumStr = "" } else if opts.SessionId != "" { - whereClause += fmt.Sprintf(" AND sessionid = '%s'", opts.SessionId) - hnumStr = "s" + whereClause += fmt.Sprintf(" AND h.sessionid = '%s'", opts.SessionId) + hNumStr = "s" } else { - hnumStr = "g" + hNumStr = "g" } if opts.SearchText != "" { - whereClause += " AND cmdstr LIKE ? ESCAPE '\\'" + whereClause += " AND h.cmdstr LIKE ? ESCAPE '\\'" likeArg := opts.SearchText likeArg = strings.ReplaceAll(likeArg, "%", "\\%") likeArg = strings.ReplaceAll(likeArg, "_", "\\_") queryArgs = append(queryArgs, "%"+likeArg+"%") } if opts.FromTs > 0 { - whereClause += fmt.Sprintf(" AND ts <= %d", opts.FromTs) + whereClause += fmt.Sprintf(" AND h.ts <= %d", opts.FromTs) } if opts.RemoteId != "" { - whereClause += fmt.Sprintf(" AND remoteid = '%s'", opts.RemoteId) + whereClause += fmt.Sprintf(" AND h.remoteid = '%s'", opts.RemoteId) } if opts.NoMeta { - whereClause += " AND NOT ismetacmd" + whereClause += " AND NOT h.ismetacmd" } - query := fmt.Sprintf("SELECT %s, '%s' || row_number() OVER win AS historynum FROM history %s WINDOW win AS (ORDER BY ts, historyid) ORDER BY ts DESC, historyid DESC LIMIT %d OFFSET %d", HistoryCols, hnumStr, whereClause, itemLimit, realOffset) + query := fmt.Sprintf("SELECT %s, ('%s' || CAST((row_number() OVER win) as text)) historynum, l.linenum FROM history h LEFT OUTER JOIN line l ON (h.lineid = l.lineid) %s WINDOW win AS (ORDER BY h.ts, h.historyid) ORDER BY h.ts DESC, h.historyid DESC LIMIT %d OFFSET %d", HistoryCols, hNumStr, whereClause, itemLimit, realOffset) + fmt.Printf("HISTORY QUERY %s\n", query) marr := tx.SelectMaps(query, queryArgs...) rtn := make([]*HistoryItemType, len(marr)) for idx, m := range marr { diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index de02764c6..d1978ddcf 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -368,6 +368,7 @@ func (h *HistoryItemType) FromMap(m map[string]interface{}) bool { quickSetStr(&h.Remote.Name, m, "remotename") quickSetBool(&h.IsMetaCmd, m, "ismetacmd") quickSetStr(&h.HistoryNum, m, "historynum") + quickSetInt64(&h.LineNum, m, "linenum") quickSetBool(&h.Incognito, m, "incognito") return true } @@ -497,6 +498,7 @@ type HistoryItemType struct { // transient (string because of different history orderings) HistoryNum string `json:"historynum"` + LineNum int64 `json:"linenum"` } type HistoryQueryOpts struct {