From 064e31eb2cc2bb6b9649449188143393c34224f2 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 17 Mar 2023 21:36:49 -0700 Subject: [PATCH] fix history query time --- db/migrations/000012_historylinenum.down.sql | 1 + db/migrations/000012_historylinenum.up.sql | 6 ++++++ pkg/cmdrunner/cmdrunner.go | 3 +++ pkg/sstore/dbops.go | 8 ++++---- pkg/sstore/migrate.go | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 db/migrations/000012_historylinenum.down.sql create mode 100644 db/migrations/000012_historylinenum.up.sql diff --git a/db/migrations/000012_historylinenum.down.sql b/db/migrations/000012_historylinenum.down.sql new file mode 100644 index 00000000..89bde3e6 --- /dev/null +++ b/db/migrations/000012_historylinenum.down.sql @@ -0,0 +1 @@ +ALTER TABLE history DROP COLUMN linenum; diff --git a/db/migrations/000012_historylinenum.up.sql b/db/migrations/000012_historylinenum.up.sql new file mode 100644 index 00000000..d7dc6353 --- /dev/null +++ b/db/migrations/000012_historylinenum.up.sql @@ -0,0 +1,6 @@ +ALTER TABLE history ADD COLUMN linenum int NOT NULL DEFAULT 0; + +UPDATE history +SET linenum = COALESCE((SELECT line.linenum FROM line WHERE line.lineid = history.lineid), 0) +; + diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 43b2308f..6cfb69c1 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -95,6 +95,7 @@ type SetVarScope struct { type historyContextType struct { LineId string + LineNum int64 CmdId string RemotePtr *sstore.RemotePtrType } @@ -378,6 +379,7 @@ func addToHistory(ctx context.Context, pk *scpacket.FeCommandPacketType, history SessionId: ids.SessionId, ScreenId: ids.ScreenId, LineId: historyContext.LineId, + LineNum: historyContext.LineNum, HadError: hadError, CmdId: historyContext.CmdId, CmdStr: cmdStr, @@ -1240,6 +1242,7 @@ func updateHistoryContext(ctx context.Context, line *sstore.LineType, cmd *sstor hctx := ctxVal.(*historyContextType) if line != nil { hctx.LineId = line.LineId + hctx.LineNum = line.LineNum } if cmd != nil { hctx.CmdId = cmd.CmdId diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index d3aa1c5b..e10f6571 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 = "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 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, h.linenum" const DefaultMaxHistoryItems = 1000 type SingleConnDBGetter struct { @@ -187,8 +187,8 @@ func InsertHistoryItem(ctx context.Context, hitem *HistoryItemType) error { } txErr := WithTx(ctx, func(tx *TxWrap) error { query := `INSERT INTO history - ( historyid, ts, userid, sessionid, screenid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito) VALUES - (:historyid,:ts,:userid,:sessionid,:screenid,:lineid,:cmdid,:haderror,:cmdstr,:remoteownerid,:remoteid,:remotename,:ismetacmd,:incognito)` + ( historyid, ts, userid, sessionid, screenid, lineid, cmdid, haderror, cmdstr, remoteownerid, remoteid, remotename, ismetacmd, incognito, linenum) VALUES + (:historyid,:ts,:userid,:sessionid,:screenid,:lineid,:cmdid,:haderror,:cmdstr,:remoteownerid,:remoteid,:remotename,:ismetacmd,:incognito,:linenum)` tx.NamedExec(query, hitem.ToMap()) return nil }) @@ -315,7 +315,7 @@ func runHistoryQuery(tx *TxWrap, opts HistoryQueryOpts, realOffset int, itemLimi if opts.NoMeta { whereClause += " AND NOT h.ismetacmd" } - 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) + query := fmt.Sprintf("SELECT %s, ('%s' || CAST((row_number() OVER win) as text)) historynum FROM history h %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) marr := tx.SelectMaps(query, queryArgs...) rtn := make([]*HistoryItemType, len(marr)) for idx, m := range marr { diff --git a/pkg/sstore/migrate.go b/pkg/sstore/migrate.go index 64b52d60..894e4769 100644 --- a/pkg/sstore/migrate.go +++ b/pkg/sstore/migrate.go @@ -17,7 +17,7 @@ import ( "github.com/golang-migrate/migrate/v4" ) -const MaxMigration = 11 +const MaxMigration = 12 const MigratePrimaryScreenVersion = 9 func MakeMigrate() (*migrate.Migrate, error) {